-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathprogram1.c
More file actions
85 lines (71 loc) · 1.45 KB
/
program1.c
File metadata and controls
85 lines (71 loc) · 1.45 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
#include<stdio.h>
int a[20],n,ele,i,pos;
void create()
//creating an array
{
printf("\nEnter the size of the array elements:\t");
scanf("%d",&n);
printf("\nEnter the elements for the array:\n");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
}
void display() //display array elements
{
printf("\nThe array elements are: \n");
for(i=0;i<n;i++)
printf("%d \t",a[i]);
}
void insert_ele() //inserting an element at specified position
{
printf("\nEnter the position for the new element:\t");
scanf("%d",&pos);
printf("\nEnter the element to be inserted :\t");
scanf("%d",&ele);
if(pos> (n+1)||pos<=0)
{
printf("invalid position\n");
return;
}
for(i=n-1; i>=pos-1; i--)
a[i+1]=a[i];
a[pos-1]=ele;
n++;
}
void delete_ele() //deleting an array element
{
printf("\nEnter the position of the element to be deleted:\t");
scanf("%d",&pos);
if(pos>=n+1||pos<=0)
{
printf("invalid position\n");
return;
}
printf("element deleted is %d", a[pos-1]);
for(i=pos-1; i<n-1; i++)
a[i]=a[i+1];
n--;
}
void main()
{
int ch;
do
{
printf(" \n -------- MENU ----------- \n");
printf("1:CREATE 2:DISPLAY 3:INSERT 4:DELETE\n");
printf("-----------------------");
printf("\nENTER YOUR CHOICE:\t");
scanf("%d",&ch);
switch(ch)
{
case 1: create();
break;
case 2: display();
break;
case 3: insert_ele();
break;
case 4: delete_ele();
break;
default: printf("invalid choice\n");
}
} while(ch<=4);
}