/* Program to process exam grades - illustrate use of EOF detection */ #include #include #include //needed to use exit() function using namespace std; const int SIZE = 50; /* Function Prototypes */ void readdata(int [], int &); int sumarray(int [], int); double avgarray(int [], int); int findmax(int [], int); void countmarks(int [], int, double, int &, int &, int &); int main() { int num; int mark[SIZE]; double avgmark; int numabove,numbelow,numequal,hi_mark; // call function to read the marks readdata(mark,num); // print the mark array cout << endl << endl << "The values of the array are:" << endl; for (int count = 0; count < num; count++) cout << "mark[" << count << "]=" << mark[count] << endl; // find and print the average mark avgmark = avgarray(mark,num); cout << endl << "The average is " << avgmark << endl; // find and print the highest mark hi_mark = findmax(mark,num); cout << "The highest mark is " << hi_mark << endl; // classify marks w.r.t. average mark countmarks(mark,num,avgmark,numbelow,numabove,numequal); cout << "Number of marks below the average is " << numbelow << endl; cout << "Number of marks above the average is " << numabove << endl; cout << "Number of marks equal to the average is " << numequal << endl; // system("pause"); //un-comment when using DevC++ return 0; } /* Function sumarray() * Input: * numbers - an array of integers * n - the number of elements in the array * Process: * finds the sum of the first n elements in the numbers array. * Output: * returns the sum to the calling function. */ int sumarray(int numbers[], int n) { int sum=0; for (int count = 0; count < n; count++) sum += numbers[count]; return(sum); } /* Function avgarray() * Input: * numbers - an array of integers * n - the number of elements in the array * Process: * calls sumarray to find the sum of the first n elements * and then divides by n to find the average. * Output: * returns the average to the calling function. */ double avgarray(int numbers[], int n) { return ((double)sumarray(numbers,n)/n); } /* Function readdata() * Input: * numbers - an array to be filled * n - a reference to the number of elements in the array * the parameters are uninitialized upon entry * Process: * reads values into the array until EOF encountered * and counts how many are read * Output: * the filled numbers array * n - the number of elements in the array */ void readdata(int numbers[], int &n) { //un-comment to redirect cin to a file //ifstream cin("c:\\bc\\CISC1110\\pgms\\chapter7\\myinput.txt"); // read and count number of marks n = 0; cout << "Enter the marks - end with EOF: " << endl; while (cin >> numbers[n]) n++; if (n > 0 && n <= SIZE) cout << endl << endl << "There are " << n << " marks" << endl; else { cout << "Invalid number of marks" << endl; // system("pause"); //un-comment when using DevC++ exit(1); } return; } /* Function findmax() * Input: * numbers - an array of integers * n - the number of elements in the array * Process: * finds the largest value in the array * Output: * returns the maximum value within the array */ int findmax(int numbers[], int n) { int largest_so_far; largest_so_far = numbers[0]; for (int count = 1; count < n; count++) if (largest_so_far < numbers[count]) largest_so_far = numbers[count]; return(largest_so_far); } /* Function countmarks() * Input: * numbers - an array of integers * n - the number of elements in the array * avgnum - the average value of the array elements * num_below - a reference to the number of elements of * the array that are below the average * num_above - a reference to the number of elements of * the array that are above the average * num_equal - a reference to the number of elements of * the array that equal to the average * Process: * counts the number of elements that are above, below, * and equal to avgnum. * Output: * num_below - the number of elements below average * num_above - the number of elements above average * num_equal - the number of elements equal to the average */ void countmarks(int numbers[], int n, double avgnum, int &num_below, int &num_above, int &num_equal) { num_below = num_above = num_equal = 0; //initialize the counters for (int count = 0; count < n; count++) if ((double)numbers[count] < avgnum) num_below++; else if ((double)numbers[count] > avgnum) num_above++; else num_equal++; return; }