Sign in to follow this  
Youbar

Program that prints out array is not working

Recommended Posts

Youbar    122
[code]
#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");
}
[/code]

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. :(

Share this post


Link to post
Share on other sites
RulerOfNothing    1466
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)

Share this post


Link to post
Share on other sites
Youbar    122
[spoiler][s]Ah thanks, number isn't reset! That solved the problem! Thanks for pointing that out.

Well, now I'm trying to create a movement script for an ingame character:
[code]
prev = map[x][y];
cout << "Where would you like to go?" << endl;
cin >> input;
if (input == 8)
{
y--;
x, y == 2;
prev == 0;
goto map;
mapn = 0;
mapn2 = 0;
number = 0;
number2 = 0;
}
[/code]

But it seems to just automatically skip the loop again, despite it being reset...
Is there a way to fix this?

Sorry, but I'm just really new to C++ so my code probably looks extremely bad.[/spoiler][/s]

Nvm Edited by Youbar

Share this post


Link to post
Share on other sites
wicked357    2424
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:
[code]
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];
}
}
[/code] Edited by wicked357

Share this post


Link to post
Share on other sites
Serapth    6671
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.

Share this post


Link to post
Share on other sites
BCullis    1955
[quote name='Youbar' timestamp='1350359411' post='4990604']
int x, y = map[x][y];
[/quote]
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.

Share this post


Link to post
Share on other sites
RLS0812    3043
[quote name='BCullis' timestamp='1350405737' post='4990791']
[quote name='Youbar' timestamp='1350359411' post='4990604']
int x, y = map[x][y];
[/quote]
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.
[/quote]
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.
[CODE]
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}
};
[/CODE]

Share this post


Link to post
Share on other sites
Serapth    6671
[quote name='Shippou' timestamp='1350416954' post='4990858']
[quote name='BCullis' timestamp='1350405737' post='4990791']
[quote name='Youbar' timestamp='1350359411' post='4990604']
int x, y = map[x][y];
[/quote]
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.
[/quote]
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.
[CODE]
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}
};
[/CODE]
[/quote]

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:
[code]
int x, y = map[x][y];
[/code]

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

Share this post


Link to post
Share on other sites

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