10K views

Uploaded by api-26077977

- Math Fun Facts
- Plumbing Mathematics
- Textbook Solutions to Exercises Chapter 6: Repetition - Section 6.6
- Introduction to Programming in Turing Chapter 5: Variables and Constants
- 2.Algebra.docx
- Math 1
- The Ten Most Common Mistakes in Scripting as Seen From User Support
- Chapter 6: Repetition — Extra Practice
- 7th Grade Translations, Set 3 and KEY
- Final Term Syllabus
- null
- Review of GMAT Quant Topics
- null
- Rational and Irrational Numbers
- 2005NYSMathStandards HoltMcDougalCourse2.Standards
- Control Structure
- School Students
- cs50
- TG NS teasers for CAT-2012.pdf
- Solution_to_POW_Math_IX_Ist_term.pdf

You are on page 1of 18

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.

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.

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.

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.

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

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.

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

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.

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

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.

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.

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?

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.

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

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.

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.

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

begin the count over again.

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.

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

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

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.

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.

a. 4 and 5

b. 0 and 10

c. 20 and 30

d. x and y where x and y are integer inputs.

Turing, Chapter 6: Repetition – Solutions to Chapter Questions

- Math Fun FactsUploaded byUjjwal Sharma
- Plumbing MathematicsUploaded byben
- Textbook Solutions to Exercises Chapter 6: Repetition - Section 6.6Uploaded byapi-26077977
- Introduction to Programming in Turing Chapter 5: Variables and ConstantsUploaded byapi-26077977
- 2.Algebra.docxUploaded byJane
- Math 1Uploaded byCarlito Doringo
- The Ten Most Common Mistakes in Scripting as Seen From User SupportUploaded bycitilabs
- Chapter 6: Repetition — Extra PracticeUploaded byapi-26077977
- 7th Grade Translations, Set 3 and KEYUploaded byhkaruza
- Final Term SyllabusUploaded bySabih Siddiqi
- nullUploaded byapi-26077977
- Review of GMAT Quant TopicsUploaded bymicore8
- nullUploaded byapi-26077977
- Rational and Irrational NumbersUploaded bynishagoyal
- 2005NYSMathStandards HoltMcDougalCourse2.StandardsUploaded byJoel Paulino
- Control StructureUploaded bypankajusb
- School StudentsUploaded bySekar
- cs50Uploaded byAdi
- TG NS teasers for CAT-2012.pdfUploaded byParijat Roy
- Solution_to_POW_Math_IX_Ist_term.pdfUploaded byWow
- JAVA Assignment.docxUploaded byJacquelina Stanley
- UntitledUploaded byapi-127466285
- Dear Reader, Let Us Explore the Traditional Point of ViewUploaded byDoronShadmi
- Copy of Catalogue of ResourcesUploaded bynettie223
- Scientific MethodUploaded bymayeth
- 0008 - Programming Part 2Uploaded byAnimesh Ghosh
- [Natasha Maurits, Branislava Ćurčić-Blake] Math(B-ok.xyz)Uploaded byDiego Evaristo
- BCA - SEM - 1Uploaded bylogicballia
- DIFFERENTIAL CALCULUS NOTES.pdfUploaded byWycliff
- kuliah-realnumbersystemnotesUploaded byRizwan Hanafie

- nullUploaded byapi-26077977
- nullUploaded byapi-26077977
- nullUploaded byapi-26077977
- nullUploaded byapi-26077977
- nullUploaded byapi-26077977
- nullUploaded byapi-26077977
- nullUploaded byapi-26077977
- nullUploaded byapi-26077977
- nullUploaded byapi-26077977
- nullUploaded byapi-26077977
- nullUploaded byapi-26077977
- nullUploaded byapi-26077977
- nullUploaded byapi-26077977
- nullUploaded byapi-26077977
- nullUploaded byapi-26077977
- BTA3O1 – Productivity Software – Unit 2:Uploaded byapi-26077977
- nullUploaded byapi-26077977
- Exercise 2 AUploaded byapi-26077977
- Exercise 11Uploaded byapi-26077977
- BTA3O1 – Productivity Software – Unit 2:Uploaded byapi-26077977
- Creating Your Own SpreadsheetUploaded byapi-26077977
- Exercise 12Uploaded byapi-26077977
- Page 1 of 13Uploaded byapi-26077977
- BTA3O1 – Productivity Software – Unit 2:Uploaded byapi-26077977
- The Jarvis Business Department Held a Charity Bowling Event LastUploaded byapi-26077977
- Exercise 10 Creating Your Own SpreadsheetUploaded byapi-26077977
- BTA3O1 – Productivity Software – Unit 2:Uploaded byapi-26077977
- EXERCISE 2 b Average Function Cineplex Odeon Theatres Wants YouUploaded byapi-26077977
- BTA3O1 – Productivity Software – Unit 2: Spreadsheet – PartUploaded byapi-26077977
- Your Teacher Has Asked You to Create a Spreadsheet ThatUploaded byapi-26077977

- EMC-1Uploaded byAlexander Ong
- 10v4-online-security-of.pdfUploaded byFernando Da Costa Correa
- SR530C8.pdfUploaded byUltisolar
- Lecture 2Uploaded byAdnan Anwer
- 04808203Uploaded byPursito
- A1301-2-DatasheetUploaded byJaime Lazo
- PM32xx_UserManualUploaded byMary Harrison
- 1045 Medium Tensile Carbon Steel Bar DatasheetUploaded bySamuel Freitas
- Social Impact of MoonUploaded byEvandro
- Analysis of Soil Nailing under Earthquake Loading in Malaysia using.pdfUploaded byAriffin Ngah
- Pc 2 Pc Laser Communication SystemUploaded byMamun Limon
- report for lab 1 finalUploaded byapi-285757994
- ASME Technologist GDTP Certificate HoldersUploaded byShilpa Kumari
- chem energy webquestUploaded byapi-297581533
- Guidelines for Nemo Analyze Custom Queries October 2012Uploaded bySerkan Ozgan
- ESD Work Instruction WI-OPS-005Uploaded byNiv Narkiss
- Les03Uploaded byNiranjan Nageswara
- bsccbcsrs36.docUploaded byKalpana Jayakumar
- RecrystallizationUploaded byDini F Gunawan
- Holloman Air Force BaseUploaded byCAP History Library
- MFC-8510dn 2 Page BrochureUploaded byWilly Cif
- Hillesheim Catalogue EnUploaded byaldo2306
- KHX16C10B1BK2_16XUploaded byΒΙΒΗ ΓΚΑΣΙΩΝΗ
- FluidDynamics.pptUploaded byMoosa Salim Al Kharusi
- [slides] PCI-Express-Interface .pdfUploaded byJohn
- tgs_tgx_e2014_v2_enUploaded byFlorin Alexandru Vlad
- Test Case ExampleUploaded byjustingct
- Mass Balance.xlsUploaded byscrbdgharavi
- JF_V02_S02_L01_try.pdfUploaded byYoga Botex
- Green Concrete in InidiaUploaded byIftsam