Preview:
#include <stdio.h>

// Function to insert element
void insert(int arr[], int *n, int pos, int value) {
    int i;
    if (pos < 1 || pos > *n + 1) {
        printf("Invalid position!\n");
        return;
    }

    for (i = *n; i >= pos; i--) {
        arr[i] = arr[i - 1];
    }

    arr[pos - 1] = value;
    (*n)++;
    printf("Element inserted successfully.\n");
}

// Function to delete element
void deleteElement(int arr[], int *n, int pos) {
    int i;
    if (pos < 1 || pos > *n) {
        printf("Invalid position!\n");
        return;
    }

    for (i = pos - 1; i < *n - 1; i++) {
        arr[i] = arr[i + 1];
    }

    (*n)--;
    printf("Element deleted successfully.\n");
}

// Function to display array
void display(int arr[], int n) {
    int i;
    if (n == 0) {
        printf("Array is empty.\n");
        return;
    }

    printf("Array elements: ");
    for (i = 0; i < n; i++) {
        printf("%d ", arr[i]);
    }
    printf("\n");
}

int main() {
    int arr[100], n = 0, choice, pos, value, i;

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

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

    do {
        printf("\n--- MENU ---\n");
        printf("1. Insert\n");
        printf("2. Delete\n");
        printf("3. Display\n");
        printf("4. Exit\n");
        printf("Enter your choice: ");
        scanf("%d", &choice);

        switch (choice) {
            case 1:
                printf("Enter position: ");
                scanf("%d", &pos);
                printf("Enter value: ");
                scanf("%d", &value);
                insert(arr, &n, pos, value);
                break;

            case 2:
                printf("Enter position: ");
                scanf("%d", &pos);
                deleteElement(arr, &n, pos);
                break;

            case 3:
                display(arr, n);
                break;

            case 4:
                printf("Exiting program...\n");
                break;

            default:
                printf("Invalid choice!\n");
        }

    } while (choice != 4);

    return 0;
}
downloadDownload PNG downloadDownload JPEG downloadDownload SVG

Tip: You can change the style, width & colours of the snippet with the inspect tool before clicking Download!

Click to optimize width for Twitter