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 129 130 131 132 133 134 135 136
|
#include <iostream>
#include <cassert>
using namespace std;
void Multiply(double* [], double* [], double* [], int, int, int, int);
void DisplayMatrix(double**, int, int);
double** MakeMatrix(int, int);
bool MatrixMultiplicationCheck(int, int);
void DataEntryMatrix(double**, int, int);
void FreeMatrixMemory(int numRows, double** matrix);
void DisplayMatrix(double** matrix, int rows, int cols)
{
for (int i = 0; i < rows; i++)
{
for (int j = 0; j < cols; j++)
{
cout << matrix[i][j] << ", ";
}
}
}
double** MakeMatrix(int numRows, int numCols)
{
double** matrix;
matrix = new double* [numRows];
for (int i = 0; i < numRows; i++)
{
matrix[i] = new double[numCols];
}
return matrix;
}
void FreeMatrixMemory(int numRows, double** matrix)
{
for (int i = 0; i < numRows; i++)
{
delete[] matrix[i];
}
delete[] matrix;
}
void DataEntryMatrix(double** x, int numRows, int numCols)
{
double temp;
for (int i = 0; i < numRows; i++)
{
for (int j = 0; j < numCols; j++)
{
cout << "What is number you would like to enter?" << endl;
cin >> temp;
x[i][j] = temp;
}
}
cout << "The elements of your matirix are " << endl;
cout << "{ ";
for (int i = 0; i < numRows; i++)
{
for (int j = 0; j < numCols; j++)
{
cout << x[i][j] << ", ";
}
}
cout << "}" << endl;
}
bool MatrixMultiplicationCheck(int columns1, int rows2)
{
assert(columns1 != rows2);
{
cout << "Matrices cannot be mutiplied" << endl;
return false;
}
return true;
}
void Multiply(double* x[], double* y[], double* z[], int numRows1, int numCols1, int numRows2, int numCols2)
{
for (int i = 0; i < numCols1; i++)
{
for (int j = 0; j < numCols1; j++)
{
z[i][j] = 0;
for (int k = 0; k < numCols1; k++)
{
z[i][j] += x[i][k] * y[k][j];
}
}
}
}
int main()
{
int i, j, k, l = 0;
cout << "How many rows in your first matrix?" << endl;
cin >> i;
cout << "How many columns in your first matrix?" << endl;
cin >> j;
cout << "How many rows in your 2nd matrix?" << endl;
cin >> k;
cout << "How many columns in your 2nd matrix?" << endl;
cin >> l;
cout << "Matrix A has rows = " << i << " and columns = " << j << endl;
cout << "Matrix B has rows = " << k << " and columns = " << l << endl;
//MatrixMultiplicationCheck(j, k);
double** A;
double** B;
double** C;
A = MakeMatrix(i, j);
B = MakeMatrix(k, l);
C = MakeMatrix(j, k);
DataEntryMatrix(A, i, j);
DataEntryMatrix(B, k, l);
Multiply(A, B, C, i, j, k, l);
DisplayMatrix(C, j, k);
FreeMatrixMemory(i, A);
FreeMatrixMemory(l, B);
FreeMatrixMemory(j, C);
return 0;
}
| |