You are on page 1of 9

Textbook Solutions to Exercises

Chapter 6: Repetition - Section 6.6

Textbook Solutions to Exercises 1 - 10


Chapter 6: Repetition - Section 6.6

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.

% The "06-01a Solution” Program


var count : int := 5
loop
put count
count:= count + 5
end loop

Revise this program to output the integers starting at 5 and decreasing by 10s.

% The "06-01 b Solution" Program


var count: int := 5
loop
put count
count := count - 10
end loop
Textbook Solutions to Exercises
Chapter 6: Repetition - Section 6.6

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:

Write a program that endlessly tells you to "Have a good day".

% The "06-02a Solution” Program


loop
put "Have a nice day"
end loop

Change it so that it is a program to wish you a good day only six times.

% The "06-02b Solution” Program


for i: 1 .. 6
put "Have a nice day"
end for

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:

% The "06-03 Solution” Program


var word: string
var count: int := 0
loop
put "Enter a word:"..
get word
exit when word = "end"
count := count+ 1
end loop
put count," words were read before 'end'."

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:

% The "06-04 Solution” Program


var numberOfMarks, mark: int
var sum : int := 0
assert may be new to you. You can
put "How many marks are there:" .. probably see what it does, but check
get numberOfMarks what Turing’s Help says at Help, Turing
assert numberOfMarks > 0 Reference, Search, assert.
for i: 1 .. numberOfMarks
put "Enter a mark:" .. Here we use a variable
get mark holding input from the user
as the number of passes
sum := sum + mark through a counted loop.
end for
put "The average mark is ", sum / numberOfMarks: 4 : 1
Textbook Solutions to Exercises
Chapter 6: Repetition - Section 6.6
6-5. Write a program that announces at each repetition of a loop the number of times it
has executed the loop. Have it stop at each execution with the message
Type 'more' to continue

A sample Execution window might be

Loop execution number 1


Type 'more' to continue
more
Loop execution number 2
Type 'more' to continue
more
Loop execution number 3
Type 'more' to continue
stop

SOLUTION:

% The "06-05 Solution” Program


var count: int := 0
loop
count := count+ 1
put "Loop execution number", count
put "Type 'more' to continue" To make the
var ans: string program pause,
simply ask for input
get ans from the user.
exit when ans not= "more"
end loop
Textbook Solutions to Exercises
Chapter 6: Repetition - Section 6.6

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:

% The "06-06a1 Solution” Program


put "Number Square"
Formatting is done with spaces for
var val :int := 1 first line of output (the titles) and
loop fields for the numbers. Experiment
to make it look right.
put val: 3, val** 2: 8
val := val + 1
end loop

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).

% The '06-06a2 Solution” Program


put "Number Square”
for val: 1 .. 100
put val: 3, val** 2 :8
end for
Textbook Solutions to Exercises
Chapter 6: Repetition - Section 6.6
6-6.b Modify your program so that in one for loop you output the following

Num Square Num Square Num Square Num Square


1 1 21 441 41 1681 61 3721
2 4 22 484 42 1764 62 3864

Continue through to

20 400 40 1600 60 3600 80 6400

SOLUTION:

% The "06-06b Solution” Program


put "Num Square Num Square Num Square Num Square"
for val: 1 .. 20
put val: 3, val** 2 : 7, val + 20 : 7, (val+ 20) ** 2 : 7,
val + 4 0 : 7 , (val+40)** 2 : 7,
val + 60 :7, (val+60)** 2 :7
end for

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:

% The "06-07 Solution” Program


for decreasing countdown : 5 .. 1
Remember, the
put countdown : 2 .. index of a counted
end for loop can be used for
output.
Textbook Solutions to Exercises
Chapter 6: Repetition - Section 6.6

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:

What number do I stop at? 82


Stop when count less than 82
100
95
90
85

SOLUTION:
Write a program to output a backwards count by 5s from 100 down to 5.

% The "06-08a Solution" Program


const startValue := 100
const minStopValue := 5
const decrement := 5
var counter: int := startValue
var stopValue : int := minStopValue
loop
exit when counter < stopValue
put counter: 3
counter := counter - decrement
end loop

Modify it so that you count from 100 down to 50.

% The "06-08b Solution" Program


const startValue := 100
const minStopValue := 50
const decrement := 5
var counter: int := startValue
var stopValue : int := minStopValue
loop
exit when counter < stopValue
put counter: 3
counter := counter - decrement
end loop
Textbook Solutions to Exercises
Chapter 6: Repetition - Section 6.6
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.

% The "06-08c Solution" Program


const startValue := 100
const minStopValue := 50
const decrement := 5
var counter: int := startValue
var stopValue : int := minStopValue
put "What number do I stop at? "..
get stopValue
put "Stop when count less than ", stopValue
assert stopValue <= startValue and stopValue >= minStopValue
loop
exit when counter < stopValue
put counter: 3
counter := counter- decrement
end loop

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

Aside: if x is between 0 and 1, this series sums to l/(l-x) in the limit.


Textbook Solutions to Exercises
Chapter 6: Repetition - Section 6.6

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

You might also like