Originally Posted by
lscamaro
How does this look? Is this right?
Pretty close, check the comments in the code
Code:
int i,pass;
boolean swapMade;
pass = 1;
*numberCompares = 0;
*numberMoves = 0;
STARTOFDO:
{
swapMade = false;
i = 0;
FIRSTLOOP:
// The end of the for loop may not be the same as the end of the do, use two separate labels with good names and goto the right one
if(!(i <= n-(pass+1))) goto END;
{
*numberCompares += 1;
// Next line is wrong, you skip the increment. Just use the if condition as it was before (no !) without a goto
// if (ShouldSwap(...))
if(!(ShouldSwap(A[i],A[i+1]))) goto FIRSTLOOP;
{
int T = A[i]; A[i] = A[i+1]; A[i+1] = T;
*numberMoves += 3;
swapMade = true;
}
}
pass++; // This should be i++
// Don't forget to go back to the start of the for loop here
// This is where your END_FOR_LOOP label would go
}if(!(swapMade)) goto END; // The do loop needs to continue if swapMade is true, try: if (swapMade) goto STARTOFDO
END: