-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMultiplyMatrix.cpp
More file actions
67 lines (56 loc) · 1.91 KB
/
Copy pathMultiplyMatrix.cpp
File metadata and controls
67 lines (56 loc) · 1.91 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
62
63
64
65
66
67
#include <iostream>
using namespace std;
const int MAX_ROWS = 10;
const int MAX_COLS = 10;
int main() {
int A[MAX_ROWS][MAX_COLS], B[MAX_ROWS][MAX_COLS], result[MAX_ROWS][MAX_COLS];
int rowsA, colsA, rowsB, colsB;
// Input dimensions of the first matrix (A)
cout << "Enter the number of rows and columns for matrix A: ";
cin >> rowsA >> colsA;
// Input elements of the first matrix (A)
cout << "Enter the elements of matrix A:" << endl;
for (int i = 0; i < rowsA; ++i) {
for (int j = 0; j < colsA; ++j) {
cin >> A[i][j];
}
}
// Input dimensions of the second matrix (B)
cout << "Enter the number of rows and columns for matrix B: ";
cin >> rowsB >> colsB;
// Input elements of the second matrix (B)
cout << "Enter the elements of matrix B:" << endl;
for (int i = 0; i < rowsB; ++i) {
for (int j = 0; j < colsB; ++j) {
cin >> B[i][j];
}
}
// Check if matrices can be multiplied
if (colsA != rowsB) {
cout << "Matrix multiplication is not possible. The number of columns in A must be equal to the number of rows in B." << endl;
return 1; // Exit with an error code
}
// Initialize the result matrix to zeros
for (int i = 0; i < rowsA; ++i) {
for (int j = 0; j < colsB; ++j) {
result[i][j] = 0;
}
}
// Perform matrix multiplication
for (int i = 0; i < rowsA; ++i) {
for (int j = 0; j < colsB; ++j) {
for (int k = 0; k < colsA; ++k) {
result[i][j] += A[i][k] * B[k][j];
}
}
}
// Display the result matrix
cout << "Resultant Matrix:" << endl;
for (int i = 0; i < rowsA; ++i) {
for (int j = 0; j < colsB; ++j) {
cout << result[i][j] << " ";
}
cout << endl;
}
return 0;
}