Chapter 4 - Loops and looping – Try these problems if you’ve finished everything else.

1. Write and test a program that inputs a list of 10 numbers. The program should then print the sum and average of the numbers. Make sure that you write your program so that the source code can be easily modified to cope with 5, 10 or 15 numbers.  If set up correctly with loops, you will not need 10 variables to store the 10 numbers.

2. Write and test a program that inputs the temperature of a water cooling system. Inputs should be repeated until three successive inputs are greater than 100oC or a single input is greater than 108oC. If this occurs then the program should print

Danger cooling system malfunction.

3. At the start of 1989, the city of Gonbust had a population of 100,000 but this is falling by 9% each year. At the same time, the city of Nuplace had a population of 30,000 but this is increasing by 7% each year. Write and test a program that calculates the number of years that will pass before the population of Nuplace is greater than the population of Gonbust.

 

4. The answer to a division sum can be found with a series of simple subtractions. For example, to calculate 7/3

7

- 3

---

4

- 3

---

1 so 7/3 = 2 remainder 1

Write and test a program that inputs two numbers and use this technique to find the answer and the remainder.

5. The charge for a candy bar in a vending machine is $.85. The machine accepts quarters, dimes, or nickels.  As the coins are added the display changes to show the amount outstanding. When the amount entered is greater or equal to $.85, the candy bar is dispensed and the correct change is given. Write and test a program that simulates the running of the vending machine. The value of the coins entered should be typed in and checked, the value left to pay should be displayed and the message

Thank You

should be output when more than $.85 has been added. Finally the correct change should be given

6. Write and test a program that inputs a list of numbers, ending with the rogue value -1. The program should then print:

You entered ________ numbers.

The greatest was _________ .

The smallest was _________ .

The average of the numbers was ___________ . 

(Note:  You need the IF statement to complete this program).

7. Find out how long it takes for the computer to count to 100000. To do this you will need to use the TIMER function, this returns the number of seconds elapsed since midnight.

8. A farmer has 100 one-yard fencing panels. He wishes to build a rectangular enclosure with the panels, using a hedge as one side.

The length of the two equal sides of the enclosure can be any integer from 0 to 50 yards. Write and test a program to print out all the possible areas (in yards squared) that the farmer can enclose. The program should also print out the values that produce the maximum area.

9. A shopkeeper is currently selling a chocolate bar for $.85. The bars cost him $.50 each and he sells 200 each week. He estimates that for every $.05 increase he puts on the cost of a bar, he will sell 25 bars less each week. Write and test a program that prints the profit the shopkeeper will make from the chocolate bars each week, as the price increases from $.85 to $1.25 in steps of $.05.

 

Source:  Problems taken from http://www.colchsfc.ac.uk/computing/manuals/qbasic/QB04.htm