To debug the program, the most appropriate variable to print out would be sum
.
The reason for this is that the calculation of the sum is incorrect. In the line sum = temp + 1
, the program is mistakenly adding 1 to the current grade instead of adding the grade value to the running total of grades. Printing sum
at various points in the loop will help you see how it's being computed.
Here's a brief explanation of the error in the code:
- Instead of accumulating the grades by adding them to
sum
, it setssum
totemp + 1
each time through the loop, which results in the incorrect average being calculated.
To correct the code, you should change line 4 to:
sum += temp
This will correctly accumulate the grades input by the user.