Each student in a class of 30 students takes 2 tests in which scores range between 0 and 100. Suppose the test scores are stored in a 30 X 2 array TEST. Write a program that should output the average score for both tests. Your program must consist of the following functions:
(a) getData()that reads and stores data in the two dimensional array.
(b) averageTest1() that calculates and returns the average score for Test1.
(c) averageTest2() that calculates and returns the average score for Test2.
(d) display() that output the average score Test1 and Test2.
(These functions must all have the appropriate parameters.)
1
Expert's answer
2012-03-02T07:54:42-0500
#include <iostream>
using namespace std;
void getValue(int ** arr, int m, int n){ cout<<"Input marks."<<endl; int i=0, j; while(i < m){ j=0; while(j < n){ cout<<"Student "<<i+1<<", test"<<j+1<<endl; cin>>arr[i][j]; if((arr[i][j] >= 0) && (arr[i][j] <= 100)){ j++; } else { cout<<"Wrong value! Scores range should be between 0 and 100."<<endl; } } i++; } }
double averageTest1(int ** array, int m){ double tmp = 0;
for(int i=0; i < m ; i++){ tmp += array[i][0]; }
return tmp / m; }
double averageTest2(int ** arr, int m){ double tmp = 0;
for(int i=0; i < m ; i++){ tmp += arr[i][1]; } return tmp / m; } void display(int ** arr, int m){ cout<<"The first test average: "<<averageTest1(arr,m)<<endl; cout<<"The second test average: "<<averageTest2(arr,m)<<endl; }
int main(){ int nStudents = 30; int nTests = 2; //create 2dimetnion array int ** test = new int* [nStudents]; for(int i=0; i<nStudents; i++){ test[i] = new int [nTests]; } getValue(test, nStudents, nTests); display(test, nStudents);
system("pause");
//remove it from memory for(int i=0; i<nStudents; i++){ delete[] test[i] ; } delete [] test;
Comments
Leave a comment