C Program to insert, delete and edit element in array with Algorithm

C Program to insert, delete and edit element in array with Algorithm blurnooz.com

C program for performing insert, delete and edit element. It’s easy to understand C program with Algorithm and Output Images. Download Link Available for End Of Article..

(A) To Insert Element:

Algorithm [INSERT ELEMENT IN ARRAY]

Step 1: [Read n, x, position]
        Read n, x, position
Step 2: [Initialization]
	temp<-n-1
Step 3: [Scan array for position] 	
	Repeat while(temp>=position)
        i)    a[temp+1]<-a[temp]
        ii)   temp<-temp-1
Step 4: [Write element]
	Write x
Step 5: [Increment]
        N<-n+1
Step 6: [Finished]
	Exit.

 

Program :-

#include......
#include......
void main()
{
	int a[10],n,x,pos,temp,i;
	clrscr();
	printf("Enter n:");
	scanf("%d",&n);
	printf("Enter x:");
	scanf("%d",&x);
	printf("Enter position:");
	scanf("%d",&pos);
	printf("\nEnter element in array:\n");
	for(i=0;i<n;i++) 
        { 
                printf("Element %d:",i);
                scanf("%d",&a[i]); 
        } 
        temp=n-1;
        while(temp>=pos)
	{
		a[temp+1]=a[temp];
		temp=temp-1;
	}
	a[pos]=x;
	n=n+1;
	printf("\nDisplay array:\n");
	for(i=0;i<n;i++)
	{
		printf("%d,",a[i]);
	}
	getch();
}
Output :-

Enter n:5
Enter x:99
Enter position:2

Enter element in array:
Element 0:5
Element 1:6
Element 2:7
Element 3:8
Element 4:9

Display array:
5,6,99,7,8,9

 

(B) To Delete Element: 

Algorithm [DELETE ELEMENT IN ARRAY]

Step 1: [Read n, x, position]
         Read n, x, position
Step 2: [Store value from position to x]
         X<-position
Step 3: [Initialization] 	
         Temp<-position
Step 4: [Perform deletion]
         Repeat while(temp<n-1)
         a[temp]<-a[temp+1]
         temp<-temp+1
Step 5: [Display deleted element]
	 Write x
Step 6: [Finished]
         Exit

Program :-

#include.......
#include.......
void main()
{
	int a[10],n,x,pos,temp,i;
	clrscr();
	printf("Enter n:");
	scanf("%d",&n);
	printf("Enter position:");
	scanf("%d",&pos);
	printf("\nEnter element in array:\n");
	for(i=0;i<n;i++)
	{
		printf("Element %d:",i);
		scanf("%d",&a[i]);
	}
	x=a[pos];
	temp=pos;
	while(temp<n-1)
	{
		a[temp]=a[temp+1];
		temp=temp+1;
	}
	n=n-1;
	printf("\nDisplay array\n");
	for(i=0;i<n;i++)
	{
		printf("%d,",a[i]);
	}
	printf("\nDelete element:%d",x);
	getch();
}
Output :-

Enter n: 5
Enter position: 3

Enter element in array:
Element 0:1
Element 1:2
Element 2:3
Element 3:4
Element 4:5

Display array
1,2,3,5
Delete element:4

 

(C) To Edit Element:

Program : –

#include........
#include........
void main()
{
	int a[10],n,x,pos,i;
	clrscr();
	printf("Enter n:");
	scanf("%d",&n);
	printf("Enter x:");
	scanf("%d",&x);
	printf("Enter position:");
	scanf("%d",&pos);
	printf("\nEnter element in array:\n");
	for(i=0;i<n;i++)
	{
		printf("Element %d:",i);
		scanf("%d",&a[i]);
	}
	a[pos]=x;
	printf("\nDisplay array:\n");
	for(i=0;i<n;i++)
	{
		printf("%d,",a[i]);
	}
	getch();
}
Output :-

Enter n:5
Enter x:22
Enter position:3

Enter element in array:
Element 0:1
Element 1:2
Element 2:3
Element 3:4
Element 4:5

Display array:
1,2,3,22,5

Leave a Reply

Please enter your comment!
Please enter your name here

*