473,385 Members | 1,942 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

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_again == '\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("TORTOISE 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);

}
Nov 14 '05 #1
10 10609
Vince wrote:
while(play_again == '\n')


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)
Nov 14 '05 #2
Sam Halliday wrote:
Vince wrote:
while(play_again == '\n')

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)


That's a good tip! I'll try to remember that.
Nov 14 '05 #3
Vince wrote:
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.
This usage is illiterate and, frankly, stupid:

while(play_again == '\n')
{
while(i <= 69 && j <= 69)
{
tortoise[i] = 0;;
check = random_1(); [...]

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

Nov 14 '05 #4
Sam Halliday <em***@example.com> writes:
Vince wrote:
while(play_again == '\n')


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)


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_Keith) ks***@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.
Nov 14 '05 #5
Keith Thompson wrote:
Sam Halliday <em***@example.com> writes:
Vince wrote:
while(play_again == '\n')
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)


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.


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 `>'
(Some, but not all, compilers will warn you about the use of an
assignment operator in a condition.)


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
Nov 14 '05 #6
On 17 Jul 2004 17:42:49 -0700, wi*********@yahoo.com (Vince) wrote:
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.
I'm sorry but the only things taught by code are:

abuse of vertical white space
inconsistent indenting
bad logic
bad prompting
undefined behavior
snip int i = 0;

int j = 0;
snip if(check == 6 || check == 7)

tortoise[i-=6] = 0;
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 if(check_1 == 9 || check_1 == 10)

hare[j-=2] = 0;

if(j < 0){
If j can be less than 0, what does that say about the subscript in the
previous line?

snip if(i >= 69)

{

printf("TORTOISE 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;

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

snip
printf("Press enter");
You need either a \n in your message or a call to fflush().

scanf("%c", &play_again);


What should the user do to stop playing?

snip
<<Remove the del for email>>
Nov 14 '05 #7
In article <a1**************************@posting.google.com >, wilmguy2004
@yahoo.com says...
The following is code for the classic tortoise and hare C assignment.
Horrid.
Please do not plagiarize as comp sci instructors regularly read newsgroups.
Little or no chance of that happening in this case.
However, this program does help one learn about arrays and pointers.


Not really, no.

[snipped]

Nov 14 '05 #8
On 17 Jul 2004 17:42:49 -0700, wi*********@yahoo.com (Vince) wrote:
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.


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.

Nov 14 '05 #9
In article <vi********************************@4ax.com>, nick1@r-e-m-o-v-
e.nildram.co.uk says...
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.

[snip]
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 ) );
Why leave this srand() in here? Regardless of the method
using high/low order bits, etc. this shouldn't be here.
return ( 1 + rand() % 10 );
}

--
Randy Howard
To reply, remove FOOBAR.
Nov 14 '05 #10
Sam Halliday <em***@example.com> wrote:
Vince wrote:
while(play_again == '\n')


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)


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=variable_two) {
variable_one=next_value();
}

Oops...

Richard
Nov 14 '05 #11

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Shock | last post by:
Hi everybody, I have been getting so much good advice here that I figured I would post another question. I am still learning c++ from a text that I inherited from my brother. I am now working...
22
by: edgrsprj | last post by:
PROPOSED EARTHQUAKE FORECASTING COMPUTER PROGRAM DEVELOPMENT EFFORT Posted July 11, 2005 My main earthquake forecasting Web page is: http://www.freewebz.com/eq-forecasting/Data.html ...
11
by: christopher diggins | last post by:
I am wondering if any can point me to any open-source library with program objects for C++ like there is in Java? I would like to be able to write things like MyProgram1 >> MyProgram2 >>...
3
by: Bob E | last post by:
How can I insert a field name like O'Hare or O'Conner into a table. I would rather not have to check every field entered to see if the character ( ' ) is in the field. I'm entering Streets, Names,...
2
by: FUGATO | last post by:
How I can design a Hare and tortoise using pointer? Somebody can give an idea about that.
0
by: Buckaroo Banzai | last post by:
Hello, newbie here... I'm writing this program but when I click the start button which should initiate either the Hare or the Tortoise, it does not, this is the first time I use threads, so the...
2
Banfa
by: Banfa | last post by:
Posted by Banfa The previous tutorial discussed what programming is, what we are trying to achieve, the answer being a list of instructions constituting a valid program. Now we will discuss how...
41
by: c | last post by:
Hi every one, Me and my Cousin were talking about C and C#, I love C and he loves C#..and were talking C is ...blah blah...C# is Blah Blah ...etc and then we decided to write a program that...
2
by: sandeep | last post by:
hi we are using tortoise cvs and putty. i want to write a python script to whom i can provide a tag and module.now what this script will do is look for this specific tag and checks for whether...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.