Write code to delete data from an array from a particular position?

Long Answer
Views 563

Answer:


 
#include <stdio.h>

int main()
{
    int arr[100], n, pos, i;

    // Read size of array
    printf("Enter number of elements: ");
    scanf("%d", &n);

    // Read array elements
    printf("Enter array elements:\n");
    for (i = 0; i < n; i++) {
        scanf("%d", &arr[i]);
    }

    // Read position
    printf("Enter position to delete (1 to %d): ", n);
    scanf("%d", &pos);

    // Check valid position
    if (pos < 1 || pos > n) {
        printf("Invalid position!");
        return 0;
    }

    // Shift elements to the left
    for (i = pos - 1; i < n - 1; i++) {
        arr[i] = arr[i + 1];
    }

    n--; // Reduce array size

    // Print updated array
    printf("Array after deletion:\n");
    for (i = 0; i < n; i++) {
        printf("%d ", arr[i]);
    }

    return 0;
}



Related Articles:

This section is dedicated exclusively to Questions & Answers. For an in-depth exploration of Data Structure, click the links and dive deeper into this subject.

Join Our telegram group to ask Questions

Click below button to join our groups.