4

I have an array of numbers that has been sorted in before, so there's no need to sort it, I need to insert an given value, named it val, at a valid position in my array.

My program works for a given value that is smaller than the last one, but for the case where the value is bigger than the last one, my program just doesn't want to insert the value.

For example, for the array {1, 2, 3, 4, 6} and the value 5, the array should be {1, 2, 3, 4, 5, 6}, but for the value 7 my array is looking like {1, 2, 7, 4, 6, 0}.

#include <stdio.h>

void insert(int val, int *n, int v[])
{
    int index;
    index = n - 1;
    if (n == 0)
    {
        v[0] = val; // check if array is empty
        n = n + 1; // v[0] becomes the given value
    }              // increase size of array
    if (val > v[index])
    {
        v[index+1] = val; // given value is bigger than the last value in array
        n = n + 1; // increase size
    }
    else
    {
        while (index >= 0 && v[index] > val)
        {
            v[index+1] = v[index]; //shift items to the right
            index--;
        }

        v[index + 1] = val; //after moving elements to the right
        n = n + 1;   // i set the value to the valid position
    }
}

void display(int n, int v[])
{
    int i;
    for (i = 0;i < n; i++)
        printf("%d ", v[i]);
}

int main(void)
{
    int v[10] = { 12, 23, 34, 41, 69, 71, 81, 91, 100 };
    int n;
    n = 9; // size of array
    insert(101,n,v); // 101 is given value to insert
    display(n,v);
    return 0;
}
3
  • Why the n argument is an int* and no int? Commented Jun 5, 2017 at 21:36
  • int *n ... int index = n - 1; would raise a warnings on a well enabled compiler. Save time, avoid embarrassment and enable warnings. Commented Jun 5, 2017 at 21:37
  • i need the N to be passed with the increased value after the insertion to the display function that prints the array, it's true that it shown warnings on my computer but I am still struggling with pointers Commented Jun 5, 2017 at 21:40

1 Answer 1

3

You have a couple of mistakes:

  1. You are passing int instead of int * so you're not able to update array size
  2. You are not correctly placing value in the array

This is how your code should look like:

#include <stdio.h>

void insert(int val, int *nPtr, int v[]);
void display(int n, int v[]);

int main(void) {
  int v[10] = {12, 23, 34, 41, 69, 71, 81, 91, 100};
  int n;
  n = 9;
  insert(101, &n, v);
  display(n, v);
  return 0;
}

void insert(int val, int *nPtr, int v[]) {
  int n = *nPtr;
  int i, j;
  int k = 0;

  for (i = 0; i < n + 1; i++)
    if (!k) {
      if (v[i] > val || i == n) {
        for (j = n - 1; j >= i; j--) {
          v[j + 1] = v[j];
        }

        v[i] = val;
        n++;

        k = 1;
      }
    }

  *nPtr = n;
}

void display(int n, int v[]) {
  int i;
  for (i = 0; i < n; i++)
    printf("%d ", v[i]);
  printf("\n");
}

You can also try to insert number on the beginning, for example 0 and it will still work.

Sign up to request clarification or add additional context in comments.

2 Comments

Thank you! Now i know how to pass the value using pointers. But I will stick to my code and try to improve it, as I need to cover all the possible cases, and I tested yours and for the value 0, that is smaller than the first element of the array, the array looks like {0, 23, 23, ...., 23}. It's true that your code is written better than mine, I can't say no to that, and for the BREAK; I am not allowed to use it in my college admission examen so I tend to avoid it, but thank you! You've been a great help.
@MariusJula. I didn't know you're not allowed to use break. Also, thank you for mentioning that my code had a bug. I fixed it and removed break, it now works for all cases.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.