Public Group

# Variables with unexpected values

This topic is 2951 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

I have an assignment out of a book that wants you to create and initialize 3 matrix variables with a dimension of 2 x 3. two of the variables are to be added and stored in the 3rd which is then displayed to the user. The book asks you use nested for loops.

I want to know why, when I leave my for loops and get back into main, do the values of 'i' and 'j' increase by 1. The for loops in this code work, but the final display of the 3rd matrix doesn't because it becomes out of bounds.

This code is incomplete because I have some cout's listed so I could see exactly what was going on with my final array being out of bounds. I have also not fixed the final arrays format presented to the user. Any help provided would be great. Thanks.

#include <iostream>using namespace std;int main (){	int iArray1[2][3] = 	{		{-5, 2, 8},		{1, 0, 0}	};	int iArray2[2][3] = 	{		{1, 0, 2},		{0, 3, -6}	};	int iArray3[2][3] = {0};	int i = 0;	int j = 0;	for(; i < 2; ++i)	{		cout << "i is " << i << endl;		for(j = 0; j < 3; ++j)		{			cout << "j is " << j << endl;			iArray3[j] = iArray1[j] + iArray2[j];		}		cout << "Array3 [0][0] = " << iArray3[0][0] << endl;		cout << "Array3 [0][1] = " << iArray3[0][1] << endl;		cout << "Array3 [0][2] = " << iArray3[0][2] << endl;		cout << "Array3 [1][0] = " << iArray3[1][0] << endl;		cout << "Array3 [0][0] = " << iArray3[1][1] << endl;		cout << "Array3 [0][0] = " << iArray3[1][2] << endl;			}		cout << "Int i = " << i << endl;	cout << "Int j = " << j << endl;	cout << "Array Sum is " << iArray3[j] << endl;	char response;	cin >> response;	return 0;	}

##### Share on other sites
>>I want to know why, when I leave my for loops and get back into main, do the values of 'i' and 'j' increase by 1

Thats how for loops work. Look at the condition of the for loop. For example :
for(; i < 2; ++i){ ...}

That for loop would keep looping until 'i' is 2 or greater, right? So thats
why i is 1 more than what you expect. The same goes for the variable 'j'.

>>cout << "Array Sum is " << iArray3[j] << endl;

Thus in the above code what you are doing is this :
cout << "Array Sum is " << iArray[2][3] << endl;

which is out of bounds, since arrays index starts at 0, and not 1.

Also, you will find that people here will tell you that Hungarian notation is
not a good idea, especially in the way you are using it.

##### Share on other sites
The way I read it the loop should stop when 'i' is less than 2. I'm confused now.

##### Share on other sites
Quote:
 Original post by verbalabuseThe way I read it the loop should stop when 'i' is less than 2. I'm confused now.

This code:
for(; i < 2; ++i) {  // something}

is exactly the same as this:
while (i < 2) {  // something  ++i;}

Does it make more sense then that i would be 2 at the end? You keep doing this until (i < 2) is false. So it makes sense that you get out of the loop when i is 2.

##### Share on other sites
I see it now. Thanks for the explanation everyone.

##### Share on other sites
Quote:
 Original post by alvaroThis code:for(; i < 2; ++i) { // something}is exactly the same as this:while (i < 2) { // something ++i;}

Assuming "// something" doesn't contain any continue; statements, anyways, which would skip over the ++i; in the while loop but not the ++i; in the for loop.

(which is completely useless additional trivia in the context of this problem, but I couldn't help adding it anyways [lol])

##### Share on other sites
Quote:
Original post by MaulingMonkey
Quote:
 Original post by alvaroThis code:for(; i < 2; ++i) { // something}is exactly the same as this:while (i < 2) { // something ++i;}

Assuming "// something" doesn't contain any continue; statements, anyways, which would skip over the ++i; in the while loop but not the ++i; in the for loop.

(which is completely useless additional trivia in the context of this problem, but I couldn't help adding it anyways [lol])

Actually, while we are explaining for' loops, it's probably good to break down what continue' and break' actually do:
for (A; B; C) {  if (D) continue;  if (E) break;}

is the same as
{  A;  while (B) {    if (D) goto label1;    if (E) goto label2;label1:    C;  }label2:;}`

1. 1
2. 2
3. 3
Rutin
20
4. 4
frob
20
5. 5

• 32
• 13
• 10
• 11
• 9
• ### Forum Statistics

• Total Topics
632559
• Total Posts
3007080

×