• FEATURED

View more

View more

View more

### Image of the Day Submit

IOTD | Top Screenshots

### The latest, straight to your Inbox.

Subscribe to GameDev.net Direct to receive the latest updates and exclusive content.

# Program that prints out array is not working

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

7 replies to this topic

### #1Youbar  Members

Posted 15 October 2012 - 09:50 PM

#include <iostream>
using namespace std;
int map[5][5] =
{
1, 1, 1, 1, 1,
1, 0, 0, 0, 1,
1, 0, 0, 0, 1,
1, 0, 0, 0, 1,
1, 1, 1, 1, 1
};
//other stuff
int mapn = 0;
int mapn2 = 0;
int number = 0;
int number2 = 0;
int x, y = map[x][y];
int main()
{
x = map[2][y];
y = map[x][2];
while (mapn < 5, mapn2 < 5)
{
cout << map[number][number2];
mapn++;
number++;
if (mapn == 5)
{
mapn2++;
number2++;
mapn = 0;
cout << endl;
}
}
cout << endl;
system("PAUSE");
}


This program is supposed to print out an array. To do so, it prints out a number, adds onto that number and once that number equals five, number2 has 1 added onto it, and number is reset. Why isn't it working?
It compiles fine, but the output is a lot of random numbers.

### #2RulerOfNothing  Members

Posted 15 October 2012 - 09:55 PM

number isn't reset. Is there any particular reason you declared number and number2 instead of using mapn and mapn2? Also, you should probably be using nested for loops for going through a 2D array, and "mapn < 5, mapn2 < 5" probably doesn't do what you think it does (the comma operator is simply equal to the second expression, so that code is equivalent to mapn2 < 5)

### #3Youbar  Members

Posted 15 October 2012 - 10:36 PM

Spoiler

Nvm

Edited by Youbar, 15 October 2012 - 11:00 PM.

### #4jschmuff  Members

Posted 16 October 2012 - 07:56 AM

Just an FYI "goto" is typically frowned upon by programmers and usually considered bad practice and harmful, just search "Edsger Dijkstra on goto".

Example for loop for cycling this kind of 2d array:
for (int x = 0; x < 5; x++) {
// In here you can do something with just x in the map
// int somethingElse = map[x][0];
for (int y = 0; y < 5; y++) {
int something = map[x][y];
}
}


Edited by wicked357, 16 October 2012 - 07:59 AM.

### #5Serapth  Members

Posted 16 October 2012 - 08:28 AM

Seriously... where are new developers even being exposed to goto anymore?!?!?

I mean, it should have been relegated to complete niche status within the language by this point. I dont think it is even featured in most C++ books, so how the heck people even learn about it.

### #6BCullis  Members

Posted 16 October 2012 - 10:42 AM

int x, y = map[x][y];

I'm really curious what happens here. x is declared as an int without being defined, and y is declared as an int defined as map[x][y]...that's a recursive definition on top of an undefined index? It's been a while since I mucked with C++.
I guess it doesn't matter since x and y are unused after their first assignment in main. But I don't see how that's being resolved by the compiler.

Hazard Pay :: FPS/RTS in SharpDX (gathering dust, retained for... historical purposes)
DeviantArt :: Because right-brain needs love too (also pretty neglected these days)

### #7Code Fox  GDNet+

Posted 16 October 2012 - 01:49 PM

int x, y = map[x][y];

I'm really curious what happens here. x is declared as an int without being defined, and y is declared as an int defined as map[x][y]...that's a recursive definition on top of an undefined index? It's been a while since I mucked with C++.
I guess it doesn't matter since x and y are unused after their first assignment in main. But I don't see how that's being resolved by the compiler.

Unless I am missing something, the array is improperly populated.
Both X and Y are declared as NULL
After MAIN they are both declared as map[2][2], which are both NULL
Also as it's written the values of X,Y will not update when the map is updated.

Corrected 2D array declaration.
int map[5][5] =
{
{1, 1, 1, 1, 1},
{1, 0, 0, 0, 1},
{1, 0, 0, 0, 1},
{1, 0, 0, 0, 1},
{1, 1, 1, 1, 1}
};


I cannot remember the books I've read any more than the meals I have eaten; even so, they have made me.

~ Ralph Waldo Emerson

### #8Serapth  Members

Posted 16 October 2012 - 03:44 PM

int x, y = map[x][y];

I'm really curious what happens here. x is declared as an int without being defined, and y is declared as an int defined as map[x][y]...that's a recursive definition on top of an undefined index? It's been a while since I mucked with C++.
I guess it doesn't matter since x and y are unused after their first assignment in main. But I don't see how that's being resolved by the compiler.

Unless I am missing something, the array is improperly populated.
Both X and Y are declared as NULL
After MAIN they are both declared as map[2][2], which are both NULL
Also as it's written the values of X,Y will not update when the map is updated.

Corrected 2D array declaration.
int map[5][5] =
{
{1, 1, 1, 1, 1},
{1, 0, 0, 0, 1},
{1, 0, 0, 0, 1},
{1, 0, 0, 0, 1},
{1, 1, 1, 1, 1}
};


Actually, you can populate a 2D array that way. Both are functionally equivalent.

I am rather surprised that the code does compile though... there is a lot going on there that would require good knowledge of the spec to figure out what would happen.

Interesting lines are the aforementioned:
int x, y = map[x][y];


Which my brain is strugging to grok. I am kinda surprised that compiles. I believed C++ evaluated right to left, so it would encounter the y and x indexes that are undeclared.

The other fun line is:
while (mapn < 5, mapn2 < 5)

Which if my understand of C++ is correct, actually evaluates to
while(mapn2 < 5)

In that it would evaluate the first expression, throw it away, then evaluate the second expression as the condition of the loop.

Edited by Serapth, 16 October 2012 - 03:52 PM.

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.