arithmetic tutor
Author |
Message |
Misha
|
Posted: Mon Oct 04, 2004 8:07 pm Post subject: arithmetic tutor |
|
|
hi, i am new to turing and i need help with a question. the question is as follows.
Write a program that acts as an arithmetic tutor. The program should randomly pick two numbers and an operation (addition, subtraction, or multiplication) and should ask the user to answer a question involving the numbers and the operation. If the user answers incorrectly, the question should be repeated until it is answered correctly. The program should display the amount of guesses it took the user to answer the question correctly. The program should also ask the user if he/she wishes to continue after each question.
for now this is the code i have and i don't know how to do the rest.
code: |
%------------------------------------------
% arithmetic tutor
% Date Written Oct 4
%
% input: answer to the numbers and the operation
% output: displays if user is correct or incorrect
% Process: ask the user for answer
%-----------------------------------------
% Display Variables
var wer, wer2 : int
var ope : int
var ans : int
% Ask for input
loop
randomize
randint (wer, 1,10)
randint (wer2, 1,10)
put "please answer this question"
get ans
exit when ans =
end loop
|
please help
thanx |
|
|
|
|
|
Sponsor Sponsor
|
|
|
Genesis
|
Posted: Mon Oct 04, 2004 8:39 pm Post subject: (No subject) |
|
|
Read the comments in the code:
code: |
%Display Variables
%Variables for the 2 random numbers to be generated, the random operation, the correct answer, as well as the users guess:
var rand1, rand2, randop, answer, guess : int
%Variable to hold the symbol of the operator used (+/-/*) for display purposes:
var operator : string
%Ask for input
loop
randint (rand1, 1, 10) %These 2 lines generate the 2 random numbers to be used in the operation.
randint (rand2, 1, 10)
randint (randop, 1, 3) %This generates a random number between 1 and 3 because there are 3 operators to choose from.
if randop = 1 then
answer := rand1 + rand2 %If a 1 is generated as the operator, then the answer equals the sum of the 2 numbers added.
operator := "+"
elsif randop = 2 then
answer := rand1 - rand2 %If a 2 is generated as the operator, then the answer equals the sum of the 2 numbers subtracted.
operator := "-" %The operator becomes a minus sign.
elsif randop = 3 then
answer := rand1 * rand2 %If a 3 is generated as the operator, then the answer equals the product of the 2 numbers.
operator := "*" %The operator becomes a multiplaction sign.
end if
loop %This second loop will run until the user gets the correct answer for the equation generated above.
put "Please answer this question:" %Asks user to answer question.
put rand1, " ", operator, " ", rand2 %Displays first number, followed by the operator, followed by the second number to form the equation.
put "" %Leaves a blank line.
get guess %Lets user input their guess at the number.
exit when guess = answer %When the user enters the correct number, which is stored in the variable 'answer', the loop exits.
put ""
put "That is incorrect. Try again." %If user enters wrong answer, the loop fails to terminate, and displays this before restarting.
end loop
put ""
put "Correct!" %If the user entered the correct answer, this text is displayed before restarting the whole loop again.
put ""
end loop |
Although this may be unnecesarily long, it is the easiest way to follow if you're new to Turing. |
|
|
|
|
|
Misha
|
Posted: Mon Oct 04, 2004 8:50 pm Post subject: (No subject) |
|
|
thank you so much
i actually learned from this code. i am not going to copy it and try to follow the way you did it and do the same. if i had a few bits to spare i would give them to you.
thanx!!!!!!!!!!!!!!1 |
|
|
|
|
|
Genesis
|
Posted: Mon Oct 04, 2004 8:53 pm Post subject: (No subject) |
|
|
No problem. |
|
|
|
|
|
Misha
|
Posted: Mon Oct 04, 2004 9:02 pm Post subject: (No subject) |
|
|
one more question in the code you gave it didn't say how many times i guessed the number and it goes on and on forever never asking the user if they want to stop.
thanx |
|
|
|
|
|
Genesis
|
Posted: Mon Oct 04, 2004 9:11 pm Post subject: (No subject) |
|
|
Well that can be solved quite easily, for the counting how many times you guessed, just add a counter variable, and increase it by 1 each time the loop runs.
So declare:
code: | var counter : int := 0 |
And each time the loop runs, add 1 to it. And set it back to zerio where necessary.
If you want the user to be able to pick whether or not they want to continue, you can just ask the user if they want to coninue, something like "Would you like to continue? (Y/N)" and if they choose yes, then move on with the program, if they choose no, then display a closing message, and let the program terminate. (Put this outside of the main loop, so each time it exits the main loop, this takes place.) |
|
|
|
|
|
Misha
|
Posted: Mon Oct 04, 2004 9:26 pm Post subject: (No subject) |
|
|
sorry, i'm still having trouble putting how many guesses the person made. can you give me a bit more details please.
thanx |
|
|
|
|
|
apomb
|
Posted: Tue Oct 05, 2004 11:45 am Post subject: (No subject) |
|
|
well, heres some modified code, the comments are made, easy to follow
code: |
%Display Variables
%Variables for the 2 random numbers to be generated, the random operation, the correct answer, as well as the users guess:
var rand1, rand2, randop, answer, guess : int
%Variable to hold the symbol of the operator used (+/-/*) for display purposes:
var operator : string
var counter, inc, corr : int := 0
%Ask for input
loop
counter += 1
randint (rand1, 1, 10) %These 2 lines generate the 2 random numbers to be used in the operation.
randint (rand2, 1, 10)
randint (randop, 1, 3) %This generates a random number between 1 and 3 because there are 3 operators to choose from.
if randop = 1 then
answer := rand1 + rand2 %If a 1 is generated as the operator, then the answer equals the sum of the 2 numbers added.
operator := "+"
elsif randop = 2 then
answer := rand1 - rand2 %If a 2 is generated as the operator, then the answer equals the sum of the 2 numbers subtracted.
operator := "-" %The operator becomes a minus sign.
elsif randop = 3 then
answer := rand1 * rand2 %If a 3 is generated as the operator, then the answer equals the product of the 2 numbers.
operator := "*" %The operator becomes a multiplaction sign.
end if
inc := 0
loop %This second loop will run until the user gets the correct answer for the equation generated above.
put "Please answer this question:" %Asks user to answer question.
put rand1, " ", operator, " ", rand2 %Displays first number, followed by the operator, followed by the second number to form the equation.
put "" %Leaves a blank line.
get guess %Lets user input their guess at the number.
if guess = answer then
put "Correct!" %If the user entered the correct answer, this text is displayed before restarting the whole loop again.
corr += 1 %adds one to the number of correct answers
exit
else %When the user enters the correct number, which is stored in the variable 'answer', the loop exits.
put ""
exit when inc = 2
put "That is incorrect. Try again."%If user enters wrong answer, the loop fails to terminate, and displays this before restarting until the number of incorrect answers gets to 2
inc += 1 %adds one to the number that selects whether to exit the loop
end if
end loop
put ""
put ""
exit when counter = 10 %exits when counter is 10
end loop
put "you got ", corr, " correct out of ", counter %displays the number of correct answers
|
|
|
|
|
|
|
Sponsor Sponsor
|
|
|
AsianSensation
|
Posted: Tue Oct 05, 2004 6:46 pm Post subject: (No subject) |
|
|
Before I wrongly accuse someone of something...........
CompWizz, it's nice that we have people that are readily willing to help other new members. However, as a regular compsci.ca member, you should know better than post the entire code to a problem. The most problem we've had with this site is plagiarism, some teachers even forbid their students from coming to this website. Let's not make matters worse. We should instead guide the people step by step, and not giving them the finished product in 1 post.
Misha, when you ask for help, please give not only the entire problem, but also some REAL attempt at what you tried to do. I am not saying that you are abusing our system and cheating, but I am saying that you should at least attempt the problem, with a realistic effort, and when you are genuinely stuck, then come and ask for our help. So far, your posts (from other threads as well, I do notice things too) did not show any thoughts, but merely copy and paste.
This is my warning/argument. PM me if you want to assert your point. |
|
|
|
|
|
wtd
|
Posted: Tue Oct 05, 2004 6:53 pm Post subject: (No subject) |
|
|
To his credit, the code he posted appears to be just a slightly modified version of the code Misha had already posted. |
|
|
|
|
|
AsianSensation
|
Posted: Tue Oct 05, 2004 7:03 pm Post subject: (No subject) |
|
|
yes, that is true, but there is the case that anyone could waltz in here, copy and paste his code and then hand it in as a homework assignment (I mean, it was even complete with comments). Though, if for example, CompWizz had hinted and nudged Misha along the general direction to the solution, it would have prevented the copy/paste problem. |
|
|
|
|
|
Genesis
|
Posted: Tue Oct 05, 2004 8:02 pm Post subject: (No subject) |
|
|
wtd wrote:
To his credit, the code he posted appears to be just a slightly modified version of the code Misha had already posted.
Actually the code Misha posted didn't have much in it. I posted the fully commented working solution. Only because I was kind of in a hurry so figured it would be easier to just comment the code so he/she could follow what was going on and learn from that. But yes, the plagarism thing is a problem, sorry. (And the code that Compwiz posted is just a slightly modified version of my original post, so it's not him who's to blame.) |
|
|
|
|
|
AsianSensation
|
Posted: Tue Oct 05, 2004 9:14 pm Post subject: (No subject) |
|
|
oh oops, I thought CompWizz did all the coding, sorry Genesis, missed you there. And sorry CompWizz too, for blaming it all on you.
So yeah, +bits? |
|
|
|
|
|
|
|