forked from izzarzn/cselab
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path1_Array_Operation.c
64 lines (64 loc) · 1.48 KB
/
1_Array_Operation.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
#include<stdio.h>
#include<conio.h>
int a[4], n, elem, i, pos;
/*Function Prototype and Definitions*/
void create() //creating an array
{
printf("\nEnter the size of the array elements: "); scanf("%d", &n);
printf("\nEnter the elements for the array:\n");
for(i=0; i<n; i++)
scanf("%d", &a[i]);
}
void display()
{
int i;
printf("\nThe array elements are:\n");
for(i=0; i<n; i++)
{
printf("%d\t", a[i]);
}
}//end of display()
void insert() //inserting an element in to an array
{
printf("\nEnter the position for the new element:");
scanf("%d", &pos);
pos=pos-1;
printf("\nEnter the element to be inserted:"); scanf("%d", &elem);
for(i=n-1; i>=pos; i--)
{
a[i+1] = a[i];
}
a[pos] = elem; n = n+1;
}//end of insert()
void del() //deleting an array element
{
printf("\nEnter the position of the element to be deleted:"); scanf("%d", &pos);
pos=pos-1;
elem = a[pos]; for(i=pos; i<n-1; i++)
{
a[i] = a[i+1];
}
n = n-1;
printf("\nThe deleted element is = %d",elem); }//end of delete()
void main()
{
int ch;
clrscr();
do
{
printf("\n\n-------- Menu-----------\n");
printf("1.Create\n 2.Display\n 3.Insert\n 4.Delete\n 5.Exit\n");
printf("\nEnter your choice: ");
scanf(“%d”,&ch);
switch(ch)
{
case 1: create(); break;
case 2: display(); break;
case 3: insert(); break;
case 4: del(); break;
case 5: exit(0); break;
default: printf("\nInvalidchoice:\n"); break;
}
}while(ch!=5);
getch();
}