Home > Net >  Error in code for inserting an element in an array
Error in code for inserting an element in an array

Time:12-15

Here I have written a code that accepts an array of any size suitable to the user from a user agrees with a place to insert a new integer in the collection and coordinates with the integer to be inserted.

#include <stdio.h>
#include <stdlib.h>

void insert(int barray_size, int *A, int new_place, int new_int)
{
    for (int i = barray_size; i >= new_place; i = i - 1)
    {
        *(A   i) = *(A   i - 1); //copies value of cell A[i-1] to cell A[i]
        *(A   i - 1) = 0; //empties cell A[i-1] so that it can take the value A[i-2] in next loop
    }

    *(A   new_place - 1) = new_int; //putting the required integer at A[k]
}

int main()
{
    int n, k, l;
    printf("Please enter array size: ");
    scanf("%d", &n);
    printf("\n");
    int A[n   1];

    for (int i = 0; i < n; i  )
    {
        int temp;
        printf("Enter [%d] element: ", i);
        scanf("%d", &temp);
        A[i] = temp;
        printf("\n");
    }

    A[n] = 0;
    printf("Enter the place where new integer is to be inserted: ");
    scanf("%d", &k);

    if (k < n   1)
    {
        printf("Enter the integer to be inserted: ");
        scanf("%d", &l);
        int t = n   1;
        insert(t, A, k, l);
        printf("The new array is: ");

        for (int i = 0; i < t;   i)
        {
            printf("%d ", A[i]);
        }
    }
    else
    {
        printf("Please enter a valid address.");
    }
}

But when I run this code, it terminates after accepting place for integer but does not accept the integer to be inserted. Would you mind helping in detecting the error?

THANK YOU

Edit 1: Corrected the trivial error of address in scanf.

Now the program more or less adds the integer in the array properly (although at place k 1 if requested at k, but that can be corrected). But it prints all the values of the array beyond A[i 1] with garbage values, despite using the for loop in the end which should have stopped printing the loop at A[n 1]. Why is this happening?

(I apologize if my doubts are trivial or silly. I have started learning C language very recently and due to lack of practice, I am prone to such trivial errors and require help for clarification of such doubts while practicing, which I seek at SO)

Edit 2: After initializing A[n] = 0, which was missing before, the program works perfectly and terminates properly.

Thanks to everybody for helping out.

CodePudding user response:

You forget to specify the address of the variable k and l in scanf and your

Change:

scanf("%d", k);
scanf("%d", l);

To:

scanf("%d", &k);
scanf("%d", &l);

Change:

for (int i = 0; i < n 1; i  )

To:

for (int i = 0; i < t; i  )

Here down is modified code:

#include <stdio.h>
#include <stdlib.h>

void insert(int barray_size, int *A, int new_place, int new_int)
{
    for(int i=barray_size; i>new_place; i=i-1)
    {
        *(A i)=*(A i-1);
        *(A i-1)=0; 
    }
    *(A new_place)=new_int; 
}

int main()
{
    int n, k, l;
    printf("Please enter array size: ");
    scanf("%d", &n);
    printf("\n");
    int A[n - 1];
    for (int i=0; i<n; i  )
    {
        int temp;
        printf("Enter [%d] element: ", i);
        scanf("%d", &temp);
        A[i]=temp;
        printf("\n");
    }
    
    printf("Enter the place where new integer is to be inserted: ");
    scanf("%d", &k);
    
    if(k < n)
    {
        printf("Enter the integer to be inserted: ");
        scanf("%d", &l);
        int t=n 1;
        insert(t, A, k, l);
        printf("The new array is: ");
        for (int i = 0; i < t; i  )
        {
            printf("%d ", A[i]);
        }
    }
    else
    {
        printf("Please enter a valid address.");
    }
}

Output

Please enter array size: 7

Enter [0] element: 1

Enter [1] element: 2

Enter [2] element: 3

Enter [3] element: 4

Enter [4] element: 5

Enter [5] element: 6

Enter [6] element: 7

Enter the place where new integer is to be inserted: 5
Enter the integer to be inserted: 10
The new array is: 1 2 3 4 5 10 6 7 

CodePudding user response:

In your code, when you do scanf("%d", ), you need to pass in the address of the variable. Not the variable itself. Can you try the following and see if it works:

int main()
{
    // CODE TO READ ARRAY AS YOU HAVE ALREADY WRITTEN
    printf(("Enter the place where new integer is to be inserted: ");
    scanf("%d", &k); // CHANGE HERE from k to &k
    printf("Enter the integer to be inserted: ");
    scanf("%d", &l); // CHANGE HERE FROM l to &l
    /* CHANGE HERE. 
    You don't need n 1 because when you create an array of size n 1, the indices are from 0 to n. 
    You are reading values only for 0 to n-1 in your loop.
    In your insert function, you are starting at i=n which is the last free space in the array.
    */
    insert(n, A, k, l)
    // REST OF YOUR CODE TO PRINT THE ARRAY
}
  • Related