/* * Write a program called average2d.cc that: * * 1. Reads in a 4 x 4 matrix of integers from the user * 2. Computes the average of each column in the matrix * 3. Displays the average of each column in the matrix * * For example: * * Enter row 0 * 1 2 3 4 * Enter row 1 * 5 6 7 8 * Enter row 2 * 9 8 7 6 * Enter row 3 * 5 4 3 2 * Averages: * 5 5 5 5 */ #include using namespace std; const int N = 4; int main() { float numbers[N][N]; float avgcol[N] = {0, 0, 0, 0}; int i, j; // Get the numbers for (i = 0; i < N; i++) { cout << "Enter row " << i << endl; for (j = 0; j < N; j++) { cin >> numbers[i][j]; } } // Compute the averages for (i = 0; i < N; i++) { for (j = 0; j < N; j++) { avgcol[i] += numbers[j][i]; } avgcol[i] /= N; } // Display the averages cout << "Averages:" << endl; for (i = 0; i < N; i++) { cout << avgcol[i] << " "; } cout << endl; return (0); }