C Program to Delete an Element From array at desired position :There we will see how to delete an element in array from specified location.
First we have to create an array of finite number of element and input the element in the array from user.
arr[0]= 4 arr[1]= 7 arr[2]= 3 arr[3]= 9 arr[4]= 6Now we choose the index position which is we want to delete. we take input from user which is 2.
AFTER DELETION: arr[0]= 4 arr[1]= 3 arr[2]= 9 arr[3]= 6Lets have a look on Algorithm and programming code
ALGORITHM:- step 1 : start step 2 : Enter Size of array step 3 : Enter element from user in array step 4 : Now we take an index value which is we want to delete step 5 : If index value greater then size of array then show th msg "delete not possible" step 6 : Else perform the deletion step 7 : display the array element after deletion step 8 : Stop execution
PROGRAM CODE:- #include<stdio.h> #include<conio.h> void main() { //there we take an array with 20 index int array[20],i,index,n; printf("Enter size of array: "); scanf("%d", &n); printf("Enter Element in the array\n", n); for ( i = 0 ; i < n ; i++ ) { scanf("%d", &array[i]); } printf("Enter index value which is you want to delete\n"); scanf("%d", &index); if ( index >= n+1 ) { printf("There in not Inputed index value\n"); } else { for ( i = index - 1 ; i < n - 1 ; i++ ) { array[i] = array[i+1]; } printf("Array after deletion: \n"); for( i = 0 ; i < n-1 ; i++ ) { printf("%d\n", array[i]); } } getch(); }
OUTPUT:- Enter the size of array: 5 Enter Element in array: 5 1 9 5 4 Enter index value which is you want to delete: 2 Array after deletion: 5 9 5 4
C Program to delete an existing element from array How to delete an element from array Delete element in array from specified location
ConversionConversion EmoticonEmoticon