-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMatrix-Multiplication.cpp
More file actions
61 lines (44 loc) · 1.58 KB
/
Matrix-Multiplication.cpp
File metadata and controls
61 lines (44 loc) · 1.58 KB
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
//Multiply Two Matrices
#include<iostream>
using namespace std;
int main() {
int matrixOne[10][10], matrixTwo[10][10], matrixThree[10][10], sum=0, i, j, k;
int matrixOneWidth, matrixOneHeight, matrixTwoWidth, matrixTwoHeight; //width = columns, height = rows
cout<< "Enter first matrix's width : ";
cin>> matrixOneWidth;
cout<< "Enter first matrix's height : ";
cin>> matrixOneHeight;
cout<< "Enter second matrix's width : ";
cin>> matrixTwoWidth;
cout<< "Enter second matrix's height : ";
cin>> matrixTwoHeight;
if(matrixOneWidth != matrixTwoHeight) {
cout<< "Can not multiply the matrices.";
exit(-1);
}
cout<< "Enter first matrix elements : ";
for(i=0; i<matrixOneHeight; i++) {
for(j=0; j<matrixOneWidth; j++)
cin>> matrixOne[i][j];
}
cout<< "Enter second matrix elements : ";
for(i=0; i<matrixTwoHeight; i++) {
for(j=0; j<matrixTwoWidth; j++)
cin>> matrixTwo[i][j];
}
cout<< "Multiplying two matrices...\n";
for(i=0; i<matrixOneHeight; i++) { //iterate through rows of 1st matrix
for(j=0; j<matrixTwoWidth; j++) { //iterate through columns of 2nd matrix
sum = 0; //reset sum
for(k=0; k<matrixTwoHeight; k++) //iterate through rows of 2nd matrix
sum = sum + (matrixOne[i][k] * matrixTwo[k][j]); //multiply each and find the sum
matrixThree[i][j] = sum;
}
}
cout<< "\nProduct of two matrices : \n";
for(i=0; i<matrixOneHeight; i++) {
for(j=0; j<matrixTwoWidth; j++)
cout<< matrixThree[i][j] << " ";
cout<< "\n";
}
}