1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66
|
#include <iostream>
#include <fstream>
#include <iomanip>
#include <string>
const int MAX = 10;
using namespace std;
void readMatrix(double array[][MAX], int & cols, int & rows, string filename);
void showMatrix(double array[][MAX], int cols, int rows);
int main()
{
double A[MAX][MAX], B[MAX][MAX];
int ax, ay;
readMatrix(A, ax, ay, "test1.txt");
int bx, by;
readMatrix(B, bx, by, "test2.txt");
cout << setw(26) << "Matrix A" << endl;
showMatrix(A, ax, ay);
cout << "\n\n";
cout << setw(26) << "Matrix B" << endl;
showMatrix(B, bx, by);
cin.get();
}
void readMatrix(double array[][MAX], int & cols, int & rows, string filename)
{
cols = rows = 0; // set default value.
ifstream fin(filename);
if (!fin)
{
cout << "could not open input file: " << filename << endl;
return;
}
fin >> cols >> rows;
for (int i=0; i<rows; i++)
{
for (int j=0; j<cols; j++)
{
fin >> array[i][j];
}
}
}
void showMatrix(double array[][MAX], int cols, int rows)
{
for (int i=0; i<rows; i++)
{
for (int j=0; j<cols; j++)
{
cout << setw(16) << array[i][j];
}
cout << endl;
}
}
|