Solutions 6-1
Programming Logic and Design, 6th Edition
Chapter 6
Exercises
1.
a. Design the logic for a program that allows a user to enter 10 numbers, then displays them in the reverse order of their entry.
Answer:
A sample solution follows
Flowchart:
Pseudocode: start Declarations num index num SIZE = 10 num numbers[SIZE] = 0,0,0,0,0,0,0,0,0,0 getReady() Programming Logic and Design, 6e
Solutions 6-2
while index < SIZE getNumbers() endwhile finishUp() stop getReady() index = 0 return getNumbers() output “Enter a number for position ”, index input numbers[index] index = index + 1 return finishUp() output “The numbers in reverse order are: ” while index > 0 index = index – 1 output numbers[index] endwhile return
b. Modify the reverse-display program so that the user can enter up to 10 numbers until a sentinel value is entered.
Answer:
A sample solution follows
Flowchart:
Programming Logic and Design, 6e
Pseudocode: start Declarations num index num SIZE = 10 num numbers[SIZE] = 0,0,0,0,0,0,0,0,0,0 string CONTINUE = “Y” string moreNumbers = CONTINUE getReady() while index < SIZE AND moreNumbers equal to CONTINUE getNumbers() endwhile finishUp() stop getReady() index = 0 output “Do you want to enter a number? (Y/N)” input moreNumbers return getNumbers() output “Enter a number for position ”, index input numbers[index] index = index + 1
Solutions 6-3
Programming Logic and Design, 6e
Solutions 6-4
output “Do you want to enter more numbers? (Y/N)” input moreNumbers return finishUp() output “The numbers in reverse order are: ” while index > 0 index = index – 1 output numbers[index] endwhile return
2.
a. Design the logic for a program that allows a user to enter 10 numbers, then displays each and its difference from the numeric average of the numbers.
Answer:
A sample solution follows
Flowchart:
Programming