Public Group

# Some basic C++ help

This topic is 4391 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

I just started learning C++ and Visual C++. I'm trying to teach myself. This is probably an easy question for most of you programmers since you have been doing this for a long time, but for me I don't quite understand it. Here is the code for a program that outputs a 12x12 multiplication table. #include<iostream> #include<iomanip> using std::cout; using std::endl; using std::setw; int main() { const int size = 12; int i = 0, j = 0; cout << endl << size << " by " << size << " Multiplication Table" << endl << endl; cout << endl << " |"; for(i = 1; i <= size; i++) cout << setw(3) << i << " "; cout << endl; for(i = 0; i <= size; i++) cout << "_____"; for(i = 1; i <= size; i++) { cout << endl << setw(3) << i << " |"; for(j = 1; j <= size; j++) cout << setw(3) << j << " "; } cout << endl; return 0; } I'm a bit confused on how the output for the i*j works. If you remove the *j and just leave the i you will get output like this. 1 1 1 1 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 2 2 2 2 3 3 3 3 3 3 3 3 3 3 3 3 etc. If you take out the i and just leave the j you get 1 2 3 4 5 6 7 8 9 10 11 12 1 2 3 4 5 6 7 8 9 10 11 12 1 2 3 4 5 6 7 8 9 10 11 12 etc. I assume all output is from left to right in a console appliction. I'm curious why these two output differntly when I don't see any secial command or endl to cause this. It actually makes sense because it allows them to be multiplied if you have i*j to complete the table acurately, but why does it happen? Thanks

##### Share on other sites
Well, "i" is in the outer loop and "j" is in the inner loop. So, "i" changes every step of the outer loop which happens after "j" goes from 1 to size. That's why when you output just "i" in the inner loop, you see 11111... 22222... "j" on the other hand changes from 1 to size every step of the inner loop and rewinds back to 1 every step of the outer loop. That's why you see 12345...12345... when you output just the "j".

##### Share on other sites
That makes sense. Thanks.

##### Share on other sites
As sais above, Ill gvive you a visual exlaination.
Copy/paste it to see the acual results if you want.

Hope this helps.

#include <iostream>using namespace std;int main(){    for(int i = 0; i != 5, i++) // i wont be increased to 2 till k = 5    {        for(int k = 0; k != 5; k++)        {             cout << i << ", " << k << "\n"        }    }    return 0;}

##### Share on other sites
Thanks for the example. I think I get it now, but I could be wrong. :D

##### Share on other sites
So i = 1 it's first trip through the loop so it outputs a 1 12 times? The second trip through it outputs only a 2 and so fourth?

j on the otherhand outputs 1-12 each time?

##### Share on other sites
Quote:
 Original post by Chris27So i = 1 it's first trip through the loop so it outputs a 1 12 times? The second trip through it outputs only a 2 and so fourth? j on the otherhand outputs 1-12 each time?

It should output,

0,0
0,1
0,2
0,3
0,4
0,5
1,0
1,1
1,2 ...etc.

the outser for loop doesnt start its next iteratoion till the inner for loop has finished its all of its iterations. Thus, i doesnt start its snd iteration till k has finished its iteration. Then, this repeats till the outloop has finished all of its iterations.

I think that should answ your question. :D

##### Share on other sites
Sorry I was talking about the loop I put up there, but I see how yours explains what it does. I compiled it and ran it. Basically the variable in the outer loop stays the same until the inner loop finishes. Then it goes back to the outer loop and gets a new value for the variable.

Thanks for the help.

##### Share on other sites
Quote:
 Original post by Chris27Sorry I was talking about the loop I put up there, but I see how yours explains what it does. I compiled it and ran it. Basically the variable in the outer loop stays the same until the inner loop finishes. Then it goes back to the outer loop and gets a new value for the variable.Thanks for the help.

basically, yes.

1. 1
2. 2
Rutin
19
3. 3
4. 4
5. 5
frob
13

• 9
• 15
• 10
• 9
• 17
• ### Forum Statistics

• Total Topics
632602
• Total Posts
3007363

×