/*************************************************************** * * This program is an example of some two dimensional array * operations. The array is declared to have 5 rows and 6 * columns. The values for 4 rows and 5 columns are input, * one row per line (ie., a rowwise operation), from standard * input. Then the 6th element in each row is defined to be * the average of the first 5 elements in each row (a rowwise * operation), and the 5th row is defined to be the average of * the first 4 elements in ach column (a columnwise operation). * Then the entire array is output, one row per line (a rowwise * operation. * ***************************************************************/ #include using namespace std; int main () { float arr[5][6]; float sum; int i, j; // Input the array (except for the last row and column) for(i = 0; i < 4; i++) cin >> arr[i][0] >> arr[i][1] >> arr[i][2] >> arr[i][3] >> arr[i][4]; // Note: we could also use a second (inner) loop to do this // Now calculate the sum of each row, then store the average in column 5. for(i = 0; i < 4; i++) { sum = 0.0; for(j = 0; j < 5; j++) sum += arr[i][j]; arr[i][5] = sum / 5.; } // END for i // Sum the columns, put average in row 5 // NOTE: We are adding up the last column (the average of the rows) for(j = 0; j < 6; j++) { sum = 0.0; for(i = 0; i < 4; i++) sum += arr[i][j]; arr[4][j] = sum / 4.; } // END for j // Now output the entire array, including averages. for(i = 0; i < 5; i++) { for(j = 0; j < 6; j++) cout << arr[i][j] << ' '; cout << endl; } // END for i } // END main