AP CSA 4.5 Debugging: The Average That Rounds Itself

Unit 4: Data Collections · Lesson 4.5 · Debugging

The Average That Rounds Itself

Algorithms with Arrays. This is not a blank editor: it is someone else's attempt. Find what is wrong, fix it, and submit to be graded against hidden test cases.

Why debugging is its own skill

Dividing one int by another int throws the remainder away before anything else happens, so casting the result to double afterward preserves a number that is already wrong. The cast has to come first, and where you put it is the whole bug.

What is wrong with it

  1. The average is computed by dividing two ints and then casting to double, which rounds down before the cast can help. Cast the total to double BEFORE the division so the fractional part survives.
  2. The search reports the index of the target but starts scanning at index 1, so a target sitting at index 0 is reported as not found. Fix the starting index.
  3. The total is computed correctly and should stay as int.

The program reads

A count, then that many integers, then a target value to find.

The program should print

Three lines: the total, the average as a double, and the index of the first occurrence of the target or -1.

Worked examples

These show what the FIXED program should print. There are more cases you cannot see, and they use different values, so patching around just these numbers will fail.

Example 1 input
2
7 0
7
Example 1 output
7
3.5
0
Example 2 input
4
1 2 3 4
3
Example 2 output
10
2.5
2

Your answer

Main.java

Input for the Run button

Run sends whatever is in the code box below, bugs and all, so you can see the crash or the wrong answer for yourself before you fix anything.


  

  

Stuck?

Hint 1

Work out 7 / 2 in ints: the answer is 3, and the 0.5 is gone permanently. Casting 3 to double gives 3.0, not 3.5. The cast must apply to an operand, not to the finished result.

Hint 2

(double) total / data.length casts only total, and one double operand promotes the whole division to double arithmetic. That is why the parentheses placement matters so much.

Hint 3

Any search that starts at index 1 is claiming index 0 cannot hold the answer. Test with a target sitting at the front and the bug appears immediately.

Where to go next

Get in Touch

Whether you're a student, parent, or teacher — I'd love to hear from you.

Just want free AP CS resources?

Enter your email below and check the subscribe box — no message needed. Students get daily practice questions and study tips. Teachers get curriculum resources and teaching strategies.

Typically responds within 24 hours

Message Sent!

Thanks for reaching out. I'll get back to you within 24 hours.

🏫 Welcome, fellow educator!

I offer curriculum resources, practice materials, and study guides designed for AP CS teachers. Let me know what you're looking for — whether it's classroom materials, a guest speaker, or Teachers Pay Teachers resources.

Email

[email protected]

📚

Courses

AP CSA, CSP, & Cybersecurity

Response Time

Within 24 hours

Prefer email? Reach me directly at [email protected]