Next: Example Program: Test of
Up: Arrays
Previous: Initialisation of arrays
The requirement specification for a program is:
A set of positive data values (200) are available. It is required to find the average value of these values and to count the number of values that are more than 10% above the average value.
Since the data values are all positive a negative value can be used as a sentinel to signal the end of data entry. Obviously this is a problem in which an array must be used since the values must first be entered to find the average and then each value must be compared with this average. Hence the use of an array to store the entered values for later re-use.
An initial algorithmic description is:
initialise. enter elements into array and sum elements. evaluate average. scan array and count number greater than 10% above average. output results.This can be expanded to the complete algorithmic description:
set sum to zero. set count to zero. set nogt10 to zero. enter first value. while value is positive { put value in array element with index count. add value to sum. increment count. enter a value. } average = sum/count. for index taking values 0 to count-1 if array[index] greater than 1.1*average then increment nogt10. output average, count and nogt10.In the above the variable
nogt10
is the number greater than
10% above the average value. It is easy to argue that after exiting
the while
loop, count
is set to the number of positive
numbers entered. Before entering the loop count
is set to zero
and the first number is entered, that is count
is one less than
the number of numbers entered. Each time round the loop another number
is entered and count
is incremented hence count
remains
one less than the number of numbers entered. But the number of numbers
entered is one greater than the number of positive numbers so
count
is therefore equal to the number of positive numbers.
A main()
program written from the above algorithmic description
is given below:
void main() { const int NE = 200; // maximum no of elements in array float sum = 0.0; // accumulates sum int count = 0; // number of elements entered int nogt10 = 0; // counts no greater than 10% // above average float x; // holds each no as input float indata[NE]; // array to hold input float average; // average value of input values int i; // control variable // Data entry, accumulate sum and count // number of +ve numbers entered cout << "Enter numbers, -ve no to terminate: " << endl; cin >> x; while (x >= 0.0) { sum = sum + x; indata[count] = x; count = count + 1; cin >> x; } // calculate average average = sum/count; // Now compare input elements with average for (i = 0; i < count; i++) { if (indata[i] > 1.1 * average) nogt10++; } // Output results cout << "Number of values input is " << n; cout << endl << "Number more than 10% above average is " << nogt10 << endl; }
Download program.
Download sample data.
Since it was assumed in the specification that there would be less
than 200 values the array size is set at 200. In running the program
less than 200 elements may be entered, if n
elements where
n < 200
elements are entered then they will occupy the first
n
places in the array indata
. It is common to set an
array size to a value that is the maximum we think will occur in
practice, though often not all this space will be used.