The following program actually gives me two errors: The first is that the random generation works only SOMETIMES (no consistent value for aMax that will make it work correctly). The second problem is the MOLASSES speed of choosing the random numbers.
The purpose of the program is to allow the user to input a "maximum sum" : the user enters 20. The program should choose random addends whose sums total NO MORE THAN 20 (the maximum sum the user entered). Is there an optimized way of doing this. One sort of came into my mind, but I never played with it, so therefore never figured out exactly how it would work. It would involve choosing b to between between a number and 30-a
Anyways...any help is much appreciated. I'd like the program to be faster (problem #2), but my main concern is to make it work at ALL (#1).
Thanks for the help!
Code:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
void addTest(int);
void subTest(int);
void multTest(int);
void setup(void);
int prompt = 1;
int aMax=, sMax, mMax;
int main() {
int choice;
if ( prompt == 1) {
prompt = 0;
srand(time(NULL));
setup();
}
else {
system("cls");
printf("\n\nMath Skills Menu:");
printf("\n\t1. Addition (0-%d)" , aMax);
printf("\n\t2. Subtraction (0-%d)" , sMax);
printf("\n\t3. Multiplication (0-%d)" , mMax);
printf("\n\t4. Set up ranges");
printf("\n\t5. Exit");
printf("\n\nEnter a selection: ");
scanf("%d" , &choice);
switch (choice) {
case 1:
addTest(aMax);
main();
break;
case 2:
//subTest(sMax);
break;
case 3:
//multTest(mMax);
break;
case 4:
setup();
break;
case 5:
printf("\n\nGoodbye!\n\n");
system("pause");
break;
default:
printf("\n\nInvalid selection. Try again!");
system("pause");
return 0;
main();
} //end switch
} //end else
} //end main()
void addTest(int aMax) {
int a[20], b[20], resp;
int right, wrong=0, percent;
int i;
system("cls");
printf("\nEnter -1 at any time to return to the menu.\n\n");
for ( i=0; i<20; i++ ) {
a[i] = (rand() % aMax) + 1;
do {
b[i] = (rand() % aMax) + 1;
} while ( (a[i] + b[i]) > aMax );
}
for ( i=0; i<20 && resp!=(-1); i++) {
start:
printf("\n");
printf("#%d. %d + %d = " , i+1, a[i], b[i]);
scanf("%d" , &resp);
if ( resp == -1 ) {
break;
}
else if ( resp != (a[i]+b[i]) ) {
printf(" Try again!\n");
wrong++;
goto start;
} //end else
}//end for
if ( resp != -1 ) {
right = 20 - wrong;
percent = 100 * right / 20;
printf("\n\n %d of 20 questions correct: %d%%\n\n" , right, percent );
system("pause");
} //end if
} //end addTest()
void setup(void) {
system("cls");
printf("\n\nSet up ranges:");
printf("\n\nTest sums from 0 to ");
scanf("%d" , &aMax);
printf("\n\nTest differences 0 to ");
scanf("%d" , &sMax);
printf("\n\nTest multiples 0 to ");
scanf("%d" , &mMax);
main();
} //end setup()