I am working on making a program that can multiply matrices of user-defined size. I use vectors to store the values in the matrix.
void Multiply(vector<float> A,vector<float> B,int rA, int cA,int rB,int cB)
{
system(CLEARSCREEN);
vector<float> C; // The resulting matrix
i开发者_Python百科nt sizeA=rA*cA;
int sizeB=rB*cB;
int sizeC=rA*cB;
int lrA=sizeA-1;
int lrB=sizeB-1;
int writeHead=0;
A.resize(sizeA);
B.resize(sizeB);
C.resize(sizeC);
demoDisplay(rA,rB,cA,cB,lrA,lrB,sizeA,sizeB);
for(;writeHead<=lrA; writeHead++)
{
cout << "Please enter a value for \"" << alphabet[writeHead] << "\" in MATRIX A.\n";
cin >> A[writeHead];
}
cout << "\n";
writeHead=0;
for (;writeHead<=lrB; writeHead++)
{
cout << "Please enter a value for \"" << alphabet[writeHead] << "\" in MATRIX B.\n";
cin >> B[writeHead];
}
cout << "\n\n";
displayMatrices(A,B,rA,rB,cA,cB,lrA,lrB,sizeA,sizeB);
for (int colRead=0; colRead<=cA; colRead++) {
// somehow iterate through each element of the vector?
}
}
I'm relatively new to C++, and so I'm not quite sure how to do the actual multiplication of the two matrices. If anyone could help, it would be great.
Maybe you were mislead by the name of the vector container, that implies some mathematical use. The vector template doesn't provide any function to multiply matrices or even to multiply vectors. The vector in this case only provides you with a container to store a matrix. Obviously you store the matrices in some linearized way and that will make the multiplication more complicated later.
Be sure to read http://www.cplusplus.com/reference/stl/vector/
Furthermore you dont really want to iterate through the vectors, because if that was the case, you could have just used some other container. You want random access to do multiply the columns and rows by hand. For this you can use the []-operator] or the at() member function.
Then it is just a matter of doing the multiplication by hand, as for example shown here(which also includes some pseudo code).
精彩评论