> A CODE Q why i am wrong?

A CODE Q why i am wrong?

Posted at: 2014-12-18 
The parameter length is passed by value. That means your function has a *copy* of the value the caller passed in. When you do length++, you are incrementing your local copy. You are not changing the callers value.

You need to pass length by reference (using &), so the callers variable gets updated.

Write the definition for a function, insert , that accepts the following parameters (in this order): an array of integers (filled with 0 or more elements starting at index 0), the number of elements currently in the array , the maxmum number of elements the array can hold, the position at which to insert the value , and the value to be inserted. If the array is full or the position is illegal (too small or too large), the function returns false ; otherwise, the value is inserted at the specified position (moving the rest of the array one position to the right), the number of elements in the array is updated, and the function returns true .

Elements may only be inserted within the filled portion of the array or at the position after the last filled element . Thus, if the array has three elements , you can insert at positions 0, 1, 2, 3, and 4.

i wrote

bool insert(int a[], int length, int max, int position, int val){

int x = 0;

if (length + 1 > max){

return false;

}

if (position < 0 || position > length ){

return false;

}

for (x = length - 1; x >= position; x--){

a [ x + 1 ] = a [ x ];

}

a [ position ] = val;

length++;

return true;

}

but get feedback as The Council agrees that:

? You almost certainly should be using: &

Problems Detected:

? The number of elements is incorrect upon return from the function