-1

When inserting into an array, we know the index starts from 0. So, if we want to insert an element at position 3, we've to enter the input at position 2. For readability I wanted to give the proper location, i.e., position 3 means at exact 3, not 2.

Here is the code snippet.

printf("In which position you want to enter the element? ");
scanf("%d",&k);

for (j=n; j>=k; j--)
{
    array[j+1]=array[j];
}

printf("Which element do you want to insert? ");
scanf("%d", &item);

array[k]=item;

n++;

Sample output:

How many elements? 5
Enter the values
1
2
4
5
6
In which position you want to enter the element? 2
Which element do you want to insert? 3
After insertion the array is:
1
2
3
4
5
6

I want the position to be at 3.

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
Imbecile
  • 13
  • 3
  • 1
    `for (j=n;` looks wrong to me if n is the number of elements in the array, meaning `n-1` is the last actually populated element, but then as long as n+1 is inside the array bounds that's probably fine. – Rup May 14 '19 at 17:07
  • 5
    If you `scanf` into k, and you want the human entered `3` to reference index `2`, you can try subtracting 1. – William Pursell May 14 '19 at 17:09
  • 1
    If you have 3 elements in the array, then picking an element in the range 0...3 could be used to insert the new element in one of *four* positions, which is the index of where the new element will be. – Weather Vane May 14 '19 at 17:10
  • 1
    Array indices start with 0. If you want the "position 1" correspond ti index 0, you need to subtract 1 from the entered position. – n. m. could be an AI May 14 '19 at 17:11
  • 2
    Without seeing an [mcve], all we can do is guess. – user3386109 May 14 '19 at 17:11
  • 1
    you can ignore element zero – 0___________ May 14 '19 at 17:16
  • I've tried n-1, it populates the same result. @Rup – Imbecile May 14 '19 at 17:16
  • 2
    There is no question in your question. – Eric Postpischil May 14 '19 at 17:17
  • Then what will be the case if I insert it at first position? @P__J__ – Imbecile May 14 '19 at 17:18
  • 1
    If you want to use array index 2 when the user enters 3, then, after reading the number the user enters, subtract 1 from it. – Eric Postpischil May 14 '19 at 17:19
  • yeah as P__J__ pointed, you could ignore the position 0. and start filling the array from index 1. the first position would be array[1]. However you have to create an array with size = n+1 for this to work, n is nos of max elements. – rsonx May 14 '19 at 17:23
  • 1
    After you figure out your logic, you might want to look at [using memmove to shift array elements](https://stackoverflow.com/a/9041810/8767209) – MFisherKDX May 14 '19 at 17:27
  • Yeah great, that's a nice solution @ Eric Postpischil. But Is it the best solution we can think of? – Imbecile May 14 '19 at 17:31

1 Answers1

0

This code should work.

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

int main(void) {
    int *array;

    int size = 0;

    int position, value;

    printf("How many elements do you want to add? ");
    scanf("%d", &size);

    printf("\nEnter the values: \n");

    // allocate the array
    array = malloc(size * sizeof(int));

    // insert the elements
    for(int i = 0; i < size; i++) {
      scanf("%d", &array[i]);
    }

    // print the array
    for(int i = 0; i < size; i++) {
      printf("%d ", array[i]);
    }
    printf("\n");

    // read the position
    printf("In which position you want to enter the element? ");
    scanf("%d",&position);

    // resize the array
    size++;
    array = realloc(array, size * sizeof(int));

    // set the position to the true value
    position--;

    // read the value
    printf("Which element do you want to insert? ");
    scanf("%d", &value);

    // move the elements
    for(int i = size - 1; i > position; i--) {
      array[i] = array[i - 1];
    }

    // insert the array
    array[position] = value;

    // print the value
    for(int i = 0; i < size; i++) {
      printf("%d ", array[i]);
    }
    printf("\n");
}

Of course you should implement some error handling. Especially for the alloc.

MFisherKDX
  • 2,840
  • 3
  • 14
  • 25
Moschte
  • 102
  • 1
  • 11
  • yeah but his sample output looks like he shifts back all the values – Moschte May 14 '19 at 17:42
  • @ Moschte, This works fine. But is there any other way without forcefully decrements the position? Say, in the loop where we are inserting the value. – Imbecile May 14 '19 at 17:43
  • I think it's also possible to say in the loop for(; i > position-1;) but then you also need to change the code to array[position - 1 ] = value – Moschte May 14 '19 at 17:45
  • 1
    That I was looking for. Thanks. @Moschte – Imbecile May 14 '19 at 19:04