home *** CD-ROM | disk | FTP | other *** search
/ C Programming Starter Kit 2.0 / SamsPublishing-CProgrammingStarterKit-v2.0-Win31.iso / tybc4 / mat1.cpp < prev    next >
C/C++ Source or Header  |  1993-03-25  |  1KB  |  53 lines

  1. /*
  2.   C++ program that demonstrates the use of two-dimension arrays.
  3.   The average value of each matrix column is calculated.
  4. */
  5.  
  6. #include <iostream.h>
  7.  
  8. const int MAX_COL = 10;
  9. const int MAX_ROW = 30;
  10.  
  11. main()
  12. {
  13.     double x[MAX_ROW][MAX_COL];
  14.     double sum, sumx, mean;
  15.     int rows, columns;
  16.     
  17.     // get the number of rows
  18.     do {
  19.       cout << "Enter number of rows [2 to "
  20.            << MAX_ROW << "] : ";
  21.       cin >> rows;
  22.     } while (rows < 2 || rows > MAX_ROW);
  23.     
  24.     // get the number of columns
  25.     do {
  26.       cout << "Enter number of columns [1 to "
  27.            << MAX_COL << "] : ";
  28.       cin >> columns;
  29.     } while (columns < 1 || columns > MAX_COL);
  30.     
  31.     // get the matrix elements
  32.     for (int i = 0; i < rows; i++)  {
  33.       for (int j = 0; j < columns; j++)  {
  34.           cout << "X[" << i << "][" << j << "] : ";
  35.           cin >> x[i][j];
  36.       }
  37.       cout << "\n";
  38.     }
  39.  
  40.     sum = rows;
  41.     // obtain the sum of each column
  42.     for (int j = 0; j < columns; j++)  {
  43.       // initialize summations
  44.       sumx = 0.0;
  45.       for (i = 0; i < rows; i++)
  46.         sumx += x[i][j];
  47.       mean = sumx / sum;
  48.       cout << "Mean for column " << j
  49.            << " = " << mean << "\n";
  50.     }
  51.     return 0;
  52. }
  53.