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
|
#include<vector>
#include<iostream>
using namespace std;
int aRowSize, aColSize,bRowSize, bColSize; //creates column and row size
int aValue, bValue; //stores AN input for a row and column
vector<int> aTemp, bTemp; //stores each input in value and puts it in a vector
vector<int> colA, rowA, colB, rowB; //stores row# and column# size for matrix
vector< vector<int> > matrixA; vector< vector<int> > matrixB; //vector that stores the value of each row and column
int multiply_matrices();
int main(){
cout << "This program will create two matrices and muliply them together \n"
<< "=============================================================== \n";
/* Creates dimension for matrix */
cout <<"Enter the row size of matrixA: "<<endl;
cin >>aRowSize;
cout <<endl;
cout <<"Enter the column size of matrixA: "<<endl;
cin >>aColSize;
cout <<endl;
cout <<"Enter the row size of matrixB: "<<endl;
cin >>bRowSize;
cout <<endl;
cout <<"Enter the column size of matrixB: "<<endl;
cin >>bColSize;
cout <<endl;
/* Creates matrixA*/
for (int i = 0; i <= aRowSize-1; i++)
{
for(int j = 1; j <= aColSize; j++)
{
cout << "For matrixA, enter the value of row " << (i+1) << ", column " << (j) << ": ";
cin >> aValue;
aTemp.push_back(aValue);
}
matrixA.push_back(aTemp);
aTemp.clear();
}
cout << endl;
/* Creates matrixB*/
for (int i = 0; i <= bRowSize-1; i++)
{
for(int j = 1; j <= bColSize; j++)
{
cout << "For matrixB, enter the value of row " << (i+1) << ", column " << (j) << ": ";
cin >> bValue;
bTemp.push_back(bValue);
}
matrixB.push_back(bTemp);
bTemp.clear();
}
/*results for matrixA */
cout <<"Your matrixA is: "<<endl;
for (int i = 0; i < aRowSize; i++)
{
for(int j = 0; j < aColSize; j++)
{
cout<<matrixA[i][j]<< " ";
}
cout <<endl;
}
/*results for matrixB*/
cout <<"Your matrixB is: "<<endl;
for (int i = 0; i < bRowSize; i++)
{
for(int j = 0; j < bColSize; j++)
{
cout<<matrixB[i][j]<< " ";
}
cout <<endl;
}
multiply_matrices();
}
int multiply_matrices()
{
int i, j, k;
vector< vector<int> > matrixC;
if (colA != rowB)
{
cout << "incompatible dimensions";
}
else //ERROR OCCURS HERE
{
for (i=1; i <aRowSize; i++)
{
for (j=1; j <bColSize; i++)
{
matrixC[i][j] =0;
for(k = 1; i<aColSize;i++)
{
matrixC[i][j]=matrixC[i][j] + matrixA[i][k] * matrixB[k][j];
}
} // TO HERE
}
}
for (int i = 0; i < aRowSize; i++)
{
for(int j = 0; j < bColSize; j++)
{
cout<<matrixC[i][j]<< " ";
}
cout <<endl;
}
}
|