# Trying to figure out how to iterate through a 2-d array....

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

## Recommended Posts

I am trying to figure out how to display my array back to the console window in some kind of graphical way. I almost have it with this
	for (x = 0; x<1 ;x++ )
{
for ( y = 0; y < 9; y++ )
cout<<x<<","<<y<<"="<<graphArray[x][y].slotValue<<"  ";
cout<<"\n";
}


Problem is that its displaying columns as rows like this... 0,0 0,1 0,2 0,3 0,4.... 1,0 1,1 1,2 1,3 1,4..... 2,0 2,1 2,2 2,3 2,4...

##### Share on other sites
Not sure exactly what you mean, but the way you have the loop set up it will print the entries by column and then by row. If you want it the other way around, put 'y' on the outside of the loop and 'x' on the inside.

Also, although I think your current example is functionally correct, you'll save yourself potential trouble down the road by using braces correctly and consistently.

##### Share on other sites
Quote:
 Original post by jykNot sure exactly what you mean, but the way you have the loop set up it will print the entries by column and then by row. If you want it the other way around, put 'y' on the outside of the loop and 'x' on the inside.Also, although I think your current example is functionally correct, you'll save yourself potential trouble down the road by using braces correctly and consistently.

When I put y on the outside loop and x on the inside the loop never ends. I can't figure out why the conditions are never reached. You mentioned that I should figure out how to brace the inner loop correctly. I tried just wrapping the inner loops code in braces but then things are not displayed how I want them to be. Insted of it looking like an actual grid it is just one long list of x,y values.

	//Display grid	for (y = 0; y<10 ;x++ )	{		for ( x = 0; x<10; x++ )			cout<<x<<","<<y<<"="<<graphArray[x][y].slotValue<<"  ";			cout<<"\n";			}
EDIT: I just looked at it again and it won't break out of the first row. It keeps cycling through this

0,0 1,0 2,0 3,0 4,0...
0,0 1,0 2,0 3,0 4,0...
0,0 1,0 2,0 3,0 4,0...

It never goes to the next row...

##### Share on other sites
//Display grid  for(x = 0; x < 10 ; ++x)  {    for(y = 0; y < 10; ++y)    {      cout << x << ","            << y << "="            << graphArray[y][x].slotValue << " ";    }    cout << "\n";	  }

##### Share on other sites
Take a close look at the first loop:
for (y = 0; y<10 ;x++ )

:)

##### Share on other sites
Edit: Way too slow.

Perhaps that's because of the for construct:
for (y = 0; y < 10; x++)
Methinks x should say y.

Also, I agree that your indentation convention is asking for trouble. Maybe you'd prefer the following. I know I would.

// Display gridfor (y = 0; y < 10; y++){	for (x = 0; x < 10; x++)	{		cout << "(" << x << "," << y << ")=" << graphArray[x][y].slotValue << "  ";	}	cout << "\n";}

Regards

##### Share on other sites
Thanks everyone...its makes sense now.

##### Share on other sites
This is why smart, experienced programmers avoid hand rolled loops, instead relying on standard library algorithms such as std::for_each.

##### Share on other sites

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

## Create an account

Register a new account

• ### Forum Statistics

• Total Topics
628714
• Total Posts
2984353

• 23
• 11
• 10
• 13
• 14