#include <stdio.h>
#include <stdlib.h>
#include <sys/time.h>
#define NUMQUEST 20
#define NUMLIMIT 11
int main(void) {
char ops[] = {'+', '-', '*'};
int correct = 0;
struct timeval start, end;
gettimeofday(&start, NULL);
srandom((unsigned) start.tv_sec);
for (int i=0; i<NUMQUEST; i++) {
int a = random() % NUMLIMIT, b = random() % NUMLIMIT, c;
char op = ops[random() % 3];
if (op == '-' && a-b < 0) {int x = a; a = b; b = x; }
printf("%2d %c %2d = ", a, op, b);
_Bool truth;
if (scanf("%d", &c) != 1) truth = 0;
else switch (op) {
case '+': truth = (c == a+b); break;
case '-': truth = (c == a-b); break;
case '*': truth = (c == a*b); break;
}
if (truth) {correct++; printf(" CORRECT!\n"); }
else {printf(" WRONG!\n"); }
}
gettimeofday(&end, NULL);
double timeSpent = end.tv_sec - start.tv_sec + (end.tv_usec - start.tv_usec) / 1000000.0;
printf("You got %d out of %d problems correct in %.6f seconds.\n", correct, NUMQUEST, timeSpent);
return 0;
}