Write a function in C language, Implement following functions: void display_menu
ID: 3554423 • Letter: W
Question
Write a function in C language, Implement following functions:
void display_menu(),
int check_option(int),
int check_size(int),
void initialize_2Darray(int x[][MAX], int size),
void print_2Darray(int x[][MAX], int size),
void initialize_1Darray(int y[],int size ),
void print_1Darray(int y[],int size ),
int search_max((int x[][MAX], int r, int c, int size),
int count_diagonal (int x[][MAX], int i, int size),
int closest_row((int x[][MAX], int y[], int size),
void sort_1Darray(int y[],int size ),
void sort_2Darray(int x[][MAX], int size ),
main().
The sample output is:
Enter the size: -1
Invalid input enter the size of the array again: 5
Array operations, your options are:
1: Search Max
2: Count Diagonal Number
3: Closest Row
4: Sort 1D Array
5: Sort 2D Array
6: Exit
Enter the operation you want to perform: 7
Invalid option enter the option again: 1
2D array
4 0 0 5 3
2 9 9 9 8
6 2 6 4 9
4 9 1 6 6
5 6 8 8 9
Enter the row: 3
Enter the col: 2
The largest number present the row 3 and col 2 is 9
Array operations, your options are:
1: Search Max
2: Count Diagonal Number
3: Closest Row
4: Sort 1D Array
5: Sort 2D Array
6: Exit
Enter the operation you want to perform: 0
Invalid option enter the option again: 2
2D arrays
2 9 7 1 9
3 5 9 3 0
3 5 2 4 5
0 1 7 8 5
8 2 7 0 1
Diagonal element 2 is present 0 more times in the row 0 and col 0
Diagonal element 5 is present 1 more times in the row 1 and col 1
Diagonal element 2 is present 0 more times in the row 2 and col 2
Diagonal element 8 is present 0 more times in the row 3 and col 3
Diagonal element 1 is present 0 more times in the row 4 and col 4
Array operations, your options are:
1: Search Max
2: Count Diagonal Number
3: Closest Row
4: Sort 1D Array
5: Sort 2D Array
6: Exit Enter the operation you want to perform: 3
2D arrays
5 7 9 5 7
8 7 6 5 9
5 8 4 7 3
7 2 9 9 6
4 1 7 3 1
1D array
5 1 5 4 3
Row 4 in the 2D array is closest to the 1D array
Array operations, your options are:
1: Search Max
2: Count Diagonal Number
3: Closest Row
4: Sort 1D Array
5: Sort 2D Array
6: Exit
Enter the operation you want to perform: 4
1D Array before sorting
6 9 2 7 4
1D Array after sorting
2 4 6 7 9
Array operations, your options are:
1: Search Max
2: Count Diagonal Number
3: Closest Row
4: Sort 1D Array
5: Sort 2D Array
6: Exit
Enter the operation you want to perform: 5
2D Array before sorting
9 8 1 6 3
0 3 4 7 0
7 4 2 8 5
9 4 8 8 7
9 5 1 6 9
2D Array after sorting
1 3 6 8 9
0 0 3 4 7
2 4 5 7 8
4 7 8 8 9
1 5 6 9 9
Array operations, your options are:
1: Search Max
2: Count Diagonal Number
3: Closest Row
4: Sort 1D Array
5: Sort 2D Array
6: Exit
Enter the operation you want to perform: 6
Exiting the program.
Please in C language! thank you very much !!
Explanation / Answer
1 #include<stdio.h>
2 #include<stdlib.h>
3 #include<time.h>
4 #define MAX 100
5
6 void display_menu();
7 int check_option(int);
8 int check_size(int size);
9 void initialize_2Darray(int a[][MAX], int size);
10 void initialize_1Darray(int b[], int size);
11 void print_2Darray(int a[][MAX], int size);
12 void print_1Darray(int b[], int size);
13 int search_max(int a[][MAX], int row, int col, int size);
14 int count_diagonal(int a[][MAX], int i, int size);
15 int closest_row(int a[][MAX], int b[], int size);
16 void sort_1Darray(int b[], int size);
17 void sort_2Darray(int a[][MAX], int size);
18 int find_index(int a[][MAX], int size);
19
20 int main()
21 {
22 int n, o, i, d, x;
23 int row, col;
24 int ar1[MAX];
25 int ar2[MAX][MAX];
26 int num1, num2;
27
28 srand(time(NULL));
29
30 printf(" Enter the size:");
31 scanf("%d", &n);
32
33 check_size(n);
34
35 while (check_size(n) == 0)
36 {
37 printf(" Invalid input enter the size of the array again:");
38 scanf("%d", &n);
39 }
40
41 do
42 {
43 initialize_1Darray(ar1, n);
44 initialize_2Darray(ar2, n);
45 display_menu();
46
47 printf(" Enter the operation you want to perform:");
48 scanf("%d", &o);
49
50 check_option(o);
51
52 while (check_option(o) == 0)
53 {
54 printf(" Invalid option enter the option again:");
55 scanf("%d", &o);
56 }
57
58 switch (o)
59 {
60 case 1:
61 printf(" 2D array ");
62 print_2Darray(ar2, n);
63 printf(" Enter the row:");
64 scanf("%d", &row);
65 printf(" Enter the column:");
66 scanf("%d", &col);
67 printf(" The largest number present in row %d and column %d is %d ", row, col, search_max(ar2, row, col, n));
68 break;
69
70 case 2:
71 printf(" 2D array ");
72 print_2Darray(ar2, n);
73
74 for (x = 0; x < n; x++)
75 {
76 printf(" The diagonals are %d", find_index(ar2, n));
77 }
78 break;
79
80 case 4:
81 printf(" 1D Array before sorting ");
82 print_1Darray(ar1, n);
83 sort_1Darray(ar1, n);
84 printf(" 1D Array after sorting ");
85 print_1Darray(ar1, n);
86 break;
87
88 case 5:
89 printf(" 2D Array before sorting ");
90 print_2Darray(ar2, n);
91 sort_2Darray(ar2, n);
92 printf(" 2D Array after sorting ");
93 print_2Darray(ar2, n);
94 break;
95 }
96 }
97
98 while (o != 6);
99
100 return 0;
101 }
102
103 int find_index(int a[][MAX], int size)
104 {
105 int x, z;
106
107 for (x = 0; x < size; x++)
108 {
109 z = a[x][x];
110 return z;
111 }
112
113 }
114
115
116
117
118
119
120
121
122
123
124
125
126 void sort_2Darray(int a[][MAX], int size)
127 {
128 int x, y, z;
129 int s;
130
131 for (x = 0; x < size; x++)
132 {
133 for (y = 0; y < size; y++)
134 {
135 for (z = y + 1; z < size; z++)
136 {
137 if (a[x][z] < a[x][y])
138 {
139 s = a[x][y];
140 a[x][y] = a[x][z];
141 a[x][z] = s;
142 }
143 }
144 }
145 }
146
147 }
148
149 void sort_1Darray(int b[], int size)
150 {
151 int x, y, z;
152
153 for (x = 0; x < size; x++)
154 {
155 for (y = x + 1; y < size; y++)
156 {
157 if (b[x] > b[y])
158 {
159 z = b[y];
160 b[y] = b[x];
161 b[x] = z;
162 }
163 }
164 }
165
166 }
167
168 int search_max(int a[][MAX], int r, int c, int size)
169 {
170 int maxr = a[0][0];
171 int maxc = a[0][0];
172 int maxfin = 0;
173 int x;
174 int y;
175
176 for (x = r; x <= r; x++)
177 {
178 for (y = 0; y < size; y++)
179 {
180 if (maxr < a[x][y])
181 {
182 maxr = a[x][y];
183 }
184 } }
185
186 for (y = c; y <= c; y++)
187 {
188 for (x = 0; x < size; x++)
189 {
190 if (maxc < a[x][y])
191 {
192 maxc = a[x][y];
193 }
194 }
195 }
196
197 if (maxr > maxc)
198 {
199 maxfin = maxr;
200 }
201
202 else
203 {
204 maxfin = maxc;
205 }
206
207 return maxfin;
208 }
209
210 int check_option(int op)
211 {
212 if (op > 0 && op <= 6)
213 {
214 return 1;
215 }
216
217 else
218 {
219 return 0;
220 }
221 }
222
223 void display_menu()
224 {
225 printf(" Array operations, your options are ");
226 printf(" 1:Search Max ");
227 printf("2:Count Diagonal Number ");
228 printf("3:Closest Row ");
229 printf("4:Sort 1D Array ");
230 printf("5:Sort 2D Array ");
231 printf("6:Exit ");
232
233 }
234
235 int check_size(int size)
236 {
237 if (size > 0 && size <= 100)
238 {
239 return 1;
240 }
241
242 else
243 {
244 return 0;
245 }
246 }
247
248 void initialize_1Darray(int b[], int size)
249 {
250 int x;
251
252 for (x = 0; x < size; x++)
253 {
254 b[x] = rand()%10;
255 }
256 }
257
258 void print_1Darray(int b[], int size)
259 {
260 int x;
261
262 for (x = 0; x < size; x++)
263 {
264 printf("%d ", b[x]);
265 }
266
267 printf(" ");
268 }
269
270 void initialize_2Darray(int a[][MAX], int size)
271 {
272 int x, y;
273
274 for (x = 0; x < size; x++)
275 {
276 for (y = 0; y < size; y++)
277 {
278 a[x][y] = rand()%10;
279 }
280 }
281 }
282
283 void print_2Darray(int a[][MAX], int size)
284 {
285 int x, y;
286
287 for (x = 0; x < size; x++)
288 {
289 for (y = 0; y < size; y++)
290 {
291 printf("%d ", a[x][y]);
292 }
293
294 printf(" ");
295 }
296 }
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.