Chapter 5
Exercises
1. What is output by each of the pseudocode segments in Figure 5-22?
Answer:
|a. |5, 22, 5 |
|b. |4, 6, 7 |
|c. |5, 6 |
|d. |Goodbye |
| |Goodbye |
| |Goodbye |
| |Goodbye |
| |Goodbye |
| …show more content…
Create the logic for a game that simulates rolling two dice by generating two numbers between 1 and 6 inclusive. The player chooses a number between 2 and 12 (the lowest and highest totals possible for two dice). The player then “rolls” two dice up to three times. If the number chosen by the user comes up, the user wins and the game ends. If the number does not come up within three rolls, the computer wins.
Answer: A sample solution is as follows:
Flowchart:
[pic]
Pseudocode:
start Declarations num dice1 num dice2 num guess num count num won num LIMIT = 6 string PROMPT = “Choose a number between 1 and 12“ string WIN = “You win!” string LOSE = “You lose!” housekeeping() while count < 3 AND won = 0 detailLoop() endwhile finishUp() stop
housekeeping() count = 0 won = 0 dice1 = random(LIMIT) dice2 = random(LIMIT) output PROMPT input guess return
detailLoop()