Matrix multiplication

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.

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
#include<iostream>
#include<vector>

using namespace std;

int main() {
	vector<vector<int>> MatrixA;
	vector<vector<int>> MatrixB;
	int numRowsA = 0;
	int numColsA = 0;
	int numRowsB = 0;
	int numColsB = 0;

	cout << "Enter the number of rows for Matrix A: ";
	cin >> numRowsA;
	cout << endl;
	cout << "Enter the number of columns for Matrix A: ";
	cin >> numColsA;
	cout << endl;

	for (int i = 0; i < numRowsA; i++) {
		vector<int> tempVector;
		for (int j = 0; j < numColsA; j++) {
			tempVector.push_back(i);
		}
		MatrixA.push_back(tempVector);
	}

	cout << "Put values into Matrix A: " << endl;
	for (int i = 0; i < numRowsA; i++) {
		for (int j = 0; j < numColsA; j++) {	// this part populates Matrix A with whatever values the user inputs
			cout << "Enter for position [" << i << "][" << j << "]: ";
			cin >> MatrixA[i][j];
			cout << endl;
		}
	}

	cout << "This is what matrix A looks like:" << endl;
	for (int i = 0; i < numRowsA; i++) {
		for (int j = 0; j < numColsA; j++) {
			cout << MatrixA[i][j] << " ";
		}
		cout << endl;
	}

	cout << "Now matrix B..." << endl;
	cout << "Enter the number of rows for Matrix B: ";
	cin >> numRowsB;
	cout << endl;
	cout << "Enter the number of columns for Matrix B: ";
	cin >> numColsB;          // ***ERROR*** - the compiler stops the program here and gives an error saying "vector subscript out of range". Don't know why
	cout << endl;

	for (int i = 0; i < numRowsB; i++) {			// populates matrix B with user input values
		for (int j = 0; j < numColsB; j++) {
			cout << "Enter for position [" << i << "][" << j << "]: ";
			cin >> MatrixB[i][j];
			cout << endl;
		}
	}

	for (int i = 0; i < numRowsB; i++) {
		vector<int> tempVector2;
		for (int j = 0; j < numColsB; j++) {
			tempVector2.push_back(i);
		}
		MatrixB.push_back(tempVector2);
	}

	cout << "This is what matrix B looks like:" << endl;
	for (int i = 0; i < numRowsB; i++) {
		for (int j = 0; j < numColsB; j++) {
			cout << MatrixB[i][j] << " ";
		}
		cout << endl;
	}

	cout << "Now for multiplication..." << endl;

	for (int i = 0; i < numRowsA; i++) {
		for (int j = 0; j < numColsB; j++) {
			int tempVal = 0;
			for (int k = 0; k < numColsA; k++) {
				tempVal += MatrixA[i][k] * MatrixB[k][j];
			}
			cout << tempVal << " ";
		}
		cout << endl;
	}



	system("pause");

	return 0;
}
Last edited on
Your code:
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

Do you notice a difference?

Not to mention unnecessary work:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
// 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 ( const auto & 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.
Topic archived. No new replies allowed.