You could put in a printf() statement to print the value of the index into each loop that you have. You can also step through each loop and read EXACTLY what the program is doing. This is a good skill to develop because your mind has a habit of "fixing" small errors that computers don't.
Computers do exactly what they're told to do, not what you want the to do.