Jump to content

  • Log In with Google      Sign In   
  • Create Account


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.

  • You cannot reply to this topic
7 replies to this topic

#1 Youbar   Members   -  Reputation: 122

Like
0Likes
Like

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

Sponsor:

#2 RulerOfNothing   Members   -  Reputation: 1141

Like
2Likes
Like

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)

#3 Youbar   Members   -  Reputation: 122

Like
0Likes
Like

Posted 15 October 2012 - 10:36 PM

Spoiler


Nvm

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


#4 wicked357   Members   -  Reputation: 1141

Like
1Likes
Like

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.


#5 Serapth   Crossbones+   -  Reputation: 5157

Like
0Likes
Like

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.

#6 BCullis   Crossbones+   -  Reputation: 1813

Like
0Likes
Like

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
DeviantArt :: Because right-brain needs love too

#7 Shippou   Members   -  Reputation: 1272

Like
0Likes
Like

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}
};

My YouTube Channel Filled With Geek, Nerd, Politics, Economics, & More !  
[Click Here]


#8 Serapth   Crossbones+   -  Reputation: 5157

Like
0Likes
Like

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.



PARTNERS