# Thread: Help with Arrays and For loops

1. ## Help with Arrays and For loops

Hi, I have two questions about two different program examples.

What I'm having trouble with is certain things with the For loops and their use with Arrays.

An example of something I don't quite understand is the Lesson 8 array example located on this site. I'm not confused by the array itself but the For loop in the array. It goes as follows -

Code:
1.  #include <iostream>
2.
3.  using namespace std;
4.
5.  int main()
6.  {
7.  int x;
8.  int y;
9.  int array[8][8];
10.
11.  for ( x = 0; x < 8; x++ ) {
12.    for ( y = 0; y < 8; y++ )
13.      array[x][y] = x * y;
14.   }
15. cout<<"Array Indices:\n";
16.  for ( x = 0; x < 8;x++ ) {
17.     for ( y = 0; y < 8; y++ )
18.     cout<<"["<<x<<"]["<<y<<"]="<< array[x][y] <<" ";
19.   cout<<"\n";
}
cin.get();
}
What I'm not fully grasping is what is printing out on line 18. The two loops (if I understand correctly) just say to increase the variable up until the condition is met, which in this case is increase if they're less than 8. If I only had one variable up there, for example x, it would only print out 0,1,2,3,4,5,6,7 and stop at that. However, it seems they're being multiplied and being printed out 8 times each instead of just each printing out their 0-7 and stopping.

What I want to know is why they're being multiplied like that? To make it even more confusing, if I add another variable (say z) and do another <8 loop, it prints out 1544 numbers. I was wondering if someone knows the exact rules for how things are multiplied or added in for loops like above? For instance, even if I don't add z to cout, it still prints out a lot more than 49, but why? I guess I'm factoring the numbers, and how they're working in the loop, wrong...

On to another example that I'm confused by. Actually for this one I'm really just wondering if I'm thinking right or not. This is from a book I'm reading that basically guides the reader in making a simple program - in this case, a calculator. There's a function inside the program that uses an array to keep track of what the user has entered but I'm a bit confused by it. Here's the function with comments by me on what I think is happening, or what I'm confused with:

Code:
void Tape(const char theOperator,const float theOperand) // just wanted to note, both theOperator and theOperand are both whatever operator or operand I've entered in another function that I didn't include here
{
static const int myTapeSize = 20;

static char myOperator[myTapeSize];
static float myOperand[myTapeSize];

static int myNumberOfEntries = 0;

if (theOperator != '?')
{
if (myNumberOfEntries < myTapeSize)
{
myOperator[myNumberOfEntries] = theOperator; // Just want to make sure I understand this part. From what I gather, I'm referencing the element myNumberofEntries in the myOperator array and setting theOperator (-,+,*,/) to be held in that element, which in this case is 0. So I'm guessing operators are valid chars since this is a char array?
myOperand[myNumberOfEntries] = theOperand;
myNumberOfEntries++; // This increases myNumberofEntries by one. So in element 0, whatever operator and operand I've put in both elements is held in their array at 0, correct? Also, myNumberofEntries will only go from 0-19 (i.e. 20, since it stops one short of 20 in the loop but it's still 20 since arrays indices start at zero), right or wrong?
}
else
{
throw runtime_error("Error - Out of room on the tape.");
}
}
else
{
for (int Index = 0; Index < myNumberOfEntries; Index++) // This is what I'm mainly confused by. The myNumberofEntries variable will only go up to 19 (from 0 to 19 in the 20 element array in the previous for loop) so in this one, index will stop at 19 (0-18). If that's the case, why does cout still print out 20 elements (one for each element of each array)? Shouldn't it only show 19 of my entries?
{
cout << myOperator[Index] << "," << myOperand[Index] << endl;
}
}
}
All help is appreciated.

2. You do realize that the statements inside a for loop are executed every time, right? We don't just count up the numbers and not do anything. So for instance in your last for loop there, that cout statement will happen each time through the loop -- when index is 0, when index is 1, and so on.

That's why something like:
Code:
for ( x = 0; x < 8; x++ ) {
for ( y = 0; y < 8; y++ )
array[x][y] = x * y;
}
works the way it does. Everything inside the first for loop will happen for each value of x from 0 to 7. Once inside, each inner loop statement will happen for each value of y from 0 to 7, so it ends up happening for x=0, y=0; x=0, y=1; ...; x=0, y=7; x=1, y=0; x=1, y=1; et cetera.

3. Originally Posted by tabstop
You do realize that the statements inside a for loop are executed every time, right? We don't just count up the numbers and not do anything. So for instance in your last for loop there, that cout statement will happen each time through the loop -- when index is 0, when index is 1, and so on.

That's why something like:
Code:
for ( x = 0; x < 8; x++ ) {
for ( y = 0; y < 8; y++ )
array[x][y] = x * y;
}
works the way it does. Everything inside the first for loop will happen for each value of x from 0 to 7. Once inside, each inner loop statement will happen for each value of y from 0 to 7, so it ends up happening for x=0, y=0; x=0, y=1; ...; x=0, y=7; x=1, y=0; x=1, y=1; et cetera.
Thanks for the reply. It seems I've understood the loop tutorial on this site wrong; or rather, I've misunderstood how multiple for loops work. In the tutorial it says " Keep in mind also that the variable is incremented after the code in the loop is run for the first time". In the example there the first value x is printed out to the screen as 0 because, as it says, the variable isn't incremented until after the code in the loop is run for the first time. So it checks the variable is zero, goes to cout (i.e. runs the code in the loop), then goes back to increment the variable again, and repeats until said condition is met.

Going back to the example
Code:
1. for ( x = 0; x < 8; x++ ) {
2.    for ( y = 0; y < 8; y++ )
3.       cout << x << y;
}
What I thought was happening was similar to what I quoted from the tutorial "the variable is incremented after the code in the loop is run for the first time". In line 1 the variable isn't incremented until after the code is run for the first time (so x is 0 right now). The code that runs next is another for loop so I thought the same rules applied, that is, the variable isn't incremented until the code after that ran, in this case cout which would print out 0,0. After cout it would loop over again until the condition is met, meaning it would go back to line 1, right? Since line 1 is 0 I was thinking it would increase to 1 then go to y and do the same.

I see now that multiple for statements don't seem to work like that. I guess the rule I quoted only applies to the first for statement in multiple loops, or maybe I still don't get it (or am visualizing it wrong)?

Back to the second example; I see why index increases, what I don't see is why it shows 20 operands/operators when I enter ? to print out what's in the index? The code
Code:
index<myNumberofEntries
should only allow index to continue until the condition is met, which in this case is while index is less than 19 (since myNumberofEntries already increased to 19 in the earlier loop). I guess it's still taking 19 as 0-19, which is 20, so it's allowing it to go 0-19 again, correct?

Thanks again.

4. The variables in nested for loops are reset for each literation of the outter loop(s). It has to do with scope. In the example above for example, y only exists within that inner for loop. While it may have reached its max value first go around, it becomes non-existent on the next literation of the outer for loop incrementing x until the inner y loop is reached again, in which case y is re-inistalized with a value of 0 and continues looping 8 times until its equal to 8 (no longer less than 8 as specified in the loop condition). x is incremented again when the y loop has finished, and the proccess of looping for x continues until x is equal to 8 to meet x loop's loop condition.

So if you do the math, 8 literations of x loop times 8 literations of y loop means y loop's instructions are executed 64 times max.

5. Originally Posted by Phil-
should only allow index to continue until the condition is met, which in this case is while index is less than 19 (since myNumberofEntries already increased to 19 in the earlier loop). I guess it's still taking 19 as 0-19, which is 20, so it's allowing it to go 0-19 again, correct?

Thanks again.
We don't type "index = 0" for our health, but because we actually want it to happen. That first statement of the for loop is used to initialize the loop variables and it happens before the loop starts.

6. Originally Posted by kairozamorro
The variables in nested for loops are reset for each literation of the outter loop(s). It has to do with scope. In the example above for example, y only exists within that inner for loop. While it may have reached its max value first go around, it becomes non-existent on the next literation of the outer for loop incrementing x until the inner y loop is reached again, in which case y is re-inistalized with a value of 0 and continues looping 8 times until its equal to 8 (no longer less than 8 as specified in the loop condition). x is incremented again when the y loop has finished, and the proccess of looping for x continues until x is equal to 8 to meet x loop's loop condition.

So if you do the math, 8 literations of x loop times 8 literations of y loop means y loop's instructions are executed 64 times max.
Thank you. I understand now.

Originally Posted by tabstop
We don't type "index = 0" for our health, but because we actually want it to happen. That first statement of the for loop is used to initialize the loop variables and it happens before the loop starts.
Thanks again. I understand this. I was confused by why it was allowing it to go to 19 but I think i get it now with help from your and kairozamorro's explainations.

And I appreciate all the help, guys. Thanks