-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path17.1 (C++) Matrix addition and subtraction by using 2D array.cpp
More file actions
170 lines (144 loc) · 5.84 KB
/
17.1 (C++) Matrix addition and subtraction by using 2D array.cpp
File metadata and controls
170 lines (144 loc) · 5.84 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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
/// Matrix addition and subtraction by using 2D array
/*
Explain:
col [0] | col [1] | col [2] | col [3]
_________________________________________
Row [0] | 10 | 11 | 12 | 13 | -> Index Number
_________________________________________
Row [1] | 20 | 21 | 22 | 23 | -> Index Number
_________________________________________
Row [2] | 30 | 31 | 32 | 33 | -> Index Number
*/
#include <iostream>
using namespace std;
int main()
{
int a[50][50], b[50][50], c[50][50];
int row, col, numberOfrows, numberOfcols, r=0, s=0;
// Input the total numbers of row and columns
cout<<" Enter the total row of the matrix: ";
cin>>numberOfrows;
cout<<" Enter the total col of the matrix: ";
cin>>numberOfcols;
/* ------------------- Insert the values of A & B Matrix ------------------- */
cout<<"\n ------------------- Insert the values A & B Matrix ------------------- \n";
cout<<"\n ------------------- Insert the values of [A] Matrix: ";
cout<<"\n Enter the values of row 0: \n";
for(row=0; row<numberOfrows; row++){ // Insert the values of A Matrix
for(col=0; col<numberOfcols; col++){
cout<<" a["<<row<<"]["<<col<<"]: ";
cin>>a[row][col];
}
r++;
if(r==numberOfrows){
cout<<"\n";
}
else{
cout<<"\n Enter the values of row "<<r<<": ";
}
cout<<"\n";
}
cout<<"\n ------------------- Insert the values of [B] Matrix: ";
cout<<"\n Enter the values of row 0: \n"; // Insert the values of B Matrix
for(row=0; row<numberOfrows; row++){
for(col=0; col<numberOfcols; col++){
cout<<" b["<<row<<"]["<<col<<"]: ";
cin>>b[row][col];
}
s++;
if(s==numberOfrows){
cout<<"\n";
}
else{
cout<<"\n Enter the values of row "<<s<<": ";
}
cout<<"\n";
}
/*------------------- Print the values of A & B Matrix ------------------- */
cout<<"\n ------------------- Print the Values of [A] & [B] Matrix ------------------- \n";
cout<<" The A Matrix is: \n";
for(int row=0; row<numberOfrows; row++){ // Print A Matrix
cout<<"\t\t\t";
for(int col=0; col<numberOfcols; col++){
cout<<a[row][col]<<" ";
}
cout<<"\n";
}
cout<<"\n The B Matrix is: \n";
for(int row=0; row<numberOfrows; row++){ // Print B Matrix
cout<<"\t\t\t";
for(int col=0; col<numberOfcols; col++){
cout<<b[row][col]<<" ";
}
cout<<"\n";
}
/*------------------- Summation of A & B Matrix ------------------- */
for(row=0; row<numberOfrows; row++){ /// Summation: A and B matrix
for(col=0; col<numberOfcols; col++){
c[row][col] = a[row][col] + b[row][col];
}
}
/*------------------- Subtraction of A & B Matrix ------------------- */
for(row=0; row<numberOfrows; row++){ /// Subtraction: A and B matrix
for(col=0; col<numberOfcols; col++){
c[row+(numberOfrows+1)][col+(numberOfrows+1)] = a[row][col] - b[row][col];
}
}
/*------------------- Print the C Matrix ------------------- */
cout<<"\n ------------------- Print the Values of [C] Matrix ------------------- \n";
cout<<" The Summation of [A]+[B] is: \n";
for(int row=0; row<numberOfrows; row++){ /// Print A+B Matrix
cout<<"\t\t\t";
for(int col=0; col<numberOfcols; col++){
cout<<c[row][col]<<" ";
}
cout<<"\n";
}
cout<<"\n The Subtraction of [A]-[B] is: \n";
for(int row=0; row<numberOfrows; row++){ /// Print A-B Matrix
cout<<"\t\t\t";
for(int col=0; col<numberOfcols; col++){
cout<<c[row+(numberOfrows+1)][col+(numberOfrows+1)]<<" ";
}
cout<<"\n";
}
return 0;
}
/* ===== Output / Result:
Input:
----------------------------------------
Enter the total row of the matrix: 2
Enter the total col of the matrix: 3
Insert the values of [A] Matrix:
Enter the values of row 0:
a[0][0]: 40
a[0][1]: 41
a[0][2]: 42
Enter the values of row 1:
a[1][0]: 30
a[1][1]: 31
a[1][2]: 32
Insert the values of [B] Matrix:
Enter the values of row 0:
b[0][0]: 20
b[0][1]: 21
b[0][2]: 22
Enter the values of row 1:
b[1][0]: 10
b[1][1]: 11
b[1][2]: 12
Output:
------------------------------
The A Matrix is:
40 41 42
30 31 32
The B Matrix is:
20 21 22
10 11 12
The Sum. of [A]+[B] is:
60 62 64
40 42 44
The Sub. of [A]-[B] is:
20 20 20
20 20 20
*/