tortoise and hare program code

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Vince

    #1

    tortoise and hare program code

    The following is code for the classic tortoise and hare C assignment.
    I am posting this for educational purposes only. Please do not
    plagiarize as comp sci instructors regularly read newsgroups.
    However, this program does help one learn about arrays and pointers.

    #include<stdio. h>

    #include<stdlib .h>

    #include<time.h >



    int random_1();



    int main()



    {

    int i = 0;

    int j = 0;

    int check;

    int check_1;

    char play_again = '\n' ;



    int tortoise[70];

    int hare[70];



    printf("BANG !!!!!\n");

    printf("AND THEY'RE OFF !!!!!\n");





    while(play_agai n == '\n')

    {



    while(i <= 69 && j <= 69)

    {



    tortoise[i] = 0;;

    check = random_1();



    if(check == 1 || check == 2 || check == 3 || check == 4 || check ==
    5)

    tortoise[i+=3] = 0;



    if(check == 6 || check == 7)

    tortoise[i-=6] = 0;



    if(check == 8 || check == 9 || check == 10)

    tortoise[i+=1] = 0;



    char *ptr;

    char line[71] = "----------------------------------------------------------------------";



    ptr = line;



    if(i < 0){

    i = 0;

    *(ptr + i) = 'T';}



    *(ptr + i) = 'T';



    hare[j] = 0;

    check_1 = random_1();



    if(check_1 == 1 || check_1 == 2)

    hare[j] = 0;



    if(check_1 == 3 || check_1 == 4)

    hare[j+=9] = 0;



    if(check_1 == 5)

    hare[j+=12] = 0;



    if(check_1 == 6 || check_1 == 7 || check_1 == 8)

    hare[j++] = 0;



    if(check_1 == 9 || check_1 == 10)

    hare[j-=2] = 0;



    if(j < 0){

    j = 0;

    *(ptr + j) = 'H';}



    *(ptr + j) = 'H';



    if(i == j){

    *(ptr + i) = 'O';

    *(ptr + i + 1) = 'U';

    *(ptr + i + 2) = 'C';

    *(ptr + i + 3) = 'H';}



    printf("%s\n\n" , line);



    break;



    }





    if(i >= 69)

    {

    printf("TORTOIS E WINS!!! YAY!!!\n");

    break;

    }



    if(j >= 69)

    {

    printf("HARE WINS. YUCH.\n");

    break;

    }



    if(j >= 69 && i >= 69)

    {

    printf("IT'S A TIE\n");

    break;

    }



    printf("Press enter");

    scanf("%c", &play_again) ;



    }



    return 0;





    }

    int random_1()



    {

    srand(time(NULL ));

    return (1 + rand() % 10);

    }
  • Sam Halliday

    #2
    Re: tortoise and hare program code

    Vince wrote:[color=blue]
    > while(play_agai n == '\n')[/color]

    just a little thing someone once taught me: when doing equality tests... put the
    variable on the right instead of the left. that means if you typo a single '='
    instead of '=='... you get a compiler error, instead of a bug in your program.

    while('\n' == play_again)

    Comment

    • Kenneth Lantrip

      #3
      Re: tortoise and hare program code

      Sam Halliday wrote:[color=blue]
      > Vince wrote:
      >[color=green]
      >>while(play_ag ain == '\n')[/color]
      >
      >
      > just a little thing someone once taught me: when doing equality tests... put the
      > variable on the right instead of the left. that means if you typo a single '='
      > instead of '=='... you get a compiler error, instead of a bug in your program.
      >
      > while('\n' == play_again)[/color]

      That's a good tip! I'll try to remember that.

      Comment

      • Martin Ambuhl

        #4
        Re: tortoise and hare program code

        Vince wrote:[color=blue]
        > The following is code for the classic tortoise and hare C assignment.
        > I am posting this for educational purposes only. Please do not
        > plagiarize as comp sci instructors regularly read newsgroups.
        > However, this program does help one learn about arrays and pointers.[/color]

        This usage is illiterate and, frankly, stupid:

        [color=blue]
        > while(play_agai n == '\n')
        > {
        > while(i <= 69 && j <= 69)
        > {
        > tortoise[i] = 0;;
        > check = random_1();[/color]
        [...]

        [color=blue]
        > int random_1()
        > {
        > srand(time(NULL ));
        > return (1 + rand() % 10);
        > }[/color]

        Comment

        • Keith Thompson

          #5
          Re: tortoise and hare program code

          Sam Halliday <email@example. com> writes:[color=blue]
          > Vince wrote:[color=green]
          > > while(play_agai n == '\n')[/color]
          >
          > just a little thing someone once taught me: when doing equality
          > tests... put the variable on the right instead of the left. that
          > means if you typo a single '=' instead of '=='... you get a compiler
          > error, instead of a bug in your program.
          >
          > while('\n' == play_again)[/color]

          This advice is extremely controversial. Some people like it for the
          reason you cite, others (including myself) think it makes the code
          more difficult to read.

          (Some, but not all, compilers will warn you about the use of an
          assignment operator in a condition.)

          --
          Keith Thompson (The_Other_Keit h) kst-u@mib.org <http://www.ghoti.net/~kst>
          San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
          We must do something. This is something. Therefore, we must do this.

          Comment

          • Sam Halliday

            #6
            Re: tortoise and hare program code

            Keith Thompson wrote:[color=blue]
            > Sam Halliday <email@example. com> writes:[color=green]
            > > Vince wrote:[color=darkred]
            > > > while(play_agai n == '\n')[/color]
            > >
            > > just a little thing someone once taught me: when doing equality
            > > tests... put the variable on the right instead of the left. that
            > > means if you typo a single '=' instead of '=='... you get a compiler
            > > error, instead of a bug in your program.
            > >
            > > while('\n' == play_again)[/color]
            >
            > This advice is extremely controversial. Some people like it for the
            > reason you cite, others (including myself) think it makes the code
            > more difficult to read.[/color]

            really? i can't say i've ever noticed it making code more unreadable. however it
            *is* unreadable if i start doing it with things like

            if ( 13 > play_again )

            but there is no advantage to doing such an ordering. in fact when doing
            greater/lesser than checks, i always prefer to put the lower value on the left.
            guess its just the mathematician in me...

            if ( 0 < play_again && play_again < 13 )

            i try never to use `>'
            [color=blue]
            > (Some, but not all, compilers will warn you about the use of an
            > assignment operator in a condition.)[/color]

            with gcc, you are correct. but it does involve using the -Wall flag, which so
            many people (unfortunately) do not use.

            th.c: In function `main':
            th.c:45: warning: suggest parentheses around assignment used as truth value

            Comment

            • Barry Schwarz

              #7
              Re: tortoise and hare program code

              On 17 Jul 2004 17:42:49 -0700, wilmguy2004@yah oo.com (Vince) wrote:
              [color=blue]
              >The following is code for the classic tortoise and hare C assignment.
              >I am posting this for educational purposes only. Please do not
              >plagiarize as comp sci instructors regularly read newsgroups.
              >However, this program does help one learn about arrays and pointers.[/color]

              I'm sorry but the only things taught by code are:

              abuse of vertical white space
              inconsistent indenting
              bad logic
              bad prompting
              undefined behavior
              [color=blue]
              >[/color]
              snip[color=blue]
              > int i = 0;
              >
              > int j = 0;
              >[/color]
              snip[color=blue]
              > if(check == 6 || check == 7)
              >
              > tortoise[i-=6] = 0;[/color]

              What makes you think this is a valid index (between 0 and 69)? What
              happens if the first random number is 6 or 7?

              snip[color=blue]
              > if(check_1 == 9 || check_1 == 10)
              >
              > hare[j-=2] = 0;
              >
              >
              >
              > if(j < 0){[/color]

              If j can be less than 0, what does that say about the subscript in the
              previous line?

              snip[color=blue]
              > if(i >= 69)
              >
              > {
              >
              > printf("TORTOIS E WINS!!! YAY!!!\n");
              >
              > break;
              >
              > }
              >
              >
              >
              > if(j >= 69)
              >
              > {
              >
              > printf("HARE WINS. YUCH.\n");
              >
              > break;
              >
              > }
              >
              >
              >
              > if(j >= 69 && i >= 69)
              >
              > {
              >
              > printf("IT'S A TIE\n");
              >
              > break;
              >
              > }[/color]

              If this if is true, you can never reach it. You will always credit
              the tortoise even if it's a tie.

              snip
              [color=blue]
              > printf("Press enter");[/color]

              You need either a \n in your message or a call to fflush().
              [color=blue]
              >
              > scanf("%c", &play_again) ;[/color]

              What should the user do to stop playing?

              snip


              <<Remove the del for email>>

              Comment

              • Randy Howard

                #8
                Re: tortoise and hare program code

                In article <a102c76d.04071 71642.35419587@ posting.google. com>, wilmguy2004
                @yahoo.com says...[color=blue]
                > The following is code for the classic tortoise and hare C assignment.[/color]

                Horrid.
                [color=blue]
                > Please do not plagiarize as comp sci instructors regularly read newsgroups.[/color]

                Little or no chance of that happening in this case.
                [color=blue]
                > However, this program does help one learn about arrays and pointers.[/color]

                Not really, no.

                [snipped]

                Comment

                • Nick Austin

                  #9
                  Re: tortoise and hare program code

                  On 17 Jul 2004 17:42:49 -0700, wilmguy2004@yah oo.com (Vince) wrote:
                  [color=blue]
                  >The following is code for the classic tortoise and hare C assignment.
                  >I am posting this for educational purposes only. Please do not
                  >plagiarize as comp sci instructors regularly read newsgroups.
                  >However, this program does help one learn about arrays and pointers.[/color]

                  What a badly written heap of junk. The only pointer I can see is the
                  variable ptr, but it always points to &line[0] to I don't see what
                  it is supposed to illustrate.

                  I don't the style of placing closing braces on the same line as a
                  statement.

                  There are several variables that are written but not otherwise used.

                  There are numerous instances of writing to either before or off the
                  end of an array.

                  I've cleaned up the program and also split the program into functions
                  to make it more readable. The choice of variables names is poor but
                  I've kept them the same as the original to aid comparision. I've also
                  kept the magic numbers which should be #defines.

                  #include <stdio.h>
                  #include <stdlib.h>
                  #include <time.h>

                  int random_1( void );
                  int move_tortoise( int );
                  int move_hare( int );
                  int end_condition( int, int );

                  int main( void )
                  {
                  int i = 0;
                  int j = 0;
                  char play_again = '\n';

                  printf( "BANG !!!!!\n" );
                  printf( "AND THEY'RE OFF !!!!!\n" );

                  while ( play_again == '\n' )
                  {
                  while ( i <= 69 && j <= 69 )
                  {
                  char line[ 71 ] =
                  "----------------------------------------------------------------------";

                  i = move_tortoise( i );
                  j = move_hare( j );

                  if ( i == j )
                  {
                  /* problem here if i > 66 */
                  line[ i + 0 ] = 'O';
                  line[ i + 1 ] = 'U';
                  line[ i + 2 ] = 'C';
                  line[ i + 3 ] = 'H';
                  }
                  else
                  {
                  /* problem here if either i > 70 or j > 70 */
                  line[ i ] = 'T';
                  line[ j ] = 'H';
                  }

                  printf( "%s\n\n", line );

                  break;
                  }

                  if ( end_condition( i, j ) )
                  break;

                  printf( "Press enter" );
                  scanf( "%c", &play_again );
                  }
                  return 0;
                  }

                  int random_1( void )
                  {
                  /* This is a poor way to generate random numbers.
                  A better way is here:
                  http://www.eskimo.com/~scs/C-faq/q13.16.html */

                  srand( time( NULL ) );
                  return ( 1 + rand() % 10 );
                  }

                  int move_tortoise( int i )
                  {
                  int check = random_1();

                  if ( check == 1 || check == 2 || check == 3 || check == 4 || check
                  == 5 )
                  i += 3;

                  if ( check == 6 || check == 7 )
                  i -= 6;

                  if ( check == 8 || check == 9 || check == 10 )
                  i += 1;

                  if ( i < 0 )
                  {
                  i = 0;
                  }
                  return i;
                  }

                  int move_hare( int j )
                  {
                  int check = random_1();

                  if ( check == 3 || check == 4 )
                  j += 9;

                  if ( check == 5 )
                  j += 12;

                  if ( check == 6 || check == 7 || check == 8 )
                  j++;

                  if ( check == 9 || check == 10 )
                  j -= 2;

                  if ( j < 0 )
                  {
                  j = 0;
                  }
                  return j;
                  }

                  int end_condition( int i, int j )
                  {
                  if ( j >= 69 && i >= 69 )
                  {
                  printf( "IT'S A TIE\n" );
                  return 1;
                  }
                  else if ( i >= 69 )
                  {
                  printf( "TORTOISE WINS!!! YAY!!!\n" );
                  return 1;
                  }
                  else if ( j >= 69 )
                  {
                  printf( "HARE WINS. YUCH.\n" );
                  return 1;
                  }
                  return 0;
                  }

                  Nick.

                  Comment

                  • Randy Howard

                    #10
                    Re: tortoise and hare program code

                    In article <viflf0t37886o0 vq7csq69qavk0p8 bq8gq@4ax.com>, nick1@r-e-m-o-v-
                    e.nildram.co.uk says...[color=blue]
                    > I've cleaned up the program and also split the program into functions
                    > to make it more readable. The choice of variables names is poor but
                    > I've kept them the same as the original to aid comparision. I've also
                    > kept the magic numbers which should be #defines.
                    >[/color]

                    [snip]
                    [color=blue]
                    > int random_1( void )
                    > {
                    > /* This is a poor way to generate random numbers.
                    > A better way is here:
                    > http://www.eskimo.com/~scs/C-faq/q13.16.html */
                    >
                    > srand( time( NULL ) );[/color]

                    Why leave this srand() in here? Regardless of the method
                    using high/low order bits, etc. this shouldn't be here.
                    [color=blue]
                    > return ( 1 + rand() % 10 );
                    > }[/color]


                    --
                    Randy Howard
                    To reply, remove FOOBAR.

                    Comment

                    • Richard Bos

                      #11
                      Re: tortoise and hare program code

                      Sam Halliday <email@example. com> wrote:
                      [color=blue]
                      > Vince wrote:[color=green]
                      > > while(play_agai n == '\n')[/color]
                      >
                      > just a little thing someone once taught me: when doing equality tests... put the
                      > variable on the right instead of the left. that means if you typo a single '='
                      > instead of '=='... you get a compiler error, instead of a bug in your program.
                      >
                      > while('\n' == play_again)[/color]

                      And then, of course, you'll get in the habit of being very careful which
                      you put on the left side of the operator, but forgetting to check
                      whether you mistyped, because the compiler does that for you with this
                      trick, right? Wrong.

                      while (variable_one=v ariable_two) {
                      variable_one=ne xt_value();
                      }

                      Oops...

                      Richard

                      Comment

                      Working...