+2  A: 

Its no fun if someone else does your homework, but I'll offer a suggestion. If you cout all the key variables right after

while (indexOfFirst <= indexOfLast)  

I bet you anything you'll see the problem immediately.

Scott Stafford