It is something like that, only not quite. You want to check if a[i] is 0 as well each time. If it is, you don't want to do anything immediately. If it isn't, you can set a[k] to a[i] and a[i] to 0, and then use a break; statement to leave the loop.
Also, since you're sliding zeroes down the array a few spots at a time, each zero is going to come up several times. Thus, sum is going to be much larger than the actual number of zeroes. You're better off doing your counting up at the top, when you're filling the array.
Also, since you're sliding zeroes down the array a few spots at a time, each zero is going to come up several times. Thus, sum is going to be much larger than the actual number of zeroes. You're better off doing your counting up at the top, when you're filling the array.
Comment