Monday, July 27, 2009

Help with Basic Arrays and Pointers Bloodshed Dev-C++?

Start each animal at square 1 of 70 squares (each square represents possible position. Use variables to keep track of position and if animals slip before square 1 then move them back to square one. Generate %age's by using a random number generator in the range of 1%26lt;= i %26lt;= 10.





Tortoise: Fast plod 1%26lt;= i %26lt;= 5 moves 3 squares right


Slip 6%26lt;= i %26lt;=7 moves 6 squares left


Slow Plod 8%26lt;= i %26lt;=10 moves 1 square right





Hare: sleep 1%26lt;= i%26lt;= 2 no move


Big Hop 3%26lt;= i%26lt;= 4 9 squares right


small hop 5%26lt;= i %26lt;=7 1 square right


Big Slip i == 8 12 squares left


small slip 9%26lt;= i%26lt;=10 2 squares left





Need to print a 70 position line on each loop showing position T (Tortoise), H (Hare). If both land on same square print "OUCH" at that position. all print position should be blank except for T, H and ouch. After each loop test if either animal %26gt;=70 if so print who wins and terminate the program.

Help with Basic Arrays and Pointers Bloodshed Dev-C++?
Yep smells like homework. This should get you started though you will have to do a bit of work to make it work.





#include %26lt;unistd.h%26gt;


#include %26lt;stdio.h%26gt;





#define TURTLE 0


#define HARE 1





/* movements for each based on random value % 10 */


static int hmovement[] = { 0, 0, 9, 9, 9, 1, 1, -12, -2, -2 };


static int tmovement[] = { 3, 3, 3, 3, 3, -6, -6, 1, 1, 1 };





/* fill from current position to end of course but does not push


the ending | past col 70 */


void blank_fill( int i, int not_after )


{


if( i %26lt; not_after )


{


for( i; i %26lt; 69; i++ )


printf( " " );


printf( "|" );


}


printf( "\n" );


}





/* print current state, uses | to mark beginning and end of course*/


void print_pos( int t, int h )


{


int i;





/*printf( "%3d/%3d|", h, t );*/


printf( "|" );


for( i = 0; i %26lt; t %26amp;%26amp; i %26lt; h; i++ )


printf( " " );





if( t == h )


{


printf( "OUCH!" );


blank_fill( i+4, 65 );


return;


}





printf( "%s", t %26gt; h ? "H" : "T" );


for( i++; i %26lt; t || i %26lt; h; i++ )


printf( " " );


printf( "%s", t %26gt; h ? "T" : "H" );





blank_fill( i, 70 );


}





/* compute a move for one or the other */


int move( int who )


{


long r;





r = (lrand48( ) % 10);


return who == TURTLE ? tmovement[r] : hmovement[r];


}





/* driver for the race */


void race( )


{


int turt = 0;


int hare = 0;





srand48( time( NULL ) );


while( turt %26lt; 70 %26amp;%26amp; hare %26lt; 70 )


{


if( (turt += move( TURTLE )) %26lt; 0 )


turt = 0;


if( turt %26gt; 70 )


turt = 70;





if( (hare += move( HARE)) %26lt; 0 )


hare = 0;


if( hare %26gt; 70 )


hare = 70;





print_pos( turt, hare );


sleep( 1 ); /* makes it more interesting to watch */


}


}
Reply:Sounds like a homework question to me!


No comments:

Post a Comment