www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - why the array bounds array

reply Michael P. <baseball.mjp gmail.com> writes:
Okay, I'm getting an array bounds error, and I have no clue why. Here is the
code that affect it:

//Constants
const int SCREEN_WIDTH = 640;
const int SCREEN_HEIGHT = 480;
const int TILE_WIDTH = 20;
const int TILE_HEIGHT = 20; //how big one tile is, in pixels
const int NUMBER_OF_TILES_WIDTH = SCREEN_WIDTH / TILE_WIDTH;
const int NUMBER_OF_TILES_HEIGHT = SCREEN_HEIGHT / TILE_HEIGHT;
const int TYPES_OF_TILES = 4;

//variables
char[ NUMBER_OF_TILES_WIDTH ][ NUMBER_OF_TILES_HEIGHT ] tiles;
//set all tiles to random
for ( int i = 0; i < NUMBER_OF_TILES_WIDTH; i++ )
{
	for ( int j = 0; j < NUMBER_OF_TILES_HEIGHT; j++ )
	{
		tiles[ i ][ j ] = cast( char )( rand() & TYPES_OF_TILES ); //occurs here
	}
}

So, I'm not really sure why it's happening.... Anyone mind shedding some light
on why?

-Michael P.
Dec 07 2008
next sibling parent reply Michael P. <baseball.mjp gmail.com> writes:
Michael P. Wrote:

 Okay, I'm getting an array bounds error, and I have no clue why. Here is the
code that affect it:
 
 //Constants
 const int SCREEN_WIDTH = 640;
 const int SCREEN_HEIGHT = 480;
 const int TILE_WIDTH = 20;
 const int TILE_HEIGHT = 20; //how big one tile is, in pixels
 const int NUMBER_OF_TILES_WIDTH = SCREEN_WIDTH / TILE_WIDTH;
 const int NUMBER_OF_TILES_HEIGHT = SCREEN_HEIGHT / TILE_HEIGHT;
 const int TYPES_OF_TILES = 4;
 
 //variables
 char[ NUMBER_OF_TILES_WIDTH ][ NUMBER_OF_TILES_HEIGHT ] tiles;
 //set all tiles to random
 for ( int i = 0; i < NUMBER_OF_TILES_WIDTH; i++ )
 {
 	for ( int j = 0; j < NUMBER_OF_TILES_HEIGHT; j++ )
 	{
 		tiles[ i ][ j ] = cast( char )( rand() & TYPES_OF_TILES ); //occurs here
 	}
 }
 
 So, I'm not really sure why it's happening.... Anyone mind shedding some light
on why?
 
 -Michael P.
I meant array bounds error, not array bounds array. :P
Dec 07 2008
next sibling parent "Bill Baxter" <wbaxter gmail.com> writes:
On Mon, Dec 8, 2008 at 11:22 AM, Michael P. <baseball.mjp gmail.com> wrote:
 Michael P. Wrote:

 Okay, I'm getting an array bounds error, and I have no clue why. Here is the
code that affect it:

 //Constants
 const int SCREEN_WIDTH = 640;
 const int SCREEN_HEIGHT = 480;
 const int TILE_WIDTH = 20;
 const int TILE_HEIGHT = 20; //how big one tile is, in pixels
 const int NUMBER_OF_TILES_WIDTH = SCREEN_WIDTH / TILE_WIDTH;
 const int NUMBER_OF_TILES_HEIGHT = SCREEN_HEIGHT / TILE_HEIGHT;
 const int TYPES_OF_TILES = 4;

 //variables
 char[ NUMBER_OF_TILES_WIDTH ][ NUMBER_OF_TILES_HEIGHT ] tiles;
 //set all tiles to random
 for ( int i = 0; i < NUMBER_OF_TILES_WIDTH; i++ )
 {
       for ( int j = 0; j < NUMBER_OF_TILES_HEIGHT; j++ )
       {
               tiles[ i ][ j ] = cast( char )( rand() & TYPES_OF_TILES );
//occurs here
       }
 }

 So, I'm not really sure why it's happening.... Anyone mind shedding some light
on why?

 -Michael P.
I meant array bounds error, not array bounds array. :P
Your indices are backwards. --bb
Dec 07 2008
prev sibling parent Kagamin <spam here.lot> writes:
Michael P. Wrote:

 I meant array bounds error, not array bounds array. :P
http://www.digitalmars.com/d/2.0/arrays.html learn prefix and postfix syntax.
Dec 08 2008
prev sibling next sibling parent reply BCS <ao pathlink.com> writes:
Reply to Michael P.,

rand() & TYPES_OF_TILES

never use rand like that (the low order bit on many rands toggles every single 
time) 

better (I think):

rand() / (TYPES_OF_TILES / RAND_MAX)
Dec 07 2008
parent reply "Bill Baxter" <wbaxter gmail.com> writes:
On Mon, Dec 8, 2008 at 2:57 PM, BCS <ao pathlink.com> wrote:
 Reply to Michael P.,

 rand() & TYPES_OF_TILES

 never use rand like that (the low order bit on many rands toggles every
 single time)
 better (I think):

 rand() / (TYPES_OF_TILES / RAND_MAX)
That's a divide by zero, so I don't think that's what you meant. --bb
Dec 07 2008
parent reply BCS <ao pathlink.com> writes:
Reply to Bill,

 On Mon, Dec 8, 2008 at 2:57 PM, BCS <ao pathlink.com> wrote:
 
 Reply to Michael P.,
 
 rand() & TYPES_OF_TILES
 
 never use rand like that (the low order bit on many rands toggles
 every
 single time)
 better (I think):
 rand() / (TYPES_OF_TILES / RAND_MAX)
 
That's a divide by zero, so I don't think that's what you meant. --bb
Oops x-p rand() / (RAND_MAX / TYPES_OF_TILES)
Dec 07 2008
parent reply Sergey Gromov <snake.scaly gmail.com> writes:
Mon, 8 Dec 2008 06:59:40 +0000 (UTC), BCS wrote:

 Reply to Bill,
 
 On Mon, Dec 8, 2008 at 2:57 PM, BCS <ao pathlink.com> wrote:
 
 Reply to Michael P.,
 
 rand() & TYPES_OF_TILES
 
 never use rand like that (the low order bit on many rands toggles
 every
 single time)
 better (I think):
 rand() / (TYPES_OF_TILES / RAND_MAX)
 
That's a divide by zero, so I don't think that's what you meant. --bb
Oops x-p rand() / (RAND_MAX / TYPES_OF_TILES)
Don't use rand() like this, ever. ;) "RAND_MAX / TYPES_OF_TILES" is an integer expression, it rounds down, therefore your formula gives a slightly greater range of values than you expect. You'll get all sorts of out of bounds errors. Your best bet is to use a uniform distribution method of the same random number generator implementation. There is std.random.uniform() in D2's Phobos which serves this purpose. If you don't have an equivalent, your next stop is "rand() % TYPES_OF_TILES". It's not strictly uniform but close if TYPES_OF_TILES is small.
Dec 08 2008
parent BCS <ao pathlink.com> writes:
Reply to Sergey,

 Mon, 8 Dec 2008 06:59:40 +0000 (UTC), BCS wrote:
 better (I think):
rand() / (RAND_MAX / TYPES_OF_TILES)
 Don't use rand() like this, ever.  ;)  "RAND_MAX / TYPES_OF_TILES" is
 an integer expression, it rounds down, therefore your formula gives a
 slightly greater range of values than you expect.  You'll get all
 sorts of out of bounds errors.
thus the (I think). I was just to lazy to figure out the rounding. the idea still work as long as you round up. rand() / ( RAND_MAX / TYPES_OF_TILES + !!(RAND_MAX % TYPES_OF_TILES)) OTOH using someone else's code that does the same thing is always better.
 Your best bet is to use a uniform distribution method of the same
 random number generator implementation.  There is std.random.uniform()
 in D2's Phobos which serves this purpose.  If you don't have an
 equivalent, your next stop is "rand() % TYPES_OF_TILES".  It's not
 strictly uniform but close if TYPES_OF_TILES is small.
if TYPES_OF_TILES is to small (like 4) it start s getting highly un random again.
 
Dec 08 2008
prev sibling parent Zarathustra <adam.chrapkowski gmail.com> writes:
Michael P. Wrote:

 Okay, I'm getting an array bounds error, and I have no clue why. Here is the
code that affect it:
 
 //Constants
 const int SCREEN_WIDTH = 640;
 const int SCREEN_HEIGHT = 480;
 const int TILE_WIDTH = 20;
 const int TILE_HEIGHT = 20; //how big one tile is, in pixels
 const int NUMBER_OF_TILES_WIDTH = SCREEN_WIDTH / TILE_WIDTH;
 const int NUMBER_OF_TILES_HEIGHT = SCREEN_HEIGHT / TILE_HEIGHT;
 const int TYPES_OF_TILES = 4;
 
 //variables
 char[ NUMBER_OF_TILES_WIDTH ][ NUMBER_OF_TILES_HEIGHT ] tiles;
 //set all tiles to random
 for ( int i = 0; i < NUMBER_OF_TILES_WIDTH; i++ )
 {
 	for ( int j = 0; j < NUMBER_OF_TILES_HEIGHT; j++ )
 	{
 		tiles[ i ][ j ] = cast( char )( rand() & TYPES_OF_TILES ); //occurs here
 	}
 }
 
 So, I'm not really sure why it's happening.... Anyone mind shedding some light
on why?
 
 -Michael P.
for ( int i = 0; i < NUMBER_OF_TILES_WIDTH; i++ ) for( int j = 0; j < NUMBER_OF_TILES_HEIGHT; j++ ) // tiles[ i ][ j ] = cast( char )( rand() & TYPES_OF_TILES ); //occurs here tiles[ j ][ i ] = cast( char )( rand() & TYPES_OF_TILES ); // swop j and i
Dec 08 2008