Hi everyone! Let's say I have an array of structures:
1 2 3 4 5 6 7 8
struct Party
{
char * pName;
int NumPeople;
char Plane;
};
Party List [5];
And let's say List is populated as follows:
1 2 3
List [1] = Alfa Stevenson 2
List [2] = Beta Dorothy 1
List [3] = Alfa Adam 3
where Alfa/ Beta are different groups you're trying to sort the Parties into.
So I'm going to move List [2] to another array (prewritten)...and then 'shift' the rest of the list up one.
1 2 3 4 5
Party Beta [5];
Beta [0] = List [2];
for (i = 2; i < 5; i++)
List [i - 1] = List [i];
But when this runs, array is populated as below.
1 2 3
List [1] = Alfa Stevenson 2
List [2] = Alfa Adam 3
List [3] = Alfa Adam 3
So my question is (FINALLY), How do I get rid of this issue of having two copies of the last party? If I was to populate List [3] with spaces..would it be possible to wrote over List [3] with new input?
You need to keep a note of the number of items in the array. Then use that as your top exclusive bound:
1 2 3 4 5 6 7 8 9
Party List [5];
int end = 5;
Party Beta [5];
Beta [0] = List [2];
for (i = 2; i < end; i++) // only iterate up to (not including) end
List [i - 1] = List [i];
--end; // decrement end point
As helios said this would be better done with std::list, unless its an assignment.