delete an element in array

        Delete an element from an array





delete an element from an array means to remove an existing element from an array

variables

1. arr = name of the array

2. size = size of the array

3. i= loop counter

4. pos = the position from where we want to delete the element

Algorithm

step 1. Start

step 2. set i=pos-1

step 3. repeat step 4 and 5 for i =pos-1 to i<size

step 4. set arr[i]=arr[i+1]

step 5. set i=i+1

step 6.end of the loop

step 7. set size=size-1

step 8. stop

Implementation


#include<stdio.h>

int main()

{

    int i,size,pos,n;


    int arr[5];

    printf("\n enter the size of array");

    scanf("%d",&n);

    {

        printf("\n enter %d array elements:",n);

        for(i=0;i<n;i++)

            scanf("%d",&arr[i]);

    }

    size=sizeof(arr)/sizeof(arr[0]);

    printf("\n the aray elements before deletation");

    for(i=0;i<size;i++)

        printf("\n arr[%d]=%d",i,arr[i]);

    printf("\n enter the position you want to delete");

    scanf("%d",&pos);

    printf("\n the array elements after deletation");

    for(i=pos-1;i<size;i++)

        arr[i]=arr[i+1];

    size=size-1;

    for(i=0;i<size;i++)

        printf("\n arr[%d]=%d",i,arr[i]);


}

output


 enter the size of array4


 enter 4 array elements:5

6

7

8


 the aray elements before deletation

 arr[0]=5

 arr[1]=6

 arr[2]=7

 arr[3]=8

 arr[4]=8

 enter the position you want to delete2


 the array elements after deletation

 arr[0]=5

 arr[1]=7

 arr[2]=8

 arr[3]=8

Process returned 0 (0x0)   execution time : 11.474 s

Press any key to continue.



Post a Comment

0 Comments