Problem in the C++ tutorial.

Pages: 12
Is it just me or right around the Arrays page when they start describing multidimensional arrays does the tutorial stop being explicit?

jimmy[n][m]=(n+1)*(m+1)

They give this bit of code without explaining at all what the second half of it does. If they were numbers alone it would assign values to the indexes, but adding 1 to each and multiplying them by each other? Wtf does that do? And in the context of a loop god only knows.

Then the other bit of code does this:

for (n=0;n<HEIGHT;n++)
for (m=0;m<WIDTH;m++)
{
jimmy[n*WIDTH+m]=(n+1)*(m+1);
}

Which is even more confusing. Two for loops, only one doesn't have a statement (wtf??) Is it one for each index? Which one effects which one which way and in what order?

Am I just stupid or does the tutorial skip over an explanation here?

The tutorial has been awesome up to this point, major kudos. But this bit makes me want to go find another tutorial for arrays.
Last edited on
> They give this bit of code without explaining at all what the second half of it does.
But they do, in the paragraph following the code.

> Two for loops, only one doesn't have a statement
It does have an statement. Review loops if you don't understand them.
It is perhaps confusing to a beginner because it uses a whimsical style.

The snippet might have been clearer if it was either:
1
2
3
for( int n=0 ; n<HEIGHT ; n++ ) 
    for( int m=0 ; m<WIDTH ; m++ )
        jimmy[n][m] = (n+1) * (m+1) ;


or:
1
2
3
4
5
6
7
for( int n=0 ; n<HEIGHT ; n++ ) 
{
    for( int m=0 ; m<WIDTH ; m++ )
   {
        jimmy[n][m] = (n+1) * (m+1) ;
   }
}



Try this:
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
#include <iostream>

// #define WIDTH 5 // avoid
// #define HEIGHT 3

// int jimmy [HEIGHT][WIDTH];
// int n,m; // avoid

int main ()
{
    const int WIDTH = 5 ; // favour this over a #define
    const int HEIGHT = 3 ;
    int jimmy [HEIGHT][WIDTH] ;

    for( int n=0 ; n<HEIGHT ; n++ ) // limit visibility of names to the smallest 
                                     // scope in which they are required
    {
        for( int m=0 ; m<WIDTH ; m++ ) 
        {
            jimmy[n][m] = (n+1) * (m+1) ;

            // to understand what is going on, just print out the numbers
            std::cout << "n==" << n << " m== " << m
                       << " (n+1)*(m+1) == " << (n+1)*(m+1) << '\n' ;
        }
    }
}
closed account (Dy7SLyTq)
and honestly it helps to read through code line by line and sometimes character by character
It also helps to have a tutorial that explains the rules of syntax without just giving a statement and saying "you figure it out".

In every other part of the tutorial they walk you through step by step, here they got sloppy and stopped doing that and it's a headache.
They tell you what values it assigns by the end of the loop but not why it does any of it or in what order. Is the left value assigned first or second, when does multiplication occur, what value does it modify? Both of them? Etc. It makes no sense.
[They give this bit of code without explaining at all what the second half of it does.]

"But they do, in the paragraph following the code."

They tell you what the end values are, they don't explain the syntax.

[Two for loops, only one doesn't have a statement]

"It does have an statement. Review loops if you don't understand them. "

Okay, lets review:

The for loop
Its format is:
for (initialization; condition; increase) statement;

The loop(s)? in the array example has a format of:

for (initialization;condition;increase) for(initialization;condition;increase) statement;

They never explain the difference.

When they give the code mentioned in the original post it's similarly of a totally new, unexplained syntax. You may have learned from a different version of the tutorial or a different source, but for those of us using this version it uses examples without explaining them first.
And if it's a nested loop (not covered in the tutorial up to this point) wouldn't it only run 3 times since height is 3 and the height loop is called first? Or would it call the first loop once, then finish the second, then go back to the first? Are they even two loops? I don't know, the tutorial doesn't tell me.

See my dilemma?
> They tell you what the end values are, they don't explain the syntax.
You mean (n+1)*(m+1), that's just a placeholder, as it needed something to show.
If you don't understand what `(n+1)*(m+1)' does, review arithmetic operators.


1
2
3
4
5
6
for(initialization;condition;increase) 
   statement;

//that particular statement happens to be
   for(initialization;condition;increase) 
      statement;
I know what is in parentheses evaluates first, then the value of the first expression is multiplied by the second expression. What I don't know is how this effects an array or what two for loops with one statement does, why and and in what order. I have many very specific questions the tutorial does not address.

The glib answer of "go review x section" as though my problem is not understanding how multiplication works is condescending and doesn't even come close to being helpful.

I guess I'm off to go stare at a block of text some more until I figure out what neither you nor the tutorial explains.
Repeat: to understand what is going on, just print out the numbers

1
2
3
4
5
6
7
8
9
10
11
12
    // ...
    for( int n=0 ; n<HEIGHT ; n++ )
    {
        for( int m=0 ; m<WIDTH ; m++ ) 
        {
            jimmy[n][m] = (n+1) * (m+1) ;

            // to understand what is going on, just print out the numbers
            std::cout << "n==" << n << " m== " << m
                       << " (n+1)*(m+1) == " << (n+1)*(m+1) << '\n' ;
        }
    }
I asked a series of very specific, clear questions about logical rules and syntax. Nobody is going to even try to answer any of them?
The tutorial at this point hasn't even covered nesting loops and it's using one in the example. I have a legitimate grievance here (several actually).
Okay, lets review:

The for loop
Its format is:
for (initialization; condition; increase) statement;

The loop(s)? in the array example has a format of:

for (initialization;condition;increase) for(initialization;condition;increase) statement;

They never explain the difference.


What you are missing is that the entire for loop expression is a statement itself. So the inner loop is:

1
2
3
4
for (m=0;m<WIDTH;m++)
{
    jimmy[n*WIDTH+m]=(n+1)*(m+1);
}


This loops through a series of values for m, performs a function that depends on the current value of n, and stores the value in the jimmy array at a location dependent on the current values of m and n.

The outer loop is
1
2
for (n=0;n<HEIGHT;n++)
    <INNER LOOP>


This loops through a series of values for n, and for each one, performs the entire inner loop. So the inside statement is performed for n=0,m=0; n=0,m=1; ... n=0; m=WIDTH-1; n=1,m=0, ... n=HEIGHT-1,m=WIDTH-1.

jimmy[n][m]=(n+1)*(m+1)

1
2
3
4
5
for (n=0;n<HEIGHT;n++)
for (m=0;m<WIDTH;m++)
{
jimmy[n*WIDTH+m]=(n+1)*(m+1);
}


These 2 blocks of code show how a 2-dimensional array work. The (n+1)*(m+1) is trivial, throw-away code whose only purpose is to show that the n and m values are being used, allowing us to see various values after they are populated in the array. @JLBorges told you to print out the values--that's why.

The first example takes the result and formally stores it into a 2-dimensional array. Straight forward.

The second example uses a 1-dimensional array to show you how a 2-dimensinal array works under the hood. The 2-dimensional array jimmy[HEIGHT][WIDTH] is an array of arrays. The outer (row) array is an array of length <HEIGHT> containing elements which are arrays themselves. These inner (column) arrays are of length <WIDTH> and contain ints.

The second example shows how you can find an element in a 1-dimensional array that corresponds to an equivalent 2-dimensional array by indexing over entire rows (each row has WIDTH elements) and the necessary columns. Since n and m both start at 0, you can skip over previous rows with (n * WIDTH), and you can skip over previous columns with m. So, (n * WIDTH) + m gets you to the appropriate element in the 1-dimensional array representing a 2-dimensional array.

And that is how 2-dimensional arrays are implemented under the hood.

Edits: correct grammar
Last edited on
I think I understand mostly now (your answer helped but mostly I worked it out by trial and error). What was hanging me up is that the tutorial didn't explain how nested loops work (I only knew the concept at all from briefly googling it back when it warned vaguely about "nesting limitations" in the section about the goto line. So I didn't know that the first loop executes partly then the second loop executes all x times, then the first loop finishes, then the second loop executes all x times, etc. That and I wasn't sure if the multiplication on the right was setting the values or the index numbers of the figure on the left, which to be fair was just me being stupid. A lot of my confusion could've been avoided had the tutorial at least had a paragraph about nested loops in the control structures section.

One thing I still don't understand though - wouldn't the one dimensional array be just one row of 15 values? What does the * do to it? Again, glossed over in the tutorial.
In my experience online tutorials are brief and lacking in details. They seem meant for learning one feature at a time, not for learning the whole subject. Books are much better for this. Find a good book for beginners then work your way through it steadily. Don't skip any sections and try to work as many of the exercises as you can. If you can't understand something, don't just skip it and plow forward - you will only get more lost. Back up to review what was missed.

This approach has worked well for me. A good book will present the subject well as a whole and you'll be spared the frustration you've experienced here by trying to learn everything from a bunch of disconnected individual tutorial articles.
This tutorial has been good up to this point, though it's helped tremendously to google terms and concepts for more information to get them straight in my head. Recommend any books on c++?
closed account (Dy7SLyTq)
bajarne stroustops books
I believe that's Bjarne Stroustrup.
Last edited on
Pages: 12