Array Operationn
Array Operationn
h>
#include<stdlib.h>
int a[100], i, choice, element, position, n, deleted;
void create ()
{
printf ("enter the size\n");
scanf ("%d", &n);
printf ("enter the elements\n");
for (i = 0; i < n; i++)
scanf ("%d", &a[i]);
}
void display ()
{
printf ("array elements:\n");
for (i = 0; i < n; i++)
printf ("%d\t", a[i]);
}
void insert ()
{
printf ("enter the position\n");
scanf ("%d", &position);
if (position < 0 || position >= n)
printf ("invalid\n");
else
{
printf("enter the element\n");
scanf("%d",&element);
for(i=n-1;i>=position;i--)
a[i+1]=a[i];
a[position]=element;
n=n+1;
}
}
void delete()
{
printf ("enter the position\n");
scanf ("%d", &position);
deleted=a[position];
if (position < 0 || position >= n)
printf ("invalid\n");
else
{
for(i=position;i<n-1;i++)
a[i]=a[i+1];
n=n-1;
printf("deleted element is %d",deleted);
}
}
void main()
{
while(1)
{
printf("\nMENU\n1.create\t2.display\t3.insert\t4.deletet\t5,exit\n");
printf("enter your choice\n");
scanf("%d",&choice);
switch(choice)
{
case 1:create();
break;
case 2:display();
break;
case 3:insert();
break;
case 4:delete();
break;
case 5:exit(0);
default:printf("invalid\n");
}
}
}