turing, chapter 6: repetition – solutions to chapter questions

22
Turing, Chapter 6: Repetition – Solutions to Chapter Questions 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. Hints: Everyone should be able to do this question. or 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. Hints: Everyone should be able to do this question.

Upload: api-26077977

Post on 18-Nov-2014

3.551 views

Category:

Documents


4 download

TRANSCRIPT

Page 1: Turing, Chapter 6: Repetition – Solutions to Chapter Questions

Turing, Chapter 6: Repetition – Solutions to Chapter Questions

6-1. Write a program containing an infinite loop which outputs the series of integersstarting at 5 and going up by 5s. Revise this program to output the integers starting at 5 and decreasing by 10s.

Hints: Everyone should be able to do this question.

or

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.

Hints: Everyone should be able to do this question.

Page 2: Turing, Chapter 6: Repetition – Solutions to Chapter Questions

Turing, Chapter 6: Repetition – Solutions to Chapter Questions

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.

Hint #1: Counting the number of times through a loop. In the case of an infinite or conditional loop, declare a variable outside the loop, initialize it to 0, then increase it by 1 each time it goes through the loop( e.g. number := number + 1). In the case of a counted loop, you can use the index of the loop itself (the i in for i: x..y).

Hint #2: : If your count is off by one (e.g. you enter 6 words and the count shows only 5). Remember, when outputting the count, it makes a big difference whether you have increased it by 1 before the output statement or after.

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 passing through the loop only once.

Page 3: Turing, Chapter 6: Repetition – Solutions to Chapter Questions

Turing, Chapter 6: Repetition – Solutions to Chapter Questions

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.

Hints: Everyone should be able to do this question. Notice that you can use a variable in a counted loop, as in this example for i : 1 .. numberofMarks.

The statement assert numberofMarks > 0 is not really necessary at this stage. It is something new, but you can probably see what it means. We will use it later again in the year.

Page 4: Turing, Chapter 6: Repetition – Solutions to Chapter Questions

Turing, Chapter 6: Repetition – Solutions to Chapter Questions

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 beLoop execution number 1 Type 'more' to continuemoreLoop execution number 2Type 'more' to continuemoreLoop execution number 3Type 'more' to continuestop

Hints: Everyone should be able to do this question. Note that get always causes the program to pause as it waits for input from the user.

Page 5: Turing, Chapter 6: Repetition – Solutions to Chapter Questions

Turing, Chapter 6: Repetition – Solutions to Chapter Questions

6-6a. 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 Square1 12 43 94 165 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.

Hint #1: Formatting the output. Use field sizes to format the output. Be prepared to try several times, making adjustments, to get neatly arranged columns. Remember, letters and numbers align differently in their fields.

As the numbers get larger, the number of digits in the SOLUTION increase. However, Turing has an upper limit on integer values, and when 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).

6-6b. Modify your program so that in one for loop you output the following (see textbook for output).

Hint: Remember, output is done one LINE at a time. The example in the question makes some students make the mistake of thinking they need to output the first two columns, then go to the top and do the next two columns, and so on.

Page 6: Turing, Chapter 6: Repetition – Solutions to Chapter Questions

Turing, Chapter 6: Repetition – Solutions to Chapter Questions

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

Hints: Everyone should be able to do this question. Notice that the index of a counted loop can be used for output.

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 (see textbook for output):

Hints: Everyone should be able to do this question.

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

Page 7: Turing, Chapter 6: Repetition – Solutions to Chapter Questions

Turing, Chapter 6: Repetition – Solutions to Chapter Questions

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.

Page 8: Turing, Chapter 6: Repetition – Solutions to Chapter Questions

Turing, Chapter 6: Repetition – Solutions to Chapter Questions

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.

Hints: Optional. Challenging. Hint #1: The key to this solution is in the counted loop. Let’s name the index of the loop “term” (as in for term : ). We can use the index to calculate the exponented x, as in x ** term.

Hint #2: Notice this interesting fact. If we begin the counted loop at 0, then x ** 0 = 1, and x **1 = x, which are exactly what we want for our first two terms. Beautiful!

Hint #3: Be careful that your x ** term does not go too far. For example, x ** 2 is actually the THIRD time through the loop. So we can’t go through the loop a complete n times. In summary, we use a counted loop like this: for term: 0 .. n- 1 sum := sum + x ** term end for

Starting the counted loop with 0 solves a tricky problem that you might have run into when you tried this exercise. Why is it used here?

Page 9: Turing, Chapter 6: Repetition – Solutions to Chapter Questions

Turing, Chapter 6: Repetition – Solutions to Chapter Questions

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.

Hints: Everyone should be able to do this question. There are several calculations repeated for several years but nothing tricky.

or (see next page)

Page 10: Turing, Chapter 6: Repetition – Solutions to Chapter Questions

Turing, Chapter 6: Repetition – Solutions to Chapter Questions

Page 11: Turing, Chapter 6: Repetition – Solutions to Chapter Questions

Turing, Chapter 6: Repetition – Solutions to Chapter Questions

6-11a. A home owner takes out a mortgage for $120,000 at 7.75% per year. At the end of each year an amount of $24,000 is paid. Write a program to show how the mortgage is paid off, year by year, until nothing is owing.

Hints: Everyone should be able to do this question. There are several calculations repeated for several years but nothing tricky.

or

Page 12: Turing, Chapter 6: Repetition – Solutions to Chapter Questions

Turing, Chapter 6: Repetition – Solutions to Chapter Questions

6-11b. Assume that each month in the year has 30.5 days in it. Give the number of the month and the day in the month in which the mortgage is paid. (i.e. month #3, day #20)NOTE: This program assumes that you know the amount owing prior to starting the last year of payments remaining on the mortgage.

Hints: Not as difficult as it may at first seem if you remember that this program assumes that you know the amount owing prior to starting the last year of payments remaining on the mortgage ($12,840.00). See previous question.

Use round to avoid a fraction of a day.

Page 13: Turing, Chapter 6: Repetition – Solutions to Chapter Questions

Turing, Chapter 6: Repetition – Solutions to Chapter Questions

6-11. Write a program to simulate the playing of a simple dice game (played with one die). Roll the die to get a value from 1 to 6. This we will call your point. Now keep rolling until you get the same value (your point) again and see how many rolls it takes. Program it so you can play this game repeatedly.

Hints: Everyone should be able to do this question.

Note: count is assigned its initial value of 0 inside the loop, so that each replay will begin the count over again.

Page 14: Turing, Chapter 6: Repetition – Solutions to Chapter Questions

Turing, Chapter 6: Repetition – Solutions to Chapter Questions

6-13. Ask the user for an integer between 1 and 50. Output all the factors of that integer.Next, modify the program so that it outputs the factors of each integer up to the value of the integer input by the user.

Hint #1: Requires nested loops (that is, loop inside a loop).

Hint #2: What is a factor? Divide one integer by another. If there is no remainder, the second is a factor of the first.

Hint #3: Use div (check Turing Help if you do not know it).

Note: The key line of the program is the exit when statement. Can you explain what it means? Look up div in Turing Help.

or (see next page)

Page 15: Turing, Chapter 6: Repetition – Solutions to Chapter Questions

Turing, Chapter 6: Repetition – Solutions to Chapter Questions

Page 16: Turing, Chapter 6: Repetition – Solutions to Chapter Questions

Turing, Chapter 6: Repetition – Solutions to Chapter Questions

6-14. Ask the user for an integer. Output the number of digits in the integer. Then output the sum of the digits.(i.e. 1234 has 4 digits and their sum is 10).

Optional. Challenging. Limit the user to a digit of no more than 6 digits

Hint #1: Hint: Repeatedly change the given integer to a new number using div.

Page 17: Turing, Chapter 6: Repetition – Solutions to Chapter Questions

Turing, Chapter 6: Repetition – Solutions to Chapter Questions

6-15. Write a program to keep inputting integers until a perfect square (for example 64) between 40 and 100 is entered. (This is a difficult one!)Note: A perfect square is a number that has a whole number square root. For example, 25 is a perfect square, because the square root of 25 is 5, a whole number.

Hints: Optional. Challenging

Hint#1: A perfect square is a number that has a whole number square root. For example, 25 is a perfect square, because the square root of 25 is 5, a whole number.

Hint #2: Use nested loops (loop inside a loop).

6-16. Write a program to generate 10 random real numbers between:a. 4 and 5b. 0 and 10c. 20 and 30d. x and y where x and y are integer inputs.

Hint: Everyone should be able to do this question.

Page 18: Turing, Chapter 6: Repetition – Solutions to Chapter Questions

Turing, Chapter 6: Repetition – Solutions to Chapter Questions