Chapter 7- Debugging Exercises

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

1. Why does the following pseudocode not perform as indicated in the comments?

//This program asks the user to enter a value between 1 and 10 and validates the input.
Declare Integer value

//Get a value from the user
Display “Enter a value between 1 and 10.”
Input value

//Make sure the value is between and 10
While value < 1 AND value > 10
Display “ERROR: The value must be between 1 and 10.”
Display “Enter a value between 1 and 10.”
Input value
End While

SOLUTION(Highlighted in bold below.):
Test condition in While loop will be always false because of the AND clause.
Correct fix will be to use an OR clause instead.

//This program asks the user to enter a value between 1 and 10 and validates the input.
Declare Integer value

//Get a value from the user
Display “Enter a value between 1 and 10.”
Input value

//Make sure the value is between and 10
While value < 1 OR value > 10
Display “ERROR: The value must be between 1 and 10.”
Display “Enter a value between 1 and 10.”
Input value
End While

2. Why does the following pseudocode not perform as indicated in the comments?

//This program gets a dollar amount from the user and validates the input.
Declare Real amount

//Get the amount from the user.
Display “Enter a dollar amount”
Input amount

//Make sure the amount is not less than zero. If it is, get a new amount from the user.
While amount < 0 Display "ERROR: The dollar amount cannot be less than 0." Display "Enter a dollar amount." End While SOLUTION(Highlighted in bold below.): The program does not read a user input into amount variable inside the While loop. Therefore, if the initial amount input is invalid it will go in an infinite loop. //This program gets a dollar amount from the user and validates the input. Declare Real amount //Get the amount from the user. Display "Enter a dollar amount" Input amount //Make sure the amount is not less than zero. If it is, get a new amount from the user. While amount < 0 Display "ERROR: The dollar amount cannot be less than 0." Display "Enter a dollar amount." Input amount End While 3. The following pseudocode works, but it performs a case-sensitive validation of the user's input. How could the algorithm be improved so the user does not have to pay attention to capitalization when entering a name? //This program asks the user to enter a string and validates the input. Declare String choice //Get the user's response Display "Cast your vote for Chess Team Captain." Display "Would you like to nominate Lisa or Tim?" Input choice //Validate the input. While choice != "Lisa" AND choice != "Tim" Display "Please enter Lisa or Tim." Display "Cast your vote for Chess Team Captain." Display "Would you like to nominate Lisa or Tim?" Input response End While SOLUTION(Highlighted in bold below.): Validate the input as case-insensitive using toLower() function. //This program asks the user to enter a string and validates the input. Declare String choice //Get the user's response Display "Cast your vote for Chess Team Captain." Display "Would you like to nominate Lisa or Tim?" Input choice //Validate the input. While toLower(choice) != "lisa" AND toLower(choice) != "tim" Display "Please enter Lisa or Tim." Display "Cast your vote for Chess Team Captain." Display "Would you like to nominate Lisa or Tim?" Input response End While

Leave a Reply

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