473,805 Members | 2,164 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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);

}
Nov 14 '05 #1
10 10650
Vince wrote:
while(play_agai n == '\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_ag ain == '\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_agai n == '\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_agai n == '\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_Keit h) 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_agai n == '\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*********@yah oo.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("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;

}
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*********@yah oo.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

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

Similar topics

4
4127
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 on a problem that seems lame, but I really want it to work. It is a tortoise & hare sim. I am pretty sure I have everything right except printing the array. I cannot figure out how to print the array correctly. It should print the T & H one...
22
3615
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 Newsgroup Readers: If you circulate copies of this report to groups of computer programmers at different universities etc. around the world then they might find the subject matter to be interesting.
11
2615
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 >> Fork(MyProgram3, SomeFile); If not would this be something of interest to others? Thanks in advance,
3
1208
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, Clients, etc. into a table. Everything was going fine until the Name field (O'Hare) was entered. Can I somehow map the Chr$(39) to Chr$ (96)? Any suggestions?
2
1959
by: FUGATO | last post by:
How I can design a Hare and tortoise using pointer? Somebody can give an idea about that.
0
2702
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 problem might be the way I'm passing the value. can someone please take a look at this code and maybe give me some guidance on what I'm doing wrong. Please note there are 3 separate files with the corresponding classes, I separated it with ===>...
2
19357
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 we set about doing that. Every program starts with a specification, this may be a several hundred page document from your latest client or one small paragraph from your professor and pretty much anything in-between. The specification is very...
41
2719
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 will calculate the factorial of 10, 10 millions time and print the reusult in a file with the name log.txt.. I wrote something like this
2
2131
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 its a individual tag or its inside a branch.if its inside a branch then find out what is the branch tag and then check out that branch for me else it checks out that module with that tag. Actually the thing is i am not able to find the way how i...
0
9596
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10363
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10368
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10107
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
6876
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5544
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5678
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4327
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
3008
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.