To debug the program and find out why the average is not printing out correctly, the most appropriate variable to print out would be sum
.
Here's the reason:
- The program is intended to accumulate the sum of grades over five iterations, but the line
sum = temp + 1
is incorrectly updatingsum
instead of addingtemp
to the currentsum
. This causessum
to always be equal to1
plus the last grade entered, which is why the average computation leads to an incorrect result.
By printing out sum
before line 6, you can see how the value changes with each iteration and identify that the accumulation of grades is not occurring as expected.
So, printing sum
would help you determine the erroneous logic in how the grades are summed.