27. Array Operation(insertion/deletion) in C
#include<stdio.h>
int main(){
int arr[10];
int insertValue;
int position;
int size;
printf("Enter the size of array:\n");
scanf("%d",&size);
if(size>10)
{
printf("Memory Overflow");
}
else
{
printf("Enter the array value :");
for(int i=0; i<size; i++)
{
scanf("%d",&arr[i]);
}
printf("\nThe values of array are :\n");//traversal
for(int i=0; i<size; i++)
{
printf(" %d|",arr[i]);
}
//insert any value in between unsorted array
printf("\nEnter the position of array at which you wanted to insert :\n");
scanf("%d",&position);
printf("\nEnter the value u wanted to insert in the array :\n");
scanf("%d",&insertValue);
if(position>size+1 || position<1)
{
printf("Inappropriate psition !!!");
}
else{
/* //this algo is used for unsorted array
arr[size]= arr[position-1];
arr[position-1]=insertValue;
size++;*/
for(int i=size; i>=position-1; i--)//this is for sorted array
{
arr[i]=arr[i-1];
}
arr[position-1]=insertValue;
size++;
printf("\nThe modified array is :\n");
for(int i=0; i<size; i++)
{
printf(" %d|",arr[i]);
}
}
printf("\nEnter the position at which you wanted to delete the value :");
scanf("%d",&position);
for(int i=position-1; i<=size; i++)
{
arr[i]=arr[i+1];
}size--;
printf("\nThe array after deletion is :\n");
for(int i=0; i<size; i++)
{
printf(" %d|",arr[i]);
}
}
return 0;
}
Comments
Post a Comment