home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
C Programming Starter Kit 2.0
/
SamsPublishing-CProgrammingStarterKit-v2.0-Win31.iso
/
tybc4
/
mat1.cpp
< prev
next >
Wrap
C/C++ Source or Header
|
1993-03-25
|
1KB
|
53 lines
/*
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 = 10;
const int MAX_ROW = 30;
main()
{
double x[MAX_ROW][MAX_COL];
double sum, sumx, mean;
int rows, columns;
// get the number of rows
do {
cout << "Enter number of rows [2 to "
<< MAX_ROW << "] : ";
cin >> rows;
} while (rows < 2 || rows > MAX_ROW);
// get the number of columns
do {
cout << "Enter number of columns [1 to "
<< MAX_COL << "] : ";
cin >> columns;
} while (columns < 1 || columns > MAX_COL);
// get the matrix elements
for (int i = 0; i < rows; i++) {
for (int j = 0; j < columns; j++) {
cout << "X[" << i << "][" << j << "] : ";
cin >> x[i][j];
}
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;
}