home *** CD-ROM | disk | FTP | other *** search
- /*
- C++ program that demonstrates the use of two-dimension arrays.
- The average value of each matrix column is calculated.
- */
-
- #include <iostream.h>
-
- const int MAX_COL = 3;
- const int MAX_ROW = 3;
-
- main()
- {
- double x[MAX_ROW][MAX_COL] = {
- 1, 2, 3, // row # 1
- 4, 5, 6, // row # 2
- 7, 8, 9 // row # 3
- };
- double sum, sumx, mean;
- int rows = MAX_ROW, columns = MAX_COL;
-
- cout << "Matrix is:\n";
- // display the matrix elements
- for (int i = 0; i < rows; i++) {
- for (int j = 0; j < columns; j++) {
- cout.width(4);
- cout.precision(1);
- cout << x[i][j] << " ";
- }
- cout << "\n";
- }
- cout << "\n";
-
- sum = rows;
- // obtain the sum of each column
- for (int j = 0; j < columns; j++) {
- // initialize summations
- sumx = 0.0;
- for (i = 0; i < rows; i++)
- sumx += x[i][j];
- mean = sumx / sum;
- cout << "Mean for column " << j
- << " = " << mean << "\n";
- }
- return 0;
- }
-