Search code examples
arrayscinsertion

Can anyone tell me why my program crashes when I try to call the Insert or Delete function in a menu driven array manipulation program?


I'm trying to write a menu driven program to perform actions like create, display, insert, and delete elements from an array. The create and Display functions work just fine but whenever I try to call the insert or delete function, the program crashes and the message reads Process returned. I've shared my code below. I realize that the create function is not ideal, but I've just been told to do it this way. i'm using Code::Blocks 20.03 if that's relevant.

#include <stdio.h>
#define MAX 50
void create(int[],int);
void display(int[],int);
void insert(int[],int*,int,int);
void deletes(int[],int*,int);

int main()
    {
        int ch,n,a[MAX],ele,pos;
        while(1)
        {
            printf("\nChoose an operation\n");
            printf("1. Create\n2. Display\n3. Insert\n4. Delete\n5. Exit\n\n");
            scanf("%d",&ch);

            switch(ch)
            {
                case 1: printf("Enter the no. of elements\n");
                scanf("%d",&n);
                create(a, n);
                break;

                case 2: display(a, n);
                break;

                case 3: printf("Enter the position at which you want to insert the element\n");
                scanf("%d",&pos);
                printf("Enter the element to be inserted\n");
                scanf("%d",&ele);
                insert(a, &n, pos, ele);
                break;

                case 4: printf("Enter the position of the element to be deleted\n");
                scanf("%d",&pos);
                deletes(a,&n,pos);
                break;

                case 5: exit(0);

                default: printf("Invalid Input");
                break;
            }
        }
    }

    void create(int a[], int n)
    {
        int temp;
        printf("Please enter the elements\n");
        for(int i=0; i<n ; i++)
        {
            scanf("%d",&temp);
            a[i] = temp;
        }
    }

    void display(int a[], int n)
    {
        printf("The array is \n");
        for(int i=0;i<n;i++)
           {
               printf("%d\t",a[i]);
           }
            printf("\n");
    }

    void insert(int a[], int*n, int pos, int ele)
    {
        if(n==MAX)
        {
            printf("Array Overflow. Cannot Insert element\n");
        }
        else if(pos>=0 && pos<=n)
        {
            for(int i=n-1; i>=pos; i--)
            {
                a[i+1] = a[i];
            }
            a[pos] = ele;
            n++;
            printf("Element inserted successfully\n");
        }
        else
        {
            printf("Enter a valid position\n");
        }
    }

    void deletes(int *a,int*n, int pos)
    {
        if(pos<=n)
        {
            for(int i = pos-1; i<n;i++)
            {
                a[i] = a[i+1];
            }
            n--;
            printf("The element has been deleted\n");
        }
        else
            printf("Invalid position");
    }

Thank you for your Help!


Solution

  • In your insert and deletes functions, you are not dereferencing the pointer (n) given as an argument. You need to add the * operator to this variable in order to get or set the actual value of the n counter.

    For the insert function, the corrected code is:

    void insert(int a[], int*n, int pos, int ele) // NOTE: "n" is a POINTER!
    {
        if(*n==MAX) // Dereference n here
        {
            printf("Array Overflow. Cannot Insert element\n");
        }
        else if(pos>=0 && pos<=*n) // ... and here
        {
            for(int i=*n-1; i>=pos; i--) // ... and here
            {
                a[i+1] = a[i];
            }
            a[pos] = ele;
            (*n)++; // Note the brackets here - otherwise we increment the pointer!
            printf("Element inserted successfully\n");
        }
        else
        {
            printf("Enter a valid position\n");
        }
    }
    

    The changes required to the deletes function are very similar.