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

Username:   Password: 
 RegisterRegister   
 Help with a fractions program
Index -> Programming, Turing -> Turing Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
poly




PostPosted: Sun Mar 30, 2003 9:33 pm   Post subject: Help with a fractions program

Hey guys, Im pretty new to turing and new to these boards and I am in need of help for a program that we wrote in class the other day. The teacher handed us a few problems and told us to write them up, we didnt have to, but they were extra practice. Anywho I got stumped on this one question. What we need to do is write a program (in turing of coarse) that will add 2 fractions that the user inputs and than outputs the answer. well that was easy, heres my code for it
code:
% All the program variables
var fTop, fBottom, sTop, sBottom : int

% Now getting the 2 fractions from user
put "Enter the first fraction (enter Numerator, space, enter denominator) " ..
get fTop, fBottom
put "Enter the second fraction (enter Numerator, space, enter denominator) " ..
get sTop, sBottom

% Spacer
put ""

% This will show the user their 2 fractions with a '+' between the 2
put fTop, "/", fBottom, " + ", sTop, "/", sBottom

% This will add the 2 fractions and output answer
put "= ", (fTop * sBottom) + (sTop * fBottom), "/", (fBottom * sBottom)


so say the user inputs 3/4 and 5/12 it would look like...
3/4 + 5/12 = 56/48

Now how would I write something to round the fraction? would i use IF statement... could somebody (if you have some free time) pelase write me what I would put for the program to round the fraction if the fraction could be rounded
Sponsor
Sponsor
Sponsor
sponsor
Blade




PostPosted: Sun Mar 30, 2003 9:41 pm   Post subject: (No subject)

no, you would use round(), it rounds up no matter what the decimal is, but if you want to round down... then you use floor()
ex:
code:
put round(24+15)
put floor(24+15)

but in this case you dont need to round an addition question, but thats how you would use it
poly




PostPosted: Sun Mar 30, 2003 9:52 pm   Post subject: (No subject)

Oh ok well thats what I thought I had to do, but my teacher said that it wouldnt be correct.. So i was like "wtf is he talkin about"... thanks for the reply
Tony




PostPosted: Sun Mar 30, 2003 10:13 pm   Post subject: (No subject)

Confused its kind of hard to round fractions. What you do is you simplify them.

first of all, if the top number if larget then bottom number, you know there's a 1 in front of the fraction, so you can take that much of.

After that, you should run a forloop from 2 to the top number and see if both top and bottom number can be divided by the same counter.

such as:

code:

for i:2..numTop
if numTop mod i = 0 and numBottom mod i = 0 then
numTop := numTop / i
numBottom := numBottom /i
end if
end for


basically you're looking for common diviser and if one is found, you simplify your fraction. (There was a problem like that on CCC2002)
Latest from compsci.ca/blog: Tony's programming blog. DWITE - a programming contest.
Blade




PostPosted: Sun Mar 30, 2003 10:29 pm   Post subject: (No subject)

i think i misunderstood the question... i thought he wanted to round the top number... so if he got 2.5/4 then he wanted it as 2/4(1/2) or 3/4 or something like that... you see what i mean?
poly




PostPosted: Mon Mar 31, 2003 12:16 pm   Post subject: (No subject)

just what Im looking for Tony, thanks. I had it written similiar to what you have, except for i didnt have the 2.. numftop, thanks guys
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  [ 6 Posts ]
Jump to:   


Style:  
Search: