Useless 2D Array Problem

Started by
5 comments, last by Zahlman 17 years, 1 month ago
Hey guys, I'm trying to split my openGL window up into a grid, so I can later use it for BFS and DFS algorithms. However I'm trying to set the array first to contain all the coordinates for it I've set up 4 Constant Values as follows

//Screen Width
#define SCREEN_WIDTH	60
#define HALF_WIDTH	30

//Screen Height
#define SCREEN_HEIGHT	56
#define HALF_HEIGHT	28


Then these are used when I try and fill the array, which I do by:

void gameWorld::initGraph(sMap *simMap)
{	
	for(int i = HALF_HEIGHT - SCREEN_HEIGHT; i < HALF_HEIGHT; i++)
	{
		for(int j = HALF_WIDTH - SCREEN_WIDTH; j < HALF_WIDTH; j++)
		{
			simMap->iMap[j = j];
		}
	} 
}

</pre></div><!–ENDSCRIPT–>

So I'm looking for it to go from -30 to 30 in <span style="font-weight:bold;">, and -28 to 28 in the [j]

However when I print out the array, all the values are set to -858993460, which I'm assuming is what an int is set to when its not been initialised.

I'm assuming its to do with this line:

<!–STARTSCRIPT–><!–source lang="cpp"–><div class="source"><pre>
simMap-&gt;iMap[j = j];

<span class="cpp-comment">//I'm thinking this wouldnt work either</span>
simMap-&gt;iMap<span style="font-weight:bold;">[j] = i, j


</pre></div><!–ENDSCRIPT–>

But not sure how it should be, simple thing that someone will no doubt fix in a second, but its been a long day and I've hit a mental block lol

Any help will be appreciated, cheers
Advertisement
Can you further explain what you're trying to do? It looks like not only is your code very wrong, but that you're trying to do something that you can't (which means you can't do this directly).
[TheUnbeliever]
Wait...what? What are you trying to do
Sounds like you want two arrays, not a 2D array.

Each element of a 2D array only holds one value, like:

int x[10][10];
x[2][3]=25;

If, as you say, you want one array running -30 to +30 and another one running -28 to +28, you need:

int a[61];int b[57];for(int i=0;i<61;++i) a=i-30;for(int i=0;i<57;++i) b=i-28;


But then given how easy it is to compute the contents based on the index, why would you want to store these in an array?

I mean, for the first one, doing:

int v=a;

is equivalent to doing

int v=i-30;

and there is probably no performance gain at all from doing lookup on a simple operation like that.

2D arrays are normally used to store grid-like structures, such as cells in a map, where you want to index a single value by a row and column.
lol thought that might not explain it properly

Right, I'll do my best to explain it fully. I've got a GL window open thats running several objects moving around in a 30x28 space.

I'm trying to work out the best way to implement some AI search algorithms, and thinking the best way to start off going about it is by splitting the space that the objects move about in, into a grid.

So I want to make an array with all the possible points in that the objects could be at, hence wanting to go on the X value, between -30 and 30. Then on the Y axis -28 to 28.

Once I've got this set up, I can then go about using this array with all the points in for the search algorithm implementation.

I hope that explains it better, explaining things has never been my strong point :P
I think I understand what you mean.

If you want a 2D array of co-ordinates, you can create a structure:

struct Coord{    Coord(int x=0,int y=0) : X(x),Y(y) { }    int X,Y;};


then create a 2D array of these structures:

Coord Map[10][10];


then assign to each one in a loop like:

for(int Y=0;Y<10;++Y)    {    for(int X=0;X<10;++X) Map[X][Y]=Coord(X-5,Y-8); // or whatever    }


HTH
Quote:Original post by Steedie
lol thought that might not explain it properly

Right, I'll do my best to explain it fully. I've got a GL window open thats running several objects moving around in a 30x28 space.

I'm trying to work out the best way to implement some AI search algorithms, and thinking the best way to start off going about it is by splitting the space that the objects move about in, into a grid.

So I want to make an array with all the possible points in that the objects could be at, hence wanting to go on the X value, between -30 and 30. Then on the Y axis -28 to 28.

Once I've got this set up, I can then go about using this array with all the points in for the search algorithm implementation.

I hope that explains it better, explaining things has never been my strong point :P


What is the search algorithm you are planning to use? (Is it a well known one, with a name that could be looked up in Wikipedia, say?)

What should the size of the array be? Why?

What should the element type of the array be? Why? (I.e., what kind of thing do you want to store within each position in the array?)

For a given element of the array, what value should be there? Why? (I.e., for each column of each row of the array, ask the question: "which thing, of the kind from the previous question, should be here?")



Also, those #defines are NOT "constant values". C++ (and, for some time now, C) offers a built-in language feature for defining constant values, which looks almost exactly like you'd expect...

//Screen Widthconst int SCREEN_WIDTH = 60;const int HALF_WIDTH = SCREEN_WIDTH / 2;//Screen Heightconst int SCREEN_HEIGHT = 56;const int HALF_HEIGHT = SCREEN_HEIGHT / 2;


and the results have (a) a type and (b) a scope, because they are real values - real *concepts in your program* - as opposed to mere #defines, which are instructions to a text search-and-replace engine.

You'll notice I defined the HALF_ constants in terms of the other values. This is for two reasons here:

1) Because you should normally do things like this anyway: when two constants should have a mathematical relationship, expose that relationship when you set them. That way, (a) if you need to change the constants, you can't forget to update one when you update the other; (b) you document the relationship - it's right there, so the person reading the code can see what it is and whether it makes sense.

2) To illustrate that you can do this just fine with real constants, without any mucking around with extra parentheses or anything. Macros have problems with these because they are just instructions to a text search-and-replace engine (without extra parentheses, things can get messed up because of order of operations, along with the context into which the "constant" is inserted).

Don't use automated text search-and-replace hacks. Use a built-in feature of the language. Please. :)

This topic is closed to new replies.

Advertisement