Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 A Problem Concerning Division
Index -> Programming, Turing -> Turing Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
Kaykao




PostPosted: Sat Nov 10, 2012 3:20 pm   Post subject: A Problem Concerning Division

I would first like to make it known that I am a complete n00b when it comes to programming of any kind, and that the only programming experience I have besides this is making a 'guess the number' game using Turing. So please don't be angry with me if my question has an obvious answer. Wink

What is it you are trying to achieve?
I'm trying to create a program that generates random simple equations and allows the user to answer it, then tells the user if they are correct or not. So far everything is working fine, except for the division part of it. It's supposed to be for practicing simple math.

What is the problem you are having?
When Turing divides a number, it gives you a number that can go up to (I think) 16 numbers after the decimal. I'm trying to make it so that if the program divides two numbers and the answer has a lot of numbers after the decimal, the program will still count the user's answer as correct if the user's answer doesn't have all those numbers. It's a little hard to explain...

for example, the user answers the equation like this:
10 ? 9 = 1.11

I'm trying to make the program count something like that as a correct answer, even though there should technically be a lot more '1's after the decimal for this equation.

(Also, if anyone has an idea on how to make my code more efficient, that would help too. I know efficiency is extremely important in programming.)


Describe what you have tried to solve this problem
I've tried using 'div' to let the program know the answer to the two numbers that make up the question, but then it wouldn't accept any decimal number as a correct answer. I also tried to round the answer to two decimal points using :0:2 but it only rounded what was showing on the screen and still counted an answer without ALL the numbers after the decimal as wrong. I've looked through the FAQ sticky in this forum but didn't find anything there, and I browsed through the questions on the first page of this forum, but still didn't find any sort of solution.

I asked my sister, who has managed to create a few more complicated programs using turing, how I could fix this. She said I might be able to fix it using 'div' and 'mod' somehow, but I'm unsure how I would code that.


Post any relevant code (You may choose to attach the file instead of posting the code if it is too long)

The only reason I've put most of this program as a procedure is because this is only a portion of the full program. In the full thing I also have procedures for addition, subtraction, and multiplication. This is because it makes the loop that actually runs the program much easier for me to understand, and easier for me to fiddle around with the code for each type of equation. In the full program, the loop running the procedures determines which procedure to run by using an integer generated using Rand.Int and an 'if' statement saying run this procedure if the variable = 1, and so on. So at least that bit isn't a silly n00bish mistake...

But if it makes things easier for people to understand, all you need to do is ask and I'll post the full program's code. I only posted this bit because it was the only part that was relevant.

Turing:

procedure divide
    var y : real
    var input : string
    var a : int := Rand.Int (1, 12)
    var b : int := Rand.Int (1, 12)
    loop
        Text.Locate (1, 1)
        put a, " ? ", b, " = " ..
        get input
        if strrealok (input) then %----- This 'if' prevents the program from crashing if the user doesn't enter a number
            y := strreal (input)
            exit
        else
            put "That's not a number!"
            delay (2000)
            cls
            delay (500)
        end if
    end loop
    if y = a / b then %----- This 'if' statement is where the problem is
        put "Correct!"
    elsif y not= a / b then
        put "Incorrect... The answer was ", a / b
    end if
    delay (2000)
    cls
    delay (500)
end divide

loop
    divide
end loop


Please specify what version of Turing you are using
I'm using 4.1.1 but I plan to use version 4.1 to convert it into a .exe later.
Sponsor
Sponsor
Sponsor
sponsor
Insectoid




PostPosted: Sat Nov 10, 2012 3:36 pm   Post subject: RE:A Problem Concerning Division

Computers are really bad at division. There's always going to be very small errors.

You can solve this by comparing to a range. If the user's answer is, say, within 0.01 of the correct answer, call it correct. You can accomplish this by using the > and < operators instead of =, though I'll let you figure that part out yourself.
evildaddy911




PostPosted: Sat Nov 10, 2012 4:48 pm   Post subject: RE:A Problem Concerning Division

maybe round the answer to 2 decimal places?

ie:

Turing:

var Equation : string % what the user will see
var RightAnswer : real % the correct answer
procedure createEquation
     Equation := ???
     RightAnswer := ???
end createEquation

RightAnswer := DecPlaces (RightAnswer, 2)
% must create DecPlaces function yourself

% output equation
% get answer

% check answer
Insectoid




PostPosted: Sat Nov 10, 2012 5:07 pm   Post subject: RE:A Problem Concerning Division

You don't need to round anything. If the user's answer is less than the actual answer + X and greater than the actual answer -X, then it's close enough to be considered correct.
Kaykao




PostPosted: Sat Nov 10, 2012 6:45 pm   Post subject: RE:A Problem Concerning Division

Thank you for answering, Insectoid and Evildaddy.

I ended up using Insectoid's suggestion and it's working out fabulously. Thank you for coming up with that idea!
Display posts from previous:   
   Index -> Programming, Turing -> Turing Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 5 Posts ]
Jump to:   


Style:  
Search: