Problem with loop

I have a loop in a function that is supposed to insert a number in the space in the array where position is currently sitting. All the numbers have to stay in order and it is supposed to take the numbers and shuffle them down to make room for the new number. I have an array of 20 delcared. The output I am supposed to receive from this loop is 20, 19, 18, ---- 2, 1. Instead, I get 20, 0, 0, 0, ect. Any help, suggestions, or tips provided is greatly appreciated.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
//Loop being used to populate array (provided by the instructor):
for (int i=1;i<=20;i++)
{
b.insertBefore(i);
}


bool List::insertBefore(ET value)
{
    if(Position + 1 > CAPACITY)//Check to see if out of bounds
    {
        return false;
    }
    else
    {
        if(Used == 0)
        {
            MyAry[Used] = value;
        }
        else
        {
            for(int i = Used; i > Position; i--)
            {
                MyAry[i + 1] = MyAry[i];
            }
        }
            MyAry[Position] = value;
            Used++;

        return true;
    }
}
Think about the two cases that you have coded.
In one case, assuming that the variable Used has been initialized to 0, you have:
1
2
3
MyAry[0] = value;
MyAry[Position] = value;
Used = 1;


The next time you call this function, assuming that Position is 0, you have:
1
2
3
MyAry[2] = MyAry[1];
MyAry[0] = value;
Used = 2;


If, on the second call, Position is anything greater than 0, you have:
1
2
MyAry[0] = value;
Used = 2;


If during the first call, Position is passed the value 0, at no future time will that value be copied.

Thanks for your help! I've got it now.
Topic archived. No new replies allowed.