The complete program is given below:
This program could have been done
without using an array.
But by using an array,
the data is more organized.
The data (the inches of rain each day) are not three unrelated numbers,
but they belong to a collection.
It is nice to give the collection a name ("RAIN
") and to use a subscript
to get individual items from it.
In long programs,
or in programs that deal with large data sets,
such organization is the only way to keep things straight.
DIM RAIN(1 TO 3) ' The array will store the rainfall ' ' Collect data from the user PRINT "Enter rainfall for day 1:" INPUT RAIN(1) PRINT "Enter rainfall for day 2:" INPUT RAIN(2) PRINT "Enter rainfall for day 3:" INPUT RAIN(3) LET SUM = RAIN(1) + RAIN(2) + RAIN(3) ' Add up the rainfall for all three days LET AVG = SUM / 3 PRINT "The average rainfall was:", AVG, " inches" END
Here is a sample run of the program:
Enter rainfall for day 1: ? 1 Enter rainfall for day 2: ? 2 Enter rainfall for day 3: ? 3 The average rainfall was: 2 inches
Can this program be used without modification for a number of days other than three?