Lab

mail@pastecode.io avatar
unknown
plain_text
a year ago
1.2 kB
1
Indexable
Never
#include <stdio.h>

int main()
{
    int array[100], item, option, n=1, position;
    array[0] = 1;
    while(1){    
    printf("\n1.Insert Item");
    printf("\n2.Delete Item");
    printf("\n3.View Array");
    printf("\nAnswer: ");
    scanf("%d",&option);
    printf("\n");
    if(option==1){
        printf("Enter the element to insert item: ");
        scanf("%d",&item);

        printf("Enter the position: ");
        scanf("%d",&position);
        n++;
    
        // shift elements forward
        for (int i = n - 1; i >= position; i--)
            array[i] = array[i - 1];
    
        array[position - 1] = item;

    }
    else if (option==2)
    {
        int position;
        printf("\nEnter the position of the element to delete:\n");
        scanf("%d", &position);

        if (position >= n)
        printf("Deletion not possible.\n");
        else
        {
            for (int i = position - 1; i < n; i++)
                array[i] = array[i+1];
        }
        printf("Item Deleted Successfully.");
        n -= 1;
    }
    
    else if (option==3)
    {
        printf("\n\nArray: ");
        for(int i=0; i<n; i++)
        {
            printf("%d, ", array[i]);
        }
    }
    printf("\n");
    }


}