-
Random math problems
How would you write a program to pick a math problem at random? i.e. add,divide,subtract,multiply? I guess using either subroutines or not but it would most likely be in a switch statment where the math problems are each a case and then another case would be random quesitons.....any suggestions or guidance?
-
That depends on how complicated you want your problems to be. Generally, you're going to have a random draw for each "element" of the problem. For example, if you wanted problems like (double-digit-number)(arithmetic-operator)(double-digit-number), you would need to generate each part. You would probably want to randomly choose the operator first, since that might set different limits on what you want your numbers to be.
-
google the rand() function.
-
You'd want to think about what kind of distribution you're using. One might construct an algorithm like the following:
Code:
generateMathProblem(depth) {
if (decideToContinue(depth)) {
function = pickAFunction()
for (1 .. function.arity()) {
arguments.push_back(generateMathProblem(depth + 1))
}
return apply(function, arguments)
}
else {
return randomConstant()
}
}
newMathProblem() {
return generateMathProblem(depth)
}
Then you'd tweak the probability of decideToContinue returning true for the various depths to control how big you want your math problems to be.