Would I write the addition and subtraction in the same algorithm or use branching?
Suppose you are asked to design a software tool that helps an elementary school student learn
arithmetic operations. The software allows the student to select the arithmetic operation she or he
wishes to study. The student chooses from a menu one of five arithmetic operations: 1) addition,
2) subtraction,(other arithmetic operations have not been include to keep the problem simple).
Based on the student choice, the software tests the user with exactly 10 questions. For each
question, two random positive one-digit integers are generated; then the student is asked to enter
the answer for the arithmetic operation applied to the two numbers.
The software displays a message “Congratulations!” if more that 7 questions are answered
correctly, otherwise, the program should display "Please ask your teacher for help".
What I would do after getting the input from the user as to what operator they want to use, and checking that the input is valid, is:
- do a loop of 10 for each of the 10 questions.
- - get the 2 random numbers.
- - then do a switch on the inputted operator.
- - - according to which operator, generate the answer.
- - - ask the user to answer.
- - - compare the users answer to the generated answer.
- - - update the amount of correct answers if it is correct.
- - end switch.
- end loop.
OR
- do a loop of 10 for each of the 10 questions.
- - get the 2 random numbers.
- - then do a switch on the inputted operator.
- - - according to which operator, generate the answer.
- - end switch.
- - ask the user to answer.
- - compare the users answer to the generated answer.
- - update the amount of correct answers if it is correct.
- end loop.
Then check the amount of correct answers to output the message of congratualtions or not.
Good luck with it.
October 25th, 2009 at 3:06 am
I can’t think of an obvious way of doing them both in the same routine. I suppose if your random numbers can be either positive or negative, you could "accidentally" end up with either addition or subtraction, but it seems like you need to just create a conditional branch for each of the 5 operations.
References :
October 25th, 2009 at 3:12 am
What I would do after getting the input from the user as to what operator they want to use, and checking that the input is valid, is:
- do a loop of 10 for each of the 10 questions.
- - get the 2 random numbers.
- - then do a switch on the inputted operator.
- - - according to which operator, generate the answer.
- - - ask the user to answer.
- - - compare the users answer to the generated answer.
- - - update the amount of correct answers if it is correct.
- - end switch.
- end loop.
OR
- do a loop of 10 for each of the 10 questions.
- - get the 2 random numbers.
- - then do a switch on the inputted operator.
- - - according to which operator, generate the answer.
- - end switch.
- - ask the user to answer.
- - compare the users answer to the generated answer.
- - update the amount of correct answers if it is correct.
- end loop.
Then check the amount of correct answers to output the message of congratualtions or not.
Good luck with it.
References :