// CMPSC 101 Class #14 Demo #1a #include #include using namespace std; int main() { int row = 0; int col = 0; int sum = 0; int scores[5][3] = {0}; int rowSum = 0; int colSum = 0; double teamAve = 0.0; double colAverages[3] = {0}; ifstream infile("C:/Temp/scores.txt"); // ****** computing team average ****************** for (row = 0; row < 5; row++) { for (col = 0; col < 3; col++) { //cout << "Bowler #" << row + 1 << ", what is the score to game #" << col + 1 << ' '; infile >> scores[row][col]; sum = sum + scores[row][col]; } } teamAve = double (sum) / 15; cout << "The team average is " << teamAve << endl; // ******* neatly displaying the 2D array ********************* for (row = 0; row < 5; row++) { cout << "Bowler #" << row + 1 << "'s scores\t"; for (col = 0; col < 3; col++) { cout << scores[row][col] << "\t"; } cout << endl; // moving cursor to next row } // ****** computing row averages ************** for (row = 0; row < 5; row++) { rowSum = 0; for (col = 0; col < 3; col++) { rowSum = rowSum + scores[row][col]; } cout << "The total of the scores in row " << row + 1 << " is " << rowSum << endl; cout << "The average of this row is " << rowSum / 3.0 << endl; } // ****** computing column averages ***************** for (col = 0; col < 3; col++) { colSum = 0; for (row = 0; row < 5; row++) { colSum = colSum + scores[row][col]; } cout << "The total of the scores in column " << col + 1 << " is " << colSum << endl; colAverages[col] = colSum / 5.0; cout << "The average of this column is " << colAverages[col] << endl; } return 0; }// end of main