Problem in the C++ tutorial.

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.

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.

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.

> 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.

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.

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

1234

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

12

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.

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.

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.