Repository navigation
Expand file tree
/
Copy path24.cpp
More file actions
67 lines (56 loc) · 1.91 KB
/
Copy path24.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
// C++ program to find the sum of two compatible matrices.
#include <iostream>
#define SIZE 10
using std::cin, std::cout, std::cerr;
int main()
{
// declaring the required variables.
int matrix1[SIZE][SIZE], matrix2[SIZE][SIZE], matrixSum[SIZE][SIZE], i, j, rowCount1, columnCount1, rowCount2, columnCount2;
// entering the values for the variables from the console.
cout << "Enter the order of the first matrix: ";
cin >> rowCount1 >> columnCount1;
cout << "Enter the order of the second matrix: ";
cin >> rowCount2 >> columnCount2;
//checking whether the two given matices are compatible or not. If not, then terminate the program.
if(!((rowCount1 == rowCount2) && (columnCount1 == columnCount2)))
{
cerr << "ERROR! Order of given two matrix do not match.\n";
cout << "Explanation: To add two matrices, the order of the matrices must be the same.";
return 1;
}
// entering the values for the two matrices.
cout << "Enter the elements of the first matrix:\n";
for(i = 0; i < rowCount1; ++i)
{
for(j = 0; j < columnCount1; ++j)
{
cin >> matrix1[i][j];
}
}
cout << "Enter the elements of the second matrix:\n";
for(i = 0; i < rowCount2; ++i)
{
for(j = 0; j < columnCount2; ++j)
{
cin >> matrix2[i][j];
}
}
// carrying out matrix addition and printing the result.
for(i = 0; i < rowCount1; ++i)
{
for(j = 0; j < columnCount1; ++j)
{
matrixSum[i][j] = matrix1[i][j] + matrix2[i][j];
}
}
// printing the sum matrix to the console.
cout << "The resultant matrix is:\n";
for(i = 0; i < rowCount1; ++i)
{
for(j = 0; j < columnCount1; ++j)
{
cout << matrixSum[i][j] << "\t";
}
cout << "\n";
}
}