Thread: C++ Help
View Single Post
Old 06-27-2009, 05:57 PM   #1 (permalink)
Oreo
 
*^[#@"'.,;:\|/:;,.'"@#]^*

Join Date: Dec 2006

Location: Leicester, England

Posts: 3,656

Oreo will become famous soon enoughOreo will become famous soon enough

Send a message via MSN to Oreo
Default C++ Help

Don't laugh, only been doing C++ for a few days now, just started arrrays on my book and got a little confused as to how this code works out how it does:

#include <iostream>
using namespace std;

int main()

{

int t,i, nums[3][4];

for(t=0; t < 3; ++t) {
for(i=0; i < 4; ++i) {
nums[t][i]=(t*4)+i+1;
cout << nums[t][i] << ' ';
}
cout << '\n';
}


system("pause");

return 0;

}



The outcome is:

1 2 3 4
5 6 7 8
9 10 11 12.

Whilst i understand the outcomes themselves, e,g when [t] = 0, and [i] = 0, the result is 1.
In my mind i don't understand how all the possibilities are displayed in the order they are. Because the first time the loop is ran, [t] and [i] are set to 0, so the result is 1. Next loop, surely [t] would be 1 and [i] would be 1, so the result is 6, then next loop [t] = 2 and [i] = 2, the result is 11.

So surely the outcome would be displayed as:
1, 6, 11 ? not 1, 2, 3.

Sorry if you don't understand what i'm going on about But it's confusing me
Oreo is offline