Chapter 3- Debugging Exercises

REMEMBER: These are MY answers. These are solutions but MY SOLUTIONS!!
Debugging Exercises: THERE ARE NO INDENTATIONS ON THESE CODES, YOU HAVE TO FIGURE IT OUT YOURSELF.

1. Find the error in the following pseudocode:
Module main()
Declare Real mileage
Call getMileage()
Display “You’ve driven a total of “, mileage, ” miles.”
End Module
Module getMileage()
Display “Enter your vehicle’s mileage.”
Input mileage
End Module
ANSWER:
The following statement should be in the ‘getMileage’ module after ‘Input mileage’
statement
Display “You’ve driven a total of “, mileage, ” miles.”

2. Find the error in the following pseudocode:
Module main()
Call getCalories()
End Module
Module getCalories()
Declare Real calories
Display “How many calories are in the first food?”
Input calories
Declare Real calories
Display “How many calories are in the second food?”
Input calories
End Module
ANSWER:
Variable ‘calories’ cannot be declared twice in the same module

3. Find the potential error in the following pseudocode:
Module main()
Call squareNumber(5)
End Module
Module squareNumber(Integer Ref number)
Set number = number ^ 2
Display number
End Module
ANSWER:
Cannot pass a non variable argument into a reference parameter

4. Find the error in the following pseudocode:
Module main()
Call raiseToPower(2, 1.5)
End Module
Module raiseToPower(Real value, Integer power)
Declare Real result
Set result = value ^ power
Display result
End Module
ANSWER:
Module raiseToPower should be called with a ‘Real’ and an ‘Integer’ type arguments.
It is being called in reverse way.

Leave a Reply

Your email address will not be published. Required fields are marked *