/* Program to process exam grades and illustrate passing an array to a function */ #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 main() { int num; int mark[SIZE]; double avgmark; // 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; // 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 n and reads n values into the array * 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"); cout << "Enter the number of marks: "; cin >> n; if (n > 0 && n <= SIZE) cout << endl << "There are " << n << " marks" << endl << endl; else { cout << "Invalid number of marks" << endl; // system("pause"); //un-comment when using DevC++ exit(1); } // enter the marks for (int count = 0; count < n; count++) { cout << "Enter a mark: "; cin >> numbers[count]; } return; }