Thread: C++ Help
View Single Post
Old 06-28-2009, 05:42 AM   #2 (permalink)
Baez
Baez's Avatar
 

Join Date: Sep 2005

Location: Toronto, Canada

Posts: 5,470

Baez is a glorious beacon of lightBaez is a glorious beacon of lightBaez is a glorious beacon of lightBaez is a glorious beacon of lightBaez is a glorious beacon of light

Default Re: C++ Help

That's a good question. It's because you're looking at the nested for loops wrong not the 2D array.

Think of nested for loops like this:

Run for loop #1
Run for loop #2
Is for loop #2 done? Yes then move back to for loop #1. No then execute for loop #2 again.

And it keeps going like that.

So the equations would look like this in order:

nums[0][0] = (0*4) + 0 + 1 = 1
nums[0][1] = (0*4) + 1 + 1 = 2 - because for loop #2 isn't done "t" stays at 0 while "i" increments by 1
nums[0][2] = (0*4) + 2 + 1 = 3 - again, for loop #2 isn't finished
nums[0][3] = (0*4) + 3 + 1 = 4 - same again
nums[1][0] = (1*4) + 0 + 1 = 5 - finally for loop #2 is finished so now loop back to for loop #1 and increment "t" by 1. For loop #2 restarts and therefore "i" is reset to 0.
nums[1][1] = (1*4) + 1 + 1 = 6 - and it starts again etc.

Hope that explains it. If not I'll try again .
__________________


Last edited by Baez; 06-28-2009 at 05:45 AM.
Baez is online now