Chapter 5- Algorithm Workbench

NOTE: There are no TABS on the codes. THESE ARE MY PERSONAL solutions to the problem. Copy at your own risk.

1. Find the errors in the following pseudocode.
PROGRAM:
Declare Boolean finished = False
Declare Integer value, cube

While NOT finished
Display “Enter a value to be cubed.”
Input value;
Set cube = value^3
Display value, ” cubed is ” , cube
End While

ANSWER:
//’finished’ variable was never changed in the body of the loop.
Declare Boolean finished = False
Declare Integer value, cube

While NOT finished
Display “Enter a value to be cubed.”
Input value;
//’Input value’ should not have a semicolon at the end
Set cube = value^3
Display value, ” cubed is ” , cube
End While

2. The programmer intended the following pseudocode to display the 1 through 60, and then display the message “Time’s up!” It will not function as intended, however. Find the error.
PROGRAM:
Declare Integer counter = 1
Const Integer TIME_LIMIT = 60

While counter < TIME_LIMIT Display counter Set counter = counter + 1 End While Display "Time's up!" ANSWER: Declare Integer counter = 1 Const Integer TIME_LIMIT = 60 //'Const' should be 'Constant' //'counter' variable should be <= TIME_LIMIT or else it will only show up to 59 While counter <= TIME_LIMIT Display counter Set counter = counter + 1 End While Display "Time's up!" 3. The programmer intended the following pseudocode to get five sets of two numbers each, calculate the sum of each set, and calculate the sum of all the numbers entered. It will not function as intended, however. Find the error. PROGRAM: //This program calculates the sum of five sets of two numbers. Declare Integer number, sum, total Declare Integer sets, numbers Constant Integer MAX_SETS = 5 Constant Integer MAX_NUMBERS = 2 Set sum = 0; Set total = 0; For sets = 1 To MAX_NUMBERS For numbers = 1 To MAX_SETS Display "Enter number ", numbers, " of sets ", sets, "." Input number; Set sum = sum + number End For Display "The sum of set ", sets, " is ", sum "." Set total = total + sum Set sum = 0 End For Display "The total of all the sets is ", total, "." ANSWER: //'sets' and 'numbers' variables in For loops have wrong limit conditions. //The correct pseudocode should have: For sets = 1 To MAX_SETS For numbers = 1 To MAX_NUMBERS //Set sum = 0 should not have a semicolon at the end //Set total = 0 should not have a semicolon at the end

Leave a Reply

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