Comprehension failure.

Hi all. I'm learning C++ with Jumping Into C++ by Alex Allain and one of the examples of nested loops is code for a multiplication table. I actually made my own multiplication table using global variables but it was ugly and his grid table was much smaller, efficient and pleasant to look at. My problem is I'm having a total failure comprehending the logic of how the grid is actually created. I (embarrassingly) have spent 2 hours examining these few lines of code and I can't wrap my head around how the grid is displayed.

I commented every single line of code in the multiplication table in an attempt to figure out how it's done, but alas, I just don't get it. I'm not sure if you guys can help me because I feel like this is some simple kindergarten logic I'm failing, but if anyone has an explanation for how the grid is actually created... maybe a different perspective on how this all comes together? Maybe it will help me understand. I don't want to continue past this problem until I feel confident that I could replicate a multiplication table and -understand- each line that produces the perfect grid.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
#include <iostream>
#include <string>

using namespace std;

int main()
{

    for ( int i = 0; i < 10; i++ ) // For loop defines 'i' as 0, while 'i' is less than 10, increment by 1.
    { // Loop starts.
        cout << '\t' << i; // Horizontally prints the value of 'i' up to 9 with tab gaps between numbers.
    } // Loop terminates. This handles ONLY the first line of output and is disregarded afterward.

    cout << endl; // New line. Comprehension 100% up to this point.

    for ( int i = 0; i < 10; i++ ) // New for loop created, 'i' defined as 0 again, same as above.
    { //Outer loop starts.
        cout << i; // No idea how this line's behaviour is influenced. By itself prints 'i''s value.
        // Without the nested loop below, it prints 0 through 9 with no gaps horizontally.
        // With the nested loop below, it results in the multiplication table. ???

        for ( int j = 0; j < 10; j++ ) // Inner loop defines 'j' as 0, while 'j' is less than 10, increment by 1.
        { // Inner nested loop starts.
            cout << '\t' << i * j; // Prints result and inserts tab gap before each. Grid is created here? Don't know. How does it make this perfect grid?
        } // Inner nested loop terminates.

        cout << endl; // New line.

    } //Outer loop terminates.


}
I've scratched my head at code for a lot longer than two hours, don't sweat it.

Okay. Nested loops. I'm going to rewrite then comment, so my comments don't get in the way here.

1
2
3
4
5
6
7
8
9
10
11
for (int i=0; i < 10; ++i)
{
   std::cout << i;

   for (int j=0; j < 10; ++j)
   {
      std::cout << "\t" << i * j;
   }

   std::cout << std::endl;
}


Line 1) Essentially prints vertically, not horizontally. It's effectively writing the 0-9 in the first column.

Line 7) This is printing out the row for our current column.

Think about how it works. If i is 0, we're going to print a zero. Then we're going to go into the inner loop, printing j * 0 separated by tabs. Then we do a newline.

Then i is 1. We print our 1, go into our nested loop, do j * 1 tab separated. Then a newline. And so on and so forth.

I think maybe you confused yourself by thinking it was building the rows and columns in different ways.

Hope this helps.
Last edited on
Topic archived. No new replies allowed.