V
Vince
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 = 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);
}
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 = 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);
}