-
Notifications
You must be signed in to change notification settings - Fork 0
/
Program for multiplication of two matrices.cpp
53 lines (46 loc) · 1.34 KB
/
Program for multiplication of two matrices.cpp
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
#include <iostream>
using namespace std;
const int ROWS_1 = 2; // number of rows in first matrix
const int COLS_1 = 3; // number of columns in first matrix
const int ROWS_2 = 3; // number of rows in second matrix
const int COLS_2 = 2; // number of columns in second matrix
void matrixMultiplication(int matrix1[][COLS_1], int matrix2[][COLS_2], int result[][COLS_2]) {
// Initialize result matrix with 0s
for(int i = 0; i < ROWS_1; i++) {
for(int j = 0; j < COLS_2; j++) {
result[i][j] = 0;
}
}
// Perform matrix multiplication
for(int i = 0; i < ROWS_1; i++) {
for(int j = 0; j < COLS_2; j++) {
for(int k = 0; k < ROWS_2; k++) {
result[i][j] += matrix1[i][k] * matrix2[k][j];
}
}
}
}
void printMatrix(int matrix[][COLS_2]) {
for(int i = 0; i < ROWS_1; i++) {
for(int j = 0; j < COLS_2; j++) {
cout << matrix[i][j] << " ";
}
cout << endl;
}
}
int main() {
int matrix1[ROWS_1][COLS_1] = {
{1, 2, 3},
{4, 5, 6}
};
int matrix2[ROWS_2][COLS_2] = {
{7, 8},
{9, 10},
{11, 12}
};
int result[ROWS_1][COLS_2];
matrixMultiplication(matrix1, matrix2, result);
cout << "Result of matrix multiplication:" << endl;
printMatrix(result);
return 0;
}