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 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128
|
#include<iostream>
#include<cmath>
void intro()
{
using namespace std;
cout << "*****************************************************" << endl;
cout << "Program to compute the product of two matrices C=AB" << endl;
cout << "*****************************************************" << endl;
}
void get_A(int& rowsA, int& colsA)
{
using namespace std;
cout << "********************************" << endl;
cout << "Enter the dimensions of matrix A" << endl;
cout << "********************************" << endl;
cout << "Number of rows: ";
cin >> rowsA;
cout << "Number of columns: ";
cin >> colsA;
cout << "********************************" << endl;
double** A;
A = new double* [rowsA];
for(int i=0; i<rowsA; i++)
{
A[i] = new double [colsA];
}
for(int i=0; i<rowsA; i++)
{
for(int j=0; j<colsA; j++)
{
A[i][j]=0.0;
cout << "A[" << i << "," << j << "] = ";
cin >> A[i][j];
}
}
}
void get_B(int& rowsB, int& colsB)
{
using namespace std;
// double** B;
cout << "********************************" << endl;
cout << "Enter the dimensions of matrix B" << endl;
cout << "********************************" << endl;
cout << "Number of rows: ";
cin >> rowsB;
cout << "Number of columns: ";
cin >> colsB;
cout << "********************************" << endl;
double** B;
B = new double* [rowsB];
for(int i=0; i<rowsB; i++)
{
B[i] = new double [colsB];
}
for(int i=0; i<rowsB; i++)
{
for(int j=0; j<colsB; j++)
{
B[i][j]=0.0;
cout << "B[" << i << "," << j << "] = ";
cin >> B[i][j];
}
}
}
void get_C(double** A, double** B, int& rowsA, int& colsA, int& colsB)
{
using namespace std;
double** C;
C = new double* [rowsA];
for(int i=0; i<rowsA; i++)
{
C[i] = new double [colsB];
}
for(int i=0; i<rowsA; i++)
{
for(int j=0; j<colsA; j++)
{
for(int k=0; k<colsA; k++)
{
C[i][j]=0.0;
C[i][j] += A[i][k]*B[k][j];
}
cout << "C[" << i << "," << j << "] = " << C[i][j] << endl;
}
}
}
void FreeMatrix_A(int& rowsA, double** A)
{
for(int i=0; i<rowsA; i++)
{
delete[] A[i];
}
delete A;
}
void FreeMatrix_B(int& rowsB, double** B)
{
for(int i=0; i<rowsB; i++)
{
delete[] B[i];
}
delete B;
}
void FreeMatrix_C(int& rowsA, double** C)
{
for(int i=0; i<rowsA; i++)
{
delete[] C[i];
}
delete C;
}
int main(int argc, char* argv[])
{
//using namespace std;
double** A;
double** B;
double** C;
int rowsA=0, colsA=0, rowsB=0, colsB=0;
intro();
get_A(rowsA, colsA);
get_B(rowsB, colsB);
get_C(A, B, rowsA, colsA, colsB);
FreeMatrix_A(rowsA, A);
FreeMatrix_B(rowsB, B);
FreeMatrix_C(rowsA, C);
return 0;
}
|