-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path02_traditional_array.c
More file actions
108 lines (98 loc) · 2.56 KB
/
02_traditional_array.c
File metadata and controls
108 lines (98 loc) · 2.56 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
#include<stdio.h>
int insertElement(int arr[], int n, int loc)
{
int index;
if((loc > (n + 1)) || (loc <= 0))
printf("Invalid Location\n");
else if(n == 50)
printf("Array Overflow\n");
else
{
for(index = n - 1 ; index >= loc - 1 ; index--)
arr[index + 1] = arr[index];
printf("Enter Element : ");
scanf("%d", &arr[loc - 1]);
printf("Element Inserted Successfully\n");
n++;
}
return n;
}
int deleteElement(int arr[], int n, int loc)
{
int index;
if((loc > n) || (loc <= 0))
printf("Invalid Location\n");
else if(n == 0)
printf("Array Underflow\n");
else
{
for(index = loc - 1 ; index < n - 1 ; index++)
arr[index] = arr[index + 1];
printf("Element Deleted Successfully\n");
n--;
}
return n;
}
void display(int arr[], int n)
{
int index;
if(n == 0)
printf("Array Underflow\n");
else
{
printf("\nEntered Elements\n");
for(index = 0 ; index < n ; index++)
printf("Element %d : %d\n", index + 1, arr[index]);
}
}
int main()
{
int arr[50];
int num_of_elmnt, loc;
int choice, index;
int exit = 1;
printf("Enter Number of Elements : ");
scanf("%d", &num_of_elmnt);
if(num_of_elmnt > 50)
{
printf("Invalid Option");
return 1;
}
printf("\nEnter Elements\n");
for(index = 0 ; index < num_of_elmnt ; index++)
{
printf("Element %d : ", index + 1);
scanf("%d", &arr[index]);
}
while(exit != 0)
{
printf("\nEnter\n");
printf("1. To Insert an Element\n");
printf("2. To Delete an Element\n");
printf("3. To Display Array\n");
printf("0. To Exit\n");
printf("Enter Your Choice : ");
scanf("%d", &choice);
switch(choice)
{
case 0 : return 0;
case 1 :
{
printf("\nEnter Location where you want to Insert Element : ");
scanf("%d", &loc);
num_of_elmnt = insertElement(arr, num_of_elmnt, loc);
break;
}
case 2 :
{
printf("\nEnter Location where you want to Delete Element From : ");
scanf("%d", &loc);
num_of_elmnt = deleteElement(arr, num_of_elmnt, loc);
break;
}
case 3 : display(arr, num_of_elmnt); break;
default : printf("\nInvalid Choice, Please Try Again\n");
}
}
return 0;
}