Professional Documents
Culture Documents
1. Write a program containing an infinite loop which outputs the series of integers
starting at 5 and going up by 5s. Revise this program to output the integers starting at 5 and
decreasing by 10s.
SOLUTION:
Write a program containing an infinite loop which outputs the series of integers,
starting at 5 and going up by 5s.
Revise this program to output the integers starting at 5 and decreasing by 10s.
6-2. Write a program that endlessly tells you to "Have a good day". Try stopping execution. Change
it so that it is a program to wish you a good day only six times.
SOLUTION:
Change it so that it is a program to wish you a good day only six times.
6-3. Write a program that reads words entered one to a line and counts how many words have
been entered before you give the signal word "end" to stop execution of the program.
Experiment to determine what happens if you put several words on a line as you enter them.
SOLUTION:
If more than one word is entered on a line, each one is read in turn as though it were entered
properly when the prompt is given. However, the count is increased by only one because we
are pssing through the loop only once.
Textbook Solutions to Exercises
Chapter 6: Repetition - Section 6.6
6-4. A series of marks is to be entered and averaged. Before you enter the series, you are to have
the program ask you how many marks there are in the series then read it in. Test your program
to see that it works for series of different lengths, say four marks or six marks.
SOLUTION:
SOLUTION:
6-6.a Write a program to output a table of values of the integers starting at 1 and their squares.
Label the table at the top of the columns. For example, your output might look like this
Number Square
1 1
2 4
3 9
4 16
5 25
Try to format the output so that it looks attractive. What happens as the numbers get larger
and larger? Change the program to output the first 100 integers rather than attempting to go
on forever.
SOLUTION:
As the numbers get larger, the number of digits in the SOLUTION increase. However,
Turing has an upper limit on integer values and this limit is passed the program halts with an
error message. This implementation of Turing limits integers to 2**31-1 (a number slightly
over 2 billion).
Continue through to
SOLUTION:
6-7. Write a program using a loop counting backwards. Output the index of the loop on each
execution so the output is the same as the count down for a rocket launch. Arrange the output
so that it is all on one line like this
5 4 3 2 1
SOLUTION:
6-8. Write a program to output a backwards count by 5s from 100 down to 5. Modify it so that you
count from 100 down to 50. Modify it so that before you start the count you can input a
number between 100 and 50 so that the program will stop when the count would be less than
the number input. For example the execution might be like this:
SOLUTION:
Write a program to output a backwards count by 5s from 100 down to 5.
6-9. Write a program to find the sum of a number of terms of the infinite series 1 + x +
x**2 + x**3 + x**4 + ...
where the number of terms n to be evaluated and the value of x are input before the
summation begins. Experiment with different values of n and x.
SOLUTION:
% The "06-09 Solution" Program
var n: int
put "How many terms should be evaluated:"..
get n
var x: real
put "Enter a value for x:" ..
get x
var sum : real := 0 Starting to count with 0 solves a tricky
problem that you might have run into when
for term : 0 .. n- 1 you tried this exercise. Why is it used here?
sum := sum + x ** term
end for
put "Sum of terms is:", sum
6-10. Write a program to compute the bank balance at the end of each year for 10 years resulting
from an initial deposit of $1000 and an annual interest rate of 6%. Output for each year end
the number of the year, the initial balance, the interest for the year, and the balance at the end
of the year.
SOLUTION:
% The "06-10 Solution" Program
const numberOfYears := 10
const interestRate := .06
const initialDeposit := 1000.00
var balance : real := initialDeposit
put "Year Start Balance Interest End Balance "
for year: 1 .. numberOfYears
const interest := balance * interestRate
put year: 3, balance: 15 : 2, interest: 10 : 2,
balance + interest: 13 :,
2 balance := balance + interest
end for