home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
C Programming Starter Kit 2.0
/
SamsPublishing-CProgrammingStarterKit-v2.0-Win31.iso
/
tybc4
/
mat3.cpp
< prev
next >
Wrap
C/C++ Source or Header
|
1993-03-25
|
2KB
|
78 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;
int getRows()
{
int n;
// get the number of rows
do {
cout << "Enter number of rows [2 to "
<< MAX_ROW << "] : ";
cin >> n;
} while (n < 2 || n > MAX_ROW);
return n;
}
int getColumns()
{
int n;
// get the number of columns
do {
cout << "Enter number of columns [1 to "
<< MAX_COL << "] : ";
cin >> n;
} while (n < 1 || n > MAX_COL);
return n;
}
void inputMatrix(double mat[][MAX_COL],
int rows, int columns)
{
// get the matrix elements
for (int i = 0; i < rows; i++) {
for (int j = 0; j < columns; j++) {
cout << "X[" << i << "][" << j << "] : ";
cin >> mat[i][j];
}
cout << "\n";
}
}
void showColumnAverage(double mat[][MAX_COL],
int rows, int columns)
{
double sum, sumx, mean;
sum = rows;
// obtain the sum of each column
for (int j = 0; j < columns; j++) {
// initialize summations
sumx = 0.0;
for (int i = 0; i < rows; i++)
sumx += mat[i][j];
mean = sumx / sum;
cout << "Mean for column " << j
<< " = " << mean << "\n";
}
}
main()
{
double x[MAX_ROW][MAX_COL];
int rows, columns;
// get matrix dimensions
rows = getRows();
columns = getColumns();
// get matrix data
inputMatrix(x, rows, columns);
// show results
showColumnAverage(x, rows, columns);
return 0;
}