train program written in C
Signed-off-by: Michael <michael.lindman@gmail.com>
This commit is contained in:
parent
d7c065b3f6
commit
9251bc8c3e
42
train/c/main.c
Normal file
42
train/c/main.c
Normal file
@ -0,0 +1,42 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <stdbool.h>
|
||||
#include <time.h>
|
||||
|
||||
int main() {
|
||||
while (true) {
|
||||
srand(time(0));
|
||||
printf("Train\n");
|
||||
printf("Creative Computing Morristown, New Jersey\n");
|
||||
printf("Time - Speed Distance Exercise:");
|
||||
int car = (rand() %(65-40+1))+40;
|
||||
int distance = (rand() %(20-5+1))+5;
|
||||
int train = (rand() %(39-20+1))+20;
|
||||
printf(" A car traveling %d MPH can make a certain trip in %d hours less than a train traveling at %d MPH.\n", car, distance, train);
|
||||
while (true) {
|
||||
char input[5];
|
||||
printf("\nHow long does a trip take by car ");
|
||||
fgets(input, 5, stdin);
|
||||
printf(input);
|
||||
double answer = atof(input);
|
||||
int car_time = distance * train / (car - train);
|
||||
int percent = abs(((car_time)-answer)*100/answer)+.5;
|
||||
if (percent > 5) {
|
||||
printf("Sorry, you were off by %d percent\n", percent);
|
||||
printf("Correct answer is %d hours\n", car_time);
|
||||
} else {
|
||||
printf("Good!, Answer within %d percent\n", percent);
|
||||
printf("Another Problem? (yes or no)");
|
||||
char newgame[5];
|
||||
fgets(newgame, 5,stdin);
|
||||
if (strcmp("yes", newgame)) {
|
||||
printf("!");
|
||||
break;
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
continue;
|
||||
}
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user