Information Technology Topic

Algorithms for Totals and Averages

  1. Read notes
  2. Flashcards
  3. Quiz
  4. Confident

Study focus - 3 things

  1. To total a set of values, set a variable called total to 0 before the loop, then on every pass read a value and store total + value in total
  2. To count values, set a counter to 0 and add 1 on every pass; to find the average, divide the total by the count AFTER the loop has finished
  3. Use a For loop when the number of values is known, and a While loop with a stop value when it is not; check for a count of 0 before dividing

Section 1The running total idea

A total is built up one value at a time. Think of a cashier in a Kingston supermarket adding each item to the bill as it is scanned. The bill starts at zero, and each scan makes it bigger. In an algorithm, a variable called total plays the part of the bill. It must be given a starting value of 0 before any value arrives. Then, for every value, you store the old total plus the new value back into total. The key statement is Store total + value in total. It reads as: take what total holds now, add the new value, and put the answer back in total.

Section 2A total with a For loop

When you know how many values there are, use a For loop. This program totals four daily sales, then divides by 4 to get the average: Store 0 in total For i from 1 to 4 Read sale Store total + sale in total End For Store total / 4 in average Display total Display average The loop body runs exactly 4 times. Each pass reads one value into sale and adds it to total. Only the lines indented under For are repeated. The lines Store 0 in total (before the loop) and the divide and Display lines (after the loop) run once. Here's the trick: 'before the loop, set up; inside the loop, read and add; after the loop, finish and display'.

Section 3Counting the values

A counter is a variable that goes up by 1 each time a value is processed. Like total, it starts at 0. The statement is Store count + 1 in count. Why bother, when a For loop already knows it runs 4 times? Because with a While loop nobody tells the program how many values will arrive. The counter finds out, and the average needs that number. Counting here means counting the values that were read. Counting only the values that pass a test would need an If inside the loop, and the syllabus pseudocode does not nest one statement inside another.

Section 4Finding the average

The average of a set of values is their total divided by how many there are. In pseudocode that is Store total / count in average, or you can Display total / count directly. Watch out: do the division after the loop, once the total is complete. If the divide line is inside the loop, the program works out a running average after every value instead of one final answer. The / operator gives the exact quotient including decimals, so a total of 1625 over 4 values is 406.25.

Section 5When the number of values is not known

A shop does not know how many prices a clerk will enter, so a While loop is used with a stop value. The clerk types 0 when there are no more prices. The stop value (also called a sentinel) is not data and must not be added. Store 0 in total Store 0 in count Read price While price <> 0 Store total + price in total Store count + 1 in count Read price End While Display total Display count Display total / count The first price is read BEFORE the loop. The loop test price <> 0 checks it. Inside the loop, the value is added and counted and then the next one is read at the bottom. When 0 is read, the test fails and the loop ends without adding or counting the 0. Easy to mix up: Read before the loop, and Read again at the end of the loop body. If you only Read at the top of the body, the first value is lost.

Section 6Guarding against no values

If the very first value typed is the stop value, no values were entered. The count is then 0, and total / count means dividing by zero. That is a run-time error and the program stops. To prevent it, test the count before dividing: Store 0 in total Store 0 in count Read price While price <> 0 Store total + price in total Store count + 1 in count Read price End While If count > 0 then Display total / count Else Display "No values" End If The If-then-else sits after the loop, not inside it, so there is no nesting. With the first input 0, the loop never runs and the program displays No values.

Section 7Common faults to look for

Three faults cause most wrong answers. Putting Store 0 in total inside the loop wipes the total on every pass, so the program ends with only the last value. Leaving out Store 0 in total means the first addition has nothing to start from and the program fails. Dividing by the wrong number, such as 4 when 5 values were added, gives a wrong average. A fourth fault is the wrong order in a stop-value loop: reading inside the loop before adding means the first value is thrown away and the stop value is added. When you check an algorithm, run it by hand with a small set of values and compare with the answer you worked out first.

Examples

A tuck shop in Mandeville records its sales for four days: 1200, 850, 1400 and 950 dollars. The owner runs this program and enters the four sales in order. Store 0 in total For i from 1 to 4 Read sale Store total + sale in total End For Store total / 4 in average Display total Display average (a) Work out the value of total after each pass of the loop. (b) State what the program displays. (c) State how many times the body of the loop runs.

(a) Total starts at 0. After pass 1: 0 + 1200 = 1200. After pass 2: 1200 + 850 = 2050. After pass 3: 2050 + 1400 = 3450. After pass 4: 3450 + 950 = 4400. (b) The program first displays total, 4400. Then it divides 4400 by 4 and displays the average, 1100. (c) The loop runs 4 times, because For i from 1 to 4 counts i as 1, 2, 3 and 4. The divide line is outside the loop, so it runs once after the final value is added.

A bakery in Portmore enters the prices of cakes sold and ends with 0. The prices are 450, 300, 275, 600 and then 0. Here is the program: Store 0 in total Store 0 in count Read price While price <> 0 Store total + price in total Store count + 1 in count Read price End While Display total Display count Display total / count (a) Work out the value of total and count after each price is processed. (b) State what the program displays. (c) Explain what happens if the first value entered is 0, and rewrite the ending so it displays No values in that case.

(a) After 450: total 450, count 1. After 300: total 750, count 2. After 275: total 1025, count 3. After 600: total 1625, count 4. When 0 is read, the test price <> 0 is FALSE, so the loop ends without adding or counting it. (b) It displays 1625, then 4, then 1625 / 4 = 406.25. (c) If 0 is first, the loop body never runs, so count stays 0. The last line then divides by zero and the program stops with a run-time error. Replace the last three lines with a guard after End While: If count > 0 then Display total / count Else Display "No values" End If With 0 as the first input, the guarded program displays No values.

Now let's water it

Session runs notes to flashcards to quiz back-to-back.

Where this comes from