-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtwo_dim_arrays_4.cpp
90 lines (78 loc) · 2.5 KB
/
two_dim_arrays_4.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
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
// Addition of 2D array
#include <iostream>
using namespace std;
int main()
{
int rowsize;
int colsize;
cout<<"Rule: rows and columns must be equal to perform addition of two matrices. " << endl;
cout<<"Hence column size and row size must be equal " << endl;
cout <<" (m x n) +( m x n) format where, m=m and n=n" <<"\n";
cout <<"And m = number of rows and n = number of columns" <<"\n\n";
cout << "Enter row size" << endl;
cin >> rowsize;
cout << "Enter column size" << endl;
cin >> colsize;
if (rowsize == colsize)
{
int mat1[rowsize][colsize];
int mat2[rowsize][colsize];
int result[rowsize][colsize];
cout << "Enter elements in 1st Matrix: " << endl;
for (int i = 0; i < rowsize; i++)
{
for (int j = 0; j < colsize; j++)
{
cout << "arr[" << i << "][" << j << "] = ";
cin >> mat1[i][j];
}
}
cout << "Enter elements in 2nd Matrix: " << endl;
for (int i = 0; i < rowsize; i++)
{
for (int j = 0; j < colsize; j++)
{
cout << "arr[" << i << "][" << j << "] = ";
cin >> mat2[i][j];
}
}
cout <<"Displaying 1st Matrix:" << endl;
for (int i = 0; i < rowsize; i++)
{
cout << "|";
for (int j = 0; j < colsize; j++)
{
cout << mat1[i][j]<<" ";
}
cout << "|";
cout << endl;
}
cout <<"Displaying 2nd Matrix:" << endl;
for (int i = 0; i < rowsize; i++)
{
cout << "|";
for (int j = 0; j < colsize; j++)
{
cout << mat2[i][j]<<" ";
}
cout << "|";
cout << endl;
}
cout <<"Addition of 1st and 2nd Matrix:" << endl;
for (int i = 0; i < rowsize; i++)
{
cout << "|";
for (int j = 0; j < colsize; j++)
{
result[i][j] = mat1[i][j] + mat2[i][j];
cout << result[i][j]<<" ";
}
cout << "|";
cout << endl;
}
}
else{
cout << "Row size and column size must be equal , else addition is not possible" << endl;
}
return 0;
}