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.