Computer Science Canada Programming C, C++, Java, PHP, Ruby, Turing, VB   Username:   Password: Wiki   Blog   Search   Turing   Chat Room  Members
Help with a fractions program
Author Message
poly

Posted: 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

Posted: 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

Posted: 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

Posted: Sun Mar 30, 2003 10:13 pm   Post subject: (No 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:

 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)
Tony's programming blog. DWITE - a programming contest.

Posted: 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

Posted: 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: All Posts1 Day7 Days2 Weeks1 Month3 Months6 Months1 Year Oldest FirstNewest First

Page 1 of 1  [ 6 Posts ]
 Jump to:  Select a forum  CompSci.ca ------------ - Network News - General Discussion     General Forums   -----------------   - Hello World   - Featured Poll   - Contests     Contest Forums   -----------------   - DWITE   - [FP] Contest 2006/2008   - [FP] 2005/2006 Archive   - [FP] 2004/2005 Archive   - Off Topic     Lounges   ---------   - User Lounge   - VIP Lounge     Programming -------------- - General Programming     General Programming Forums   --------------------------------   - Functional Programming   - Logical Programming   - C     C   --   - C Help   - C Tutorials   - C Submissions   - C++     C++   ----   - C++ Help   - C++ Tutorials   - C++ Submissions   - Java     Java   -----   - Java Help   - Java Tutorials   - Java Submissions   - Ruby     Ruby   -----   - Ruby Help   - Ruby Tutorials   - Ruby Submissions   - Turing     Turing   --------   - Turing Help   - Turing Tutorials   - Turing Submissions   - PHP     PHP   ----   - PHP Help   - PHP Tutorials   - PHP Submissions   - Python     Python   --------   - Python Help   - Python Tutorials   - Python Submissions   - Visual Basic and Other Basics     VB   ---   - Visual Basic Help   - Visual Basic Tutorials   - Visual Basic Submissions     Education ----------- - Student Life   Graphics and Design ----------------------- - Web Design     Web Design Forums   ---------------------   - (X)HTML Help   - (X)HTML Tutorials   - Flash MX Help   - Flash MX Tutorials   - Graphics     Graphics Forums   ------------------   - Photoshop Tutorials   - The Showroom   - 2D Graphics   - 3D Graphics     Teams ------ - dTeam Public

 Style: Appalachia blueSilver eMJay subAppalachia subBlue subCanvas subEmjay subGrey subSilver subVereor Search: