matrix multiplication problem

Hi. The problem is to create two matrices using vectors from the user in the main(). The user decides the size of the matrices as well as its values within it. It then calls the multiply_matrices() to multiply the two matrices to print out the result. I have having trouble with the multiplication function. The output always jump the the else statement, printing out "Error: Cannot multiply matrices" EVERY single time. What am I doing wrong here?
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
137
138
  #include <cstdlib>
#include <iostream>
#include <vector>

using namespace std;


void multiply_matrices(const vector<vector<int> > & matrix_1, const vector<vector<int> > & matrix_2 ){
    
    int num_cols_3;
    int num_rows_3;
    int num_rows_1;
    int num_cols_1;
    int num_rows_2;
    int num_cols_2;
  
    
   //declare matrix 3
   vector<vector<int> > matrix_3(num_rows_3);
    
    
    for(int i= 0; i < num_rows_3; i++){
        matrix_3[i].resize(num_cols_3);
    }
    
    
    //checks if multiplication can be done
    if(num_cols_1==num_rows_2)
    {
        
        for(int i=0; i<num_rows_1; i++)
        {
            for(int j=0;j<num_cols_2; j++)
            {
                matrix_3[i][j]=0;
                for(int k=0; k<num_rows_2; k++)
                {
                    matrix_3[i][j]= matrix_3[i][j]+ matrix_1[i][k]*matrix_2[k][j];
                }
            }
        }
 
        cout<<"\n-----------------------------------------------------------\n";
 
        cout<<"\n\nMultiplication of Matrix A and Matrix B :\n\n";
 
        for(int i=0;i<num_rows_1;i++)
        {
            for(int j=0;j<num_cols_2;j++)
            {
                cout<<"\t"<<matrix_3[i][j];
            }
            cout<<"\n\n";
        }
    } else{
        cout<<"Error: Cannot multiply matrices";
      }
    
}

int main()
{
    //declare matrix 1    
    int num_rows_1 = 0;
    int num_cols_1 = 0;
   
   
    //Prompt user for matrices dimensions
    cout <<"Enter number of rows " << endl;
    cin >> num_rows_1;
    cout << "Enter number of columns \n" ;
    cin >> num_cols_1;
    
    //initialize matrices
    vector< vector<int> > matrix_1(num_rows_1);
    for(int i= 0; i < num_rows_1; i++){
        matrix_1[i].resize(num_cols_1);
    }
    
    //prompt user for values in Matrix 1
    for(int i = 0; i< num_rows_1; i++)
    {
        for(int j = 0; j<num_cols_1; j++)
        {
        cout<<"Enter the numbers for Matrix 1 \n";
             cin>>matrix_1[i][j];
        }
    }
    
    //print out matrix 1
 //   cout << "=====" <<num_rows_1<< "x"<< num_cols_1<< "matrix 1==========\n";	
	for (int i=0; i<num_rows_1; i++) {
		for (int j=0; j<num_cols_1; j++ ){
			cout << matrix_1[i][j] << " ";
		}
		cout << endl;
	}			
  
    //declare Matrix 2
    int num_rows_2 = 0;
    int num_cols_2 = 0;
   
   
    //Prompt user for matrix 2 dimensions
    cout <<"Enter number of rows" << endl;
    cin >> num_rows_2;
    cout << "Enter number of columns" << endl;
    cin >> num_cols_2;
    
    //initialize matrix 2
    vector< vector<int> > matrix_2(num_rows_2);
    for(int i= 0; i < num_rows_2; i++){
        matrix_2[i].resize(num_cols_2);
    }
    //prompt user for values in Matrix 2
    for(int i = 0; i< num_rows_2; i++)
    {
        for(int j = 0; j<num_cols_2; j++)
        {
        cout<<"Enter the numbers for Matrix 2 \n";
             cin>>matrix_2[i][j];
        }
    }
    //print out matrix 2
 //   cout << "====="<< num_rows_2 << "x" << num_cols_2 <<"matrix 2==========\n" ;	
	for (int i=0; i<num_rows_2; i++) {
		for (int j=0; j<num_cols_2; j++ ){
			cout << matrix_2[i][j] << " ";
		}
		cout << endl;
	}			
       
   //calls multiply_matrices function  
   multiply_matrices(matrix_1, matrix_2);
    
   
}


Here is the output:
Enter number of rows
2
Enter number of columns
2
Enter the numbers for Matrix 1
1
Enter the numbers for Matrix 1
2
Enter the numbers for Matrix 1
3
Enter the numbers for Matrix 1
4
=====2x2matrix 1==========
1 2
3 4
Enter number of rows
2
Enter number of columns
2
Enter the numbers for Matrix 2
5
Enter the numbers for Matrix 2
6
Enter the numbers for Matrix 2
7
Enter the numbers for Matrix 2
8
=====2x2matrix 2==========
5 6
7 8
Error: Cannot multiply matrices


Last edited on
On lines 10..15 you created a bunch of variables but gave them no value. Therefore, their values are random. (If you are lucky they might be zero or something. If you are unlucky, your program might appear to hang your PC while trying to allocate all the space for that many numbers.)

Don't forget to initialize your variables before using them.

10
11
12
13
14
15
    int num_rows_1 = matrix_1.size();
    int num_cols_1 = matrix_1[0].size();
    int num_rows_2 = matrix_2.size();
    int num_cols_2 = matrix_2[0].size();
    int num_cols_3 = num_cols_2;
    int num_rows_3 = num_rows_1;

Hope this helps.
That not only helped, it totally made the whole program run flawlessly. I suspected the variables were not initialized properly. Thanks Duoas for the swift safe!
Topic archived. No new replies allowed.