/* Programmer: Greg Perkins Program: TTTutor.c Written: May 27, 1998 This program was designed with the young student in mind. It is a Times Table Tutor. It generates random times tables and gives the student two chances to get the right answer. If the student is unable to get the right answer, he/she is shown a 'walk-through' on the way to get the right answer. It continually gives the student words of encouragement to keep them working. */ #include void clear (void); main() { int num1, num2, answer, i, j; int sum, last_sum, num_correct; int again = 1, num_tries, num_problems; float score; char dummy, cont; srand( clock() ); /* seed the random number generator */ printf("Welcome to the Times Table Tutor!\n\n"); printf("I will help you learn your times tables.\n\n"); printf("I will give you ten times tables and keep score for you.\n"); printf("You will get two tries to answer the question correctly. If\n"); printf("you don't get the right answer after the first try, I will\n"); printf("give you a hint and then let you try again. If you don't get\n"); printf("the right answer the second time, I will show you how to solve\n"); printf("the problem by taking you through it step by step.\n\n"); printf("Now, let's have some fun...\n"); printf("Hit ENTER to continue...\n"); dummy = getchar(); while (again) { num_correct = 0; clear(); for (num_problems = 1; num_problems <= 10; num_problems++) { clear(); num1 = (rand() % 13); /* get a random number between 0 and 12 */ num2 = (rand() % 13); /* get a random number between 0 and 12 */ printf("\nProblem #%2d : %2d\n", num_problems, num1); printf(" X %2d\n", num2); printf(" ------\n"); printf("Enter Answer "); if ((num1*num2) < 10) printf(" "); /* align the answer correctly */ else if ((num1*num2) < 100) printf(" "); else printf(""); scanf("%d", &answer); dummy = getchar(); if (answer == (num1*num2)) { printf("\nYou are correct. Great job!!! I knew you could do it.\n"); num_correct++; } else { clear(); printf("Ooops!!! Better try that one again...\n"); if ((num1 == 0) || (num2 == 0)) { printf("\n***** SPECIAL CASE *****"); printf("\n\nHINT: 0 times ANY number ALWAYS equals zero.\n"); } else { printf("\n\nHINT: To find the correct answer, start with zero and add %d (%d times).\n", num1, num2); printf("Example: 0 + "); for (i = 1; i <= num2; i++) { printf("%d", num1); if (i == num2) printf(" = ???\n"); else printf(" + "); } } printf("\nLet's try that one again...\n"); printf("Problem #%2d : %2d\n", num_problems, num1); printf(" X %2d\n", num2); printf(" ------\n"); printf("Enter Answer "); if ((num1*num2) < 10) printf(" "); /* align the answer correctly */ else if ((num1*num2) < 100) printf(" "); else printf(""); scanf("%d", &answer); dummy = getchar(); if (answer == (num1*num2)) { printf("\nCorrect. Good job!!! Remember the hint, okay?\n"); num_correct++; } else /* two incorrect answers invokes the talk through */ { sum = last_sum = 0; clear(); printf("\nIt looks like you are having a little trouble...\n"); printf("We'll do this one together...Okay?\n"); printf("\nThe correct answer is : %d X %d = %d\n", num1, num2, num1*num2); printf("\nHere is how to find the answer:\n"); printf("We are going to start with zero and add %d (%d times).\n", num1, num2); printf("Hit ENTER after each step to continue...\n\n"); if (num2 == 0) { printf("Step %2d : %3d + %3d = %3d", 1, last_sum, last_sum, last_sum); dummy = getchar(); } for (j = 1; j <= num2; j++) { sum += num1; printf("Step %2d : %3d + %3d = %3d", j, last_sum, num1, sum); dummy = getchar(); last_sum = sum; } /* for loop */ printf("The correct answer is : %d\n\n", sum); printf("\nDon't worry!!! I know you'll get it the next time!!!\n"); printf("Hit ENTER to continue..."); dummy = getchar(); } } /* first answer incorrect else clause */ } /* num_tries loop */ /* display the final score */ clear(); score = (((float)num_correct/(float)(num_problems-1))*100); printf("\nNumber of Problems : %3d\n", num_problems-1); printf("Number Correct : %3d\n", num_correct); printf("Score (percentage) : % 3.1f%%\n\n\n", score); /* display final words of encouragement */ if (score == 100) printf("Outstanding!!! You are a times table genius!!! Keep up the hard work.\n"); else if (score < 60) printf("We need to practice some more. I say we do it again.\n"); else if (score < 70) printf("Pretty good, but we still need practice. Let's do it again...Okay?\n"); else if (score < 80) printf("Good job!!! Let's try to get 80%% next time...Okay?\n"); else if (score < 90) printf("Great job!!! With a little more practice, I know we can get 90%%!\n"); else printf("Terrific!!! You sure are good at your times tables!!!\n"); /* prompt the user to do it again */ printf("\nWould you like to do another set? (y/n)\n"); scanf("%c", &cont); dummy = getchar(); /* clean up the I/O stream */ if ((cont == 'y') || (cont == 'Y')) again = 1; else again = 0; } /* main while loop */ } void clear (void) { int k; for (k = 1; k < 20; k++) printf("\n\n\n"); }