Page 86 - Introduction to Programming with Java: A Problem Solving Approach
P. 86
52 Chapter 2 Algorithms and Design
§2.11 Tracing
18. Which of the following is true?
a) Tracing shows sequence of execution. b) Tracing helps you debug a program.
c) Tracing highlights errors in loop initialization and termination. d) All of the above.
19. Trace the following Bowling Score algorithm (taken from Section 2.9). Use the setup shown below the algorithm.
1
2
set totalScore to 0 setcountto0
print “Enter score (1 to quit): ” input score
while score is not equal to 1
set totalScore to totalScore score set count to count 1
print “Enter score (1 to quit): ”
input score
5
4
6
3
7
9
8
10 set avg to totalScore / count 11 print “Average score is ” avg
Trace setup:
input 94 104 114 1
Exercises
Apago PDF Enhancer
line#
score
totalScore
count
avg
output
1. [after §2.5] Write pseudocode for an algorithm that (1) asks the user to input the length of the side of a square, (2) computes the square’s area, and (3) prints the square’s area. Use the following sample session.
Sample session:
Enter length of side of square in meters: 15
The area of the square is 225 square meters.
2. [after §2.8] What is an infinite loop?
3. [after §2.8] Given the following pseudocode, circle the statements that are considered to be within the body
of the while loop:
input time
while time is less than 8
print time settimetotime 1
4. [after §2.9] In exercise 3, suppose the user’s input for time is 3. How many lines of output will the algorithm generate?
The italics signify user input.