Given that t and n are two fixed |
t is decremented, but regardless the logic is flawed.
I believe he is attempt to start at the back of the array, and then shift the elements over until the value he is inserting is no longer greater than the existing element.
jlm, what does your array look like before you call this function? An array is a static size. Are you leaving enough room for it to grow?
The logic is flawed for probably a few reasons, but the major reason it you are using n as a
value for comparison on line 11, but then you're using it as an
index on line 13. You do not want to be using it as an index.
jlm, start small, and work out your logic by hand. Start with just 2 elements, e.g. [2, 5, ~, ~ ...] (where ~ is some dummy value, the logical size of array is still 2).
Since you are inserting in ascending order, I personally think it makes more sense to increment your counter variable, as opposed to decrementing like you are now.
1 2 3 4 5 6 7 8 9 10 11
|
int index_to_insert = numEntries;
for (int i = 0; i < numEntries; i++)
{
if (myArray[i] > newValue)
{
index_to_insert = i;
}
}
// perform the insertion at index_to_insert
// push over existing elements
|