Index « Previous Next »

Question

Write a program to delete an element of an one dimensional array.

Source Code

#include <stdio.h>

int main()
{
    int list[100], position, i, n;

    printf("Enter the number of elements in array : ");
    scanf("%d", &n);

    printf("Enter %d elements : ", n);

    for (i = 0; i < n; i++)
	{
        scanf("%d", &list[i]);
	}

    printf("Enter the location of element to be deleted : ");
    scanf("%d", &position);

    if (position >= n + 1)
    {
        printf("Location number is bigger then array size !!");
    }
    else
    {
        for (i = position - 1; i < n - 1; i++)
        {
            list[i] = list[i + 1];
        }

        printf("Resultant array is\n");

        for (i = 0; i < n - 1; i++)
        {
            printf("%d ", list[i]);
        }
    }

    return 0;
}

Output

Enter the number of elements in array : 5
Enter 5 elements : 10 15 5 20 30
Enter the location of element to be deleted : 3
Resultant array is
10 15 20 30