-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy path67.cpp
More file actions
56 lines (47 loc) · 1.57 KB
/
67.cpp
File metadata and controls
56 lines (47 loc) · 1.57 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
#include <iostream>
using namespace std;
int main() {
int rows1, cols1, rows2, cols2;
// Get dimensions for the first matrix
cout << "Enter the number of rows and columns for the first matrix: ";
cin >> rows1 >> cols1;
// Get dimensions for the second matrix
cout << "Enter the number of rows and columns for the second matrix: ";
cin >> rows2 >> cols2;
// Check if dimensions are compatible for addition
if (rows1 != rows2 || cols1 != cols2) {
cout << "Matrices cannot be added. Dimensions are not compatible." << endl;
return 0;
}
int matrix1[rows1][cols1], matrix2[rows2][cols2], result[rows1][cols1];
// Get elements for the first matrix
cout << "Enter elements for the first matrix:" << endl;
for(int i = 0; i < rows1; i++) {
for(int j = 0; j < cols1; j++) {
cin >> matrix1[i][j];
}
}
// Get elements for the second matrix
cout << "Enter elements for the second matrix:" << endl;
for(int i = 0; i < rows2; i++) {
for(int j = 0; j < cols2; j++) {
cin >> matrix2[i][j];
}
}
// Add the matrices
for(int i = 0; i < rows1; i++) {
for(int j = 0; j < cols1; j++) {
result[i][j] = matrix1[i][j] + matrix2[i][j];
}
}
// Display the result matrix
cout << "Resultant matrix after addition:" << endl;
for(int i = 0; i < rows1; i++) {
for(int j = 0; j < cols1; j++) {
cout << result[i][j] << " ";
if(j == cols1 - 1)
cout << endl;
}
}
return 0;
}