-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathDoubleDimensionalArray.cpp
184 lines (163 loc) · 4.82 KB
/
DoubleDimensionalArray.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
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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
#include <iostream>
using namespace std;
/* Method to display the 2-D array */
void display(int n, int m, int a[4][4]){
// loop for rows of matrix
for(int i = 0;i<n;i++){
// loop for column of matrix
for(int j = 0;j<m;j++){
cout<<a[i][j]<<"\t";
}
cout<<"\n";
}
}
/* Print boundary elements of the 2-D Array */
void printBoundary(int n, int m, int a[4][4]){
for(int i = 0;i<n;i++){
for(int j = 0;j<m;j++){
if(i==0 || j==0 || i==(m-1) || j==(n-1)){
cout<<a[i][j]<<"\t";
}
else{
cout<<"\t";
}
}
cout<<"\n";
}
}
/* print the upper left side triangle of the 2-D array */
void printLeft(int n, int m, int a[4][4]){
for(int i = 0;i<n;i++){
for(int j = 0;j<m;j++){
if((i+j)<=(n-1)){
cout<<a[i][j]<<"\t";
}
else{
cout<<"\t";
}
}
cout<<"\n";
}
}
/* Print the lower right side triangle of the 2-D array */
void printRight(int n, int m, int a[4][4]){
for(int i = 0;i<n;i++){
for(int j = 0;j<m;j++){
if((i+j)>=(n-1)){
cout<<a[i][j]<<"\t";
}
else{
cout<<"\t";
}
}
cout<<"\n";
}
}
/* Sum of boundary elements of the 2-D Array */
void sumBoundary(int n, int m, int a[4][4]){
int sumB = 0;
for(int i = 0;i<n;i++){
for(int j = 0;j<m;j++){
if(i==0 || j==0 || i==(m-1) || j==(n-1)){
sumB += a[i][j];
}
}
}
cout<<"The sum of the boundary elements is: " << sumB<<"\n";
}
/* Sum of the upper left triangle */
void sumLeft(int n, int m, int a[4][4]){
int sumL = 0;
for(int i = 0;i<n;i++){
for(int j = 0;j<m;j++){
if((i+j)<(n-1)){
sumL += a[i][j];
}
}
}
cout<<"The sum of the elements on the upper left side of the 2-D array is: "<<sumL<<"\n";
}
/* Sum of lower right triangle */
void sumRight(int n, int m, int a[4][4]){
int sumR = 0;
for(int i = 0;i<n;i++){
for(int j = 0;j<m;j++){
if((i+j)==(n-1)){
sumR += a[i][j];
}
}
}
cout<<"The sum of the elements on the lower right side of the 2-D array is: "<<sumR<<"\n";
}
void sort(int n, int m, int a[4][4]){
int i, j, temp;
for (i = 0; i < n * m - 1; ++i) {
for (j = 0; j < n * m - 1 - i; ++j) {
// row = j/n
// column = j%n
if (a[j / n][j % n] > a[(j + 1) / n][(j + 1) % n]) {
temp = a[(j + 1) / n][(j + 1) % n];
a[(j + 1) / n][(j + 1) % n] = a[j / n][j % n];
a[j / n][j % n] = temp;
}
}
}
}
int main(){
int arr[4][4] = {
{78, 67, 54, 97},
{34, 41, 23, 1},
{90, 59, 82, 20},
{12, 24, 18, 65}
};
int choice = 0;
while(choice != 10){
cout<<"\n";
cout<<"Enter 1 to display the 2-D array"<<"\n";
cout<<"Enter 2 to print the boundary of the 2-D array"<<"\n";
cout<<"Enter 3 to print the upper left trianlge of the 2-D array"<<"\n";
cout<<"Enter 4 to print the lower right triangle of the 2-D array"<<"\n";
cout<<"Enter 5 to print the sum of the elements in the boundary of the 2-D array"<<"\n";
cout<<"Enter 6 to print the sum of the elements in the upper left triangle of the 2-D array"<<"\n";
cout<<"Enter 7 to print the sum of the elements in the lower right triangle of the 2-D array"<<"\n";
cout<<"Enter 8 to print and display sorted 2-D array"<<"\n";
cout<<"Enter 9 to EXIT"<<"\n";
cout<<"Enter your choice: ";
cin>>choice;
switch(choice){
case 1:
cout<<"The 2-D Array is: "<<"\n";
display(4,4,arr);
break;
case 2:
cout<<"The boundary elements of the 2-D Array: "<<"\n";
printBoundary(4,4,arr);
break;
case 3:
cout<<"The upper left triangle: "<<"\n";
printLeft(4,4,arr);
break;
case 4:
cout<<"The lower right triangle: "<<"\n";
printRight(4,4,arr);
break;
case 5:
sumBoundary(4,4,arr);
break;
case 6:
sumLeft(4,4,arr);
break;
case 7:
sumRight(4,4,arr);
break;
case 8:
sort(4,4,arr);
cout<<"The array after sorting: "<<"\n";
display(4,4,arr);
break;
default:
break;
}
}
cout<<"Thank You.";
}