Sign in to follow this  

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.

If you intended to correct an error in the post then please contact us.

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 this post


Link to post
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 this post


Link to post
Share on other sites
Quote:
Original post by jyk
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.


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 this post


Link to post
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 grid
for (y = 0; y < 10; y++)
{
for (x = 0; x < 10; x++)
{
cout << "(" << x << "," << y << ")=" << graphArray[x][y].slotValue << " ";
}
cout << "\n";
}



Regards
Admiral

Share this post


Link to post
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.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this