I created this code using vectors such that a user can enter values for a Matrix A and a Matrix B, and then multiply the two. I am not worried about error checking, just multiplying the matrices.
However, for some reason when I input the number of columns for matrix B, the program stops. I do not know why. Any suggestions would be helpful.
create empty A
create empty B
get A dimensions
add elements to A
set values to elements of A
show elements of A
get B dimensions
set values to elements of B
add elements to B
show elements of B
multiply A and B
// This has already all elements, each with value 0:
vector<vector<int>> MatrixA( numRowsA, vector<int>(numColsA, 0) );
cout << "Put values into Matrix A:\n";
// use actual size of vectors:
for ( size_t row = 0; row < MatrixA.size(); ++row ) {
for ( size_t col = 0; col < MatrixA[row].size(); ++col ) {
cin >> MatrixA[row][col];
}
}
cout << "This is what matrix A looks like:\n";
// C++11 range-based for syntax:
for ( constauto & row : matrixA ) {
for ( auto elem : row ) {
cout << elem << ' ';
}
cout << '\n';
}
// now create MatrixB
PS. You don't check whether the dimensions of the matrices allow multiplication. You should.