Jump to content
  • Advertisement
Sign in to follow this  
elitentity

Displaying MultiDimensional arrays easily

This topic is 5120 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

How do you display multidimensional arrays to the console? I can't seem to do it the same way as normal arrays. For example: char blah[2][2]{{'a','b'}{'c',d'}}; cout<<blah; This shows up as some wierd hexadecimal-ish thing. What I want it to show up as would be: ab cd Any idea if this is possible without too much work? Thanks

Share this post


Link to post
Share on other sites
Advertisement
This isn't supported, you'll need to use a loop

for x = 0 to size_x
for y = 0 to size_y
cout << foo[x][y];
next y
cout << endl;
next x

Share this post


Link to post
Share on other sites
Thanks for the help, but...
Sorry, but I am a real noob and I don't really understand that.
Here are the parts I need clarification upon:
size_x and size_y
next y and next x

edit
----------------------------------------------------------------
I looked on google and found something that might be related:
size_t and strlen? Dunno.

Share this post


Link to post
Share on other sites
Quote:
Original post by elitentity
Thanks for the help, but...
Sorry, but I am a real noob and I don't really understand that.
Here are the parts I need clarification upon:
size_x and size_y
next y and next x

just some pseudo-code, sorry for being cryptic.

size_x and size_y are the dimentions of your array, in this case, 2 by 2.

the for loops are done in BASIC style, because I like basic, dammit. [smile]

here's the C++ code

#include <iostream>
using namespace std;

int main()
{
char blah[2][2] = {{'a', 'b'}, {'c', 'd'}};

for(int y = 0; y < 2; y++)
{
for(int x = 0; x < 2; x++)
{
cout << blah[x][y];
}
cout << endl;
}
}


That should compile.

Basically, I'm looping through all the letters and printing them out. After each row, I print a return(endl) which resets it for the next row.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!