Computer Science Canada Help with a fractions program |
Author: | poly [ 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
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 |
Author: | Blade [ Sun Mar 30, 2003 9:41 pm ] | ||
Post 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:
but in this case you dont need to round an addition question, but thats how you would use it |
Author: | poly [ Sun Mar 30, 2003 9:52 pm ] |
Post 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 |
Author: | Tony [ Sun Mar 30, 2003 10:13 pm ] | ||
Post subject: | |||
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:
basically you're looking for common diviser and if one is found, you simplify your fraction. (There was a problem like that on CCC2002) |
Author: | Blade [ Sun Mar 30, 2003 10:29 pm ] |
Post 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? |
Author: | poly [ Mon Mar 31, 2003 12:16 pm ] |
Post 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 |