home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
C Programming Starter Kit 2.0
/
SamsPublishing-CProgrammingStarterKit-v2.0-Win31.iso
/
tybc4
/
mat2.cpp
< prev
next >
Wrap
C/C++ Source or Header
|
1993-03-25
|
1KB
|
46 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 = 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;
}