Hi ! I'm having trouble getting good results here. I'm trying to write a function that sorts arrays in the following way:
1 - The first element has to be '0' (This is already guaranteed earlier from how i fill the array. I just don't want this element to change)
2 - Any other '0' encountered is to be moved to the end of the array and all elements shifted by 1 to the left.
Examples:
myArray[7] = {0, 1, 3, 0, 2, 4, 5} is sorted to {0, 1, 3, 2, 4, 5, 0}
and myArray[7] = {0, 0, 3, 0, 2, 4, 5} is sorted to {0, 3, 2, 4, 5, 0, 0}
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
|
myArray[7] = {0, 1, 3, 0, 2, 4, 5};
int zeroCounter = 0;
for(int i=1; i<(6-zeroCounter); i++)
{
if (myArray[i] == 0)
{
for(int j=i; j<(6-zeroCounter); j++)
{
myArray[j] = myArray[j+1];
}
zeroCounter += 1;
myArray[7-zeroCounter] = 0;
}
}
|
I would also like my 'zeroCounter' to count all zeros in the array (except for the 0 element in myArray[0] )