In array we hold elements. Now in programming some time you may need to add new value in array and delete old value from array. We can do that by some simple coding. But first we need to know insertion and deletion of new value in an array.
When we want to insert new element in array first we need to clear the position of array where we want to add new element. Then shift other element at the right side of target position by one to the right. Then add new element to that target position. For example
25 35 45 87 99
We want to insert new element at position 3, So first we need to shift other element at the right side of target position by 1 to the right.
25 35 _ 45 87 99
Now we put new element at target position, 79 is our new element
25 35 79 45 87 99
Now, for deletion of old element first we find the position of that old element. Then we need to shift other element at the right side of target position by 1 to the left. The element will deleted automatically. For example
25 35 45 87 99
Suppose we want to delete 45 then just move other element to the left by 1, for this case 87 and 99 at the right side of target position. So then move to the left of their position by 1. Then it become
25 35 87 99
Now let’s write the program
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
int arr[100],val,pos,i,p=0,length,ch,count=0,j;
int insert(int);
int deletion(int);
int display(int);
void main()
{
printf("enter the length of the arry:(<=100)");
scanf("%d",&length);
if(length<=100)
{
printf("length successfully entered.And the legth is:%d \n",length);
}
else
{
printf("length is invalid.\n");
main();
}
printf("Enter the elments of the array:\n");
for(i=0;i<length;i++)
{
printf("\n%d position's:",p++);
scanf("%d",&arr[i]);
}
length--;
display(length);
while(1)
{
printf("\nChose your option:1:insertion 2:deletion 3:display 4:Exit\n");
scanf("%d",&ch);
switch(ch)
{
case 1:
printf("enter the position you want to insert a value(position should be counted from 0):");
scanf("%d",&pos);
insert(pos);
break;
case 2:
printf("Enter the value,which you want to delete:");
scanf("%d",&val);
deletion(val);
break;
case 3:
display(length);
break;
case 4:
exit(0);
default:
printf("Wrong choice....Pls Enter A valid entity...");
switch(ch);
}
}getch();
}
int display(int length)
{
printf("\nNow the array is:\n");
for(i=0;i<=length;i++)
{
printf("%d\t",arr[i]);
}
}
int insert(int pos)
{
if(pos<=length+1)
{
if(pos<=100 && length<=100)
{
printf("Enter the value you want to insert:");
scanf("%d",&val);
length++;
for(i=length;i>pos;i--)
{
arr[i]=arr[i-1];
}
arr[pos]=val;
}
display(length);
}
else
{
printf("Invalid position or length...");
switch(ch);
}
}
int deletion(int val)
{
for(i=0;i<=length;i++)
{
if(arr[i]==val)
{
j=i;
while(j<=length)
{
arr[j]=arr[j+1];
j++;
}
length--;
count++;
i--;
}
}
if(count!=0)
{
printf("\n%d value(s) have deleted",count);
}
else
{
printf("you have entered a wrong value");
}
count=0;
display(length);
switch(ch);
}
In above program we use switch-case for insertion and deletion operation. We can do those operation many times. Now let’s see the output
enter the length of the arry:(<=100)5
length successfully entered.And the legth is:5
Enter the elments of the array:
0 position's: 55
1 position's: 12
2 position's: 89
3 position's: 36
4 position's: 41
Now the array is:
55 12 89 36 41
Chose your option:1:insertion 2:deletion 3:display 4:Exit
1
enter the position you want to insert a value(position should be counted from 0):2
Enter the value,which you want to delete:75
Now the array is:
55 12 75 89 36 41
Chose your option:1:insertion 2:deletion 3:display 4:Exit
2
Enter the value,which you want to delete: 36
1 value(s) have deleted
Now the array is:
55 12 75 89 41
Chose your option:1:insertion 2:deletion 3:display 4:Exit
3
Now the array is:
55 12 75 89 41
Chose your option:1:insertion 2:deletion 3:display 4:Exit
4