Deletion of An Array Element

You might also like

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 6

Algorithm To Insert An Element In An Array

1. Read the value of n i.e. the size of an array. 2. Read the elements of array 3. Read the value of pos i.e. the position where you want to insert the element. 4. Read the value of value which is to be inserted. 5. Now after the position where you want to insert the value increase the address of array element by one and shift the element value by one. 6. Now insert the value to the desired place. 7. Now print the resultant element.

Insertion Of An Array Element


#include<stdio.h> #include<conio.h> void main() { int a[100], pos, i, n, value; clrscr(); printf("\n Enter the number of elements in array"); scanf("%d",&n); printf("\n Enter Elements of array"); for (i=0; i<n; i++) { scanf("%d",&a[i]); } printf("\n Enter the location where you wish to insert an element"); scanf("%d",&pos); printf("\n Enter the value to insert"); scanf("%d",&value); for (i=n; i>=pos-1; i--) { a[i+1]=a[i]; } a[pos-1]=value; printf("\n The resultant Array"); for (i=0; i<n+1; i++)

{ printf("\n%d",a[i]); } getch(); }

OUTPUT
Enter the number of elements in array 6 Enter Elements of array 3 5 6 9 7 8 Enter the location where you wish to insert an element 3 Enter the value to insert 8 The resultant Array 3 5 8 6 9 7 8

Algorithm To Insert An Element In An Array

1. Read the value of n i.e. the size of an array. 2. Read the elements of array 3. Read the value of pos i.e. the position of element which you wish to delete. 4. Now after the position where you want to delete the value decrease the address of array element by one and shift the element value by one. 5. Now print the resultant element.

Deletion Of An Array Element


#include<stdio.h> #include<conio.h> void main() { int a[100], pos, i, n; clrscr(); printf("\n Enter the number of elements in array"); scanf("%d",&n); printf("\n Enter Elements of array"); for (i=0; i<n; i++) { scanf("%d",&a[i]); } printf("\n Enter the location which you wish to delete"); scanf("%d",&pos); if (pos>=n+1) { printf("\n Deletion not possible"); } else { for (i=pos-1; i<n-1; i++) { a[i]=a[i+1];

} printf("\n Resultant array"); } for (i=0; i<n-1; i++) { printf("\n%d",a[i]); } getch(); }

OUTPUT
Enter the number of elements in array 5 Enter Elements of array 3 5 6 9 7 Enter the location which you wish to delete 3 The resultant Array 3 5 9 7 8

You might also like