Sunday, February 8, 2015

C program to delete a data from an array and to shifting the rest

The c program given below will store some data in an array, later a data will be deleted from the array and finally left-shifting will be performed to rearrange the array.

After running the program, you will be asked to provide the limit. That means how many data you want to store into the array. After providing the limit, you will be asked to provide the data.

By the 10th line the limit will be stored into the integer type variable n and in the 16th line a for-loop has started to store the data into the array a[i]. In the for-loop there is a condition i<=n, that means the loop will continue till the condition be satisfied.
deleting data from array,left shifting data in array,working with array
#include<stdio.h>
void main(){
    //declaring array and variable
    int a[20], n, i, j;

    //displaying a message
    printf("Enter a limit: ");

    //getting input into the variable n
    scanf("%d",&n);

    //message to input data
    printf("\n\nEnter the data to store: ");

    //for loop to input data into the array
    for(i=1; i<=n; i++){
        scanf("%d",&a[i]);
    }

    //displaying a message
    printf("\n\nThe stored data is: ");

    //displaying the data from the array
    for(i=1; i<=n; i++){
        printf("%d, ",a[i]);
    }


    //displaying message
    printf("\n\nEnter the index of array you want to delete: ");
    scanf("%d",&j);

    a[j]=0;

    //displaying message
    printf("\n\nStored data after deleting: ");

    //displaying the data from the array
    for(i=1; i<=n; i++){
        printf("%d, ",a[i]);
    }

    //for loop for shifting
    for(i=j; i<=n-1; i++){
        a[i]=a[i+1];
    }

    //displaying message
    printf("\n\nStored data after shifting: ");
    for(i=1; i<n; i++){
        printf("%d, ",a[i]);
    }

    //printing two new line
    printf("\n\n");
}


No comments:

Post a Comment