-
Notifications
You must be signed in to change notification settings - Fork 0
/
33_8bytes.c
343 lines (318 loc) · 7.01 KB
/
33_8bytes.c
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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
/***************************************************************************************
NAME : Karthikeyan.V
DATE : 8.08.2021
DESCRIPTION : To take 8 consecutive bytes in memory. Provide a menu to manipulate or
display the value of variable of type t (input) & indexed at i (i/p)
OUTPUT :
./a.out
MENU:
1.ADD ELEMENT
2.REMOVE ELEMENT
3.DISPLAY ELEMENT
4.EXIT FROM THE PROGRAM
CHOICE-->1
ENTER THE TYPE YOU HAVE TO INSERT:
1.INT
2.CHAR
3.SHORT
4.FLOAT
5.DOUBLE
CHOICE-->1
ENTER THE INTEGER :5
MENU:
1.ADD ELEMENT
2.REMOVE ELEMENT
3.DISPLAY ELEMENT
4.EXIT FROM THE PROGRAM
CHOICE-->1
ENTER THE TYPE YOU HAVE TO INSERT:
1.INT
2.CHAR
3.SHORT
4.FLOAT
5.DOUBLE
CHOICE-->4
THE MEMORY IS OCCUPIED
MENU:
1.ADD ELEMENT
2.REMOVE ELEMENT
3.DISPLAY ELEMENT
4.EXIT FROM THE PROGRAM
CHOICE-->1
ENTER THE TYPE YOU HAVE TO INSERT:
1.INT
2.CHAR
3.SHORT
4.FLOAT
5.DOUBLE
CHOICE-->2
ENTER A CHARACTER : a
MENU:
1.ADD ELEMENT
2.REMOVE ELEMENT
3.DISPLAY ELEMENT
4.EXIT FROM THE PROGRAM
CHOICE-->1
ENTER THE TYPE YOU HAVE TO INSERT:
1.INT
2.CHAR
3.SHORT
4.FLOAT
5.DOUBLE
CHOICE-->2
ENTER A CHARACTER : f
MENU:
1.ADD ELEMENT
2.REMOVE ELEMENT
3.DISPLAY ELEMENT
4.EXIT FROM THE PROGRAM
CHOICE-->2
0 --> a
1 --> f
2 --> 5
ENTER THE INDEX VALUE TO BE DELETED: 0
MEMORY AT INDEX 0 SUCCESSFULLY DELETED
MENU:
1.ADD ELEMENT
2.REMOVE ELEMENT
3.DISPLAY ELEMENT
4.EXIT FROM THE PROGRAM
CHOICE-->3
---------------------------------------------
0 --> f (char)
1 --> 5 (int)
---------------------------------------------
MENU:
1.ADD ELEMENT
2.REMOVE ELEMENT
3.DISPLAY ELEMENT
4.EXIT FROM THE PROGRAM
CHOICE-->4
EXITING PROGRAM
***************************************************************************************/
#include<stdio.h>
#include<stdlib.h>
#include<stdio_ext.h>
int main()
{
int index[5];
int n1, n2;
int char_flag = 0, int_flag = 0,float_flag = 0, short_flag = 0, double_flag = 0, int_index;
void *ptr;
ptr = malloc(2 * sizeof(int));
while(1)
{
printf("\nMENU:\n1.ADD ELEMENT\n2.REMOVE ELEMENT\n3.DISPLAY ELEMENT\n4.EXIT FROM THE PROGRAM\nCHOICE-->");
__fpurge(stdin);
scanf("%d", &n1);
switch(n1)
{
/*checking the flags and add new element*/
case 1:
printf("\nENTER THE TYPE YOU HAVE TO INSERT: ");
printf("\n1.INT\n2.CHAR\n3.SHORT\n4.FLOAT\n5.DOUBLE\nCHOICE-->");
__fpurge(stdin);
scanf("%d", &n2);
switch(n2)
{
case 1:
if(int_flag == 0 && float_flag == 0 && double_flag == 0)
{
int_flag++;
printf("\nENTER THE INTEGER :");
scanf("%d",(((int *)ptr)+1));
}
else
printf("\nTHE MEMORY IS OCCUPIED");
break;
case 2:
if(char_flag < 2 && double_flag == 0)
{
printf("\nENTER A CHARACTER : ");
__fpurge(stdin);
scanf("%c",((char *)ptr)+char_flag);
char_flag++;
}
else
printf("\nTHE MEMORY IS OCCUPIED");
break;
case 3:
if(short_flag == 0 && double_flag == 0)
{
printf("\nENTER THE SHORT: ");
scanf("%hd",(((short *)ptr)+1));
short_flag++;
}
else
printf("\nTHE MEMORY IS OCCUPIED");
break;
case 4:
if(int_flag == 0 && float_flag == 0 && double_flag == 0)
{
printf("\nENTER THE FLOAT: ");
scanf("%f",(((float *)ptr)+1));
float_flag++;
}
else
printf("\nTHE MEMORY IS OCCUPIED");
break;
case 5:
if(char_flag == 0 && int_flag ==0 && float_flag == 0 && short_flag == 0 && double_flag==0)
{
printf("\nENTER THE DOUBLE");
__fpurge(stdin);
scanf("%lf",(((double *)ptr)));
double_flag++;
}
else
printf("\nTHE MEMORY IS OCCUPIED");
break;
default:
printf("\nERROR : THE SELECTED OPTION IS NOT VALID\n");
break;
}
break;
case 2:
/*display indices and remove element*/
int_index = 0;
if(char_flag > 0)
{
printf("%d --> %c\n",int_index, *((char *)ptr));
index[int_index++] = 1;
if(char_flag > 1)
{
printf("%d --> %c\n",int_index, *(((char *)ptr)+1));
index[int_index++] = 1;
}
}
if(short_flag == 1)
{
printf("%d --> %hd\n",int_index, *(((short *)ptr)+1));
index[int_index++] = 2;
}
if(int_flag == 1)
{
printf("%d --> %d\n",int_index, *(((int *)ptr)+1));
index[int_index++] = 3;
}
if(float_flag == 1)
{
printf("%d --> %g\n",int_index, *(((float *)ptr)+1));
index[int_index++] = 4;
}
if(double_flag == 1)
{
printf("%d --> %g\n",int_index, *((double *)ptr));
index[int_index++] = 5;
}
if(char_flag == 0 && int_flag ==0 && float_flag == 0 && short_flag == 0 && double_flag==0)
printf("\nNO SPACE IS OCCUPIED\n");
else
{
printf("\nENTER THE INDEX VALUE TO BE DELETED: ");
scanf(" %d",&int_index);
switch(index[int_index])
{
case 1:
if(char_flag > 0)
{
if(char_flag > 1 && int_index == 0)
{
*((char *)ptr) = *(((char *)ptr) +1);
}
char_flag--;
printf("\nMEMORY AT INDEX %d SUCCESSFULLY DELETED\n",int_index);
}
else
printf("\nMEMORY ALREADY FREED\n");
break;
case 2:
if(short_flag == 1)
{
short_flag--;
printf("\nMEMORY AT INDEX %d SUCCESSFULLY DELETED\n",int_index);
}
else
printf("\nMEMORY ALREADY FREED\n");
break;
case 3:
if(int_flag == 1)
{
int_flag--;
printf("\nMEMORY AT INDEX %d SUCCESSFULLY DELETED\n",int_index);
}
else
printf("\nMEMORY ALREADY FREED\n");
break;
case 4:
if(float_flag == 1)
{
float_flag--;
printf("\nMEMORY AT INDEX %d SUCCESSFULLY DELETED\n",int_index);
}
else
printf("\nMEMORY ALREADY FREED\n");
break;
case 5:
if(double_flag == 1)
{
double_flag--;
printf("\nMEMORY AT INDEX %d SUCCESSFULLY DELETED\n",int_index);
}
else
printf("\nMEMORY ALREADY FREED\n");
break;
default:
printf("\nINVALID OPTION\n");
break;
}
}
break;
case 3:
/*display elements*/
int_index = 0;
printf("\n---------------------------------------------");
if(char_flag == 0 && int_flag ==0 && float_flag == 0 && short_flag == 0 && double_flag==0)
printf("\nNO SPACE IS OCCUPIED\n");
else
{
if(char_flag > 0)
{
printf("\n%d --> %c (char)",int_index++, *((char *)ptr));
if(char_flag > 1)
{
printf("\n%d --> %c (char)",int_index++, *(((char *)ptr)+1));
}
}
if(short_flag == 1)
{
printf("\n%d --> %hd (short)",int_index++, *(((short *)ptr)+1));
}
if(int_flag == 1)
{
printf("\n%d --> %d (int)",int_index++, *(((int *)ptr)+1));
}
if(float_flag == 1)
{
printf("\n%d --> %g (float)",int_index++, *(((float *)ptr)+1));
}
if(double_flag == 1)
{
printf("\n%d --> %g (double)\n",int_index++, *((double *)ptr));
}
}
printf("\n---------------------------------------------");
break;
case 4:
/*free pointer and exit*/
free(ptr);
printf("\nEXITING PROGRAM\n");
exit(1);
break;
case 5:
printf("\nINVALID OPTION\n");
break;
}
}
return 0;
}