-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSample.cpp
More file actions
96 lines (95 loc) · 2 KB
/
Sample.cpp
File metadata and controls
96 lines (95 loc) · 2 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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
#include <iostream>
using namespace std;
class MatrixSum
{
int r, c;
int **x;
int **y;
int **z;
public:
MatrixSum(int r1, int c1)
{
r = r1;
c = c1;
x = new int *[r];
y = new int *[r];
z = new int *[r];
for (int i = 0; i < r; i++)
{
x[i] = new int[c];
y[i] = new int[c];
z[i] = new int[c];
}
}
void input()
{
cout << "Enter elements of the 1st Matrix:" << endl;
for (int i = 0; i < r; i++)
{
for (int j = 0; j < c; j++)
{
cin >> x[i][j];
}
}
cout << "Enter elements of the 2nd Matrix:" << endl;
for (int i = 0; i < r; i++)
{
for (int j = 0; j < c; j++)
{
cin >> y[i][j];
}
}
}
void calculate()
{
for (int i = 0; i < r; i++)
{
for (int j = 0; j < c; j++)
{
z[i][j] = x[i][j] + y[i][j];
}
}
}
void display()
{
cout << "Output Matrix:" << endl;
for (int i = 0; i < r; i++)
{
for (int j = 0; j < c; j++)
{
cout << z[i][j] << "\t";
}
cout << endl;
}
}
~MatrixSum()
{
for (int i = 0; i < r; i++)
{
delete[] x[i];
delete[] y[i];
delete[] z[i];
}
delete[] x;
delete[] y;
delete[] z;
}
};
int main()
{
int r, c;
cout << "Enter the number of rows and columns of the matrices:" << endl;
cin >> r >> c;
if (r > 0 && c > 0)
{
MatrixSum obj(r, c);
obj.input();
obj.calculate();
obj.display();
}
else
{
cout << "Error: Rows and columns should be greater than 0" << endl;
}
return 0;
}