Unit 4.2 While Loop, reading in numbers and summing them

PROG

Read in a set of numbers off a data file
Print out sum
Last number in data file is "-1"
Do NOT include this in our SUM.

PED

To see our first while loop example in real PASCAL
To learn the plan to process a list of numbers off a data file

CONCEPT

Plan to process a set of things ending in a SENTINEL

get first data item into variable
while( data item not the sentinel) do begin
process the data item
get next data item into variable
end;

SENTINEL -- data item indicating end of data, must not be something that could appear as regular data

DO LOOP Invaraint

SUM contains the sum of all the data except the value in NUMBER.



Unit 4.2 Reading in Numbers and Summing Them

This unit is your first program involving a LOOP in real PASCAL. In addition, you will learn a very important plan that you will use again and again, in this course, in the next courses, and in your professional career. That is reading in a set of data from a file and doing something to each number. In this example, we will sum all the numbers and at the end, print out the sum.

This comes up a lot of time in programming. Each line could be a record of an individual needing an invoice. Or each line could be a description of a star that requires some computing in an astronomical application.

The program must be able to tell when the last data is read. The easiest way to do this is to include a data item at the end of the file which we can guarantee won't be a regular data item. This is called the sentinel. It can be a special string if we are reading in strings. In the case of numeric values, it can be a nonsense value. If I expect only positive numbers, a negative number makes a good sentinel.

Our program reads in a list of numbers, and prints out the sum of all the valid data; that is, it excludes the sentinel.

If the data were

1
4
7
-1

The sum would be 12. -1 is not included in the sum.

The first time through the while loop, the SUM will be zero and number would contain the first data item, "1". Note that SUM does not include the first data item in "number." The sum of the numbers in the empty set is zero.

The next time we pass the "w" in the while loop, SUM would be one and number would contain "4" Notice that SUM is the sum of all the numbers before the 4, which is one.

When we next pass the "w" in the while loop, the SUM would contain the value, 5. Number would contain "7" SUM is the sum of all numbers before 7.

The last time we pass the "w" in the while loop, SUM contains 12 and number contains "-1" Notice that SUM doesn't include the -1 and is the sum of all the proceeding numbers. This is precisely the situation we want at the end of the do loop. Notice that the condition in the while finds the -1 in number and leaves so our program does work correctly.

Now let's think of this as a do loop invariant problem. The do-loop invariant is

SUM contains the sum of all numbers before the current value in NUMBER.
Let's check our four requirements for a working do loop program

Check 1: that the do loop invariant and the condition being true implies that executing the statements leaves the do loop invariant true

Our two statements are
SUM:=SUM+NUMBER
READ(NUMBER)

After the first number, SUM includes the value in NUMBER. When I read in NUMBER, we will again have the situation of SUM not including the current value in NUMBER.

Note that if NUMBER is not -1, that means there will be a NUMBER for the READ to read.

Check 2: that the do loop invariant and the condition not being true implies the POSTCONDITION for the loop

In this case, we have from the do loop invariant that SUM is the SUM of all values except the current one. If NUMBER<>-1 is not true, that means NUMBER is equal to -1. That means that we have the sum of all numbers except the -1 in variable, SUM.

Check 3: that the do loop invariant is initially true. We did that by reading in NUMBER but not including that value in the SUM.

Check 4: that executing the statements once makes progress towards the goal. Since each read statement removes a number from the file and advances us toward the end of what must be a finite set, then the statement makes progress.