Thread: anyone help a newbie with fractions

  1. #1
    Unregistered
    Guest

    anyone help a newbie with fractions

    Can anyone help me I'm very new to programming and need some help. I need to find a way to +,-,*, and / fractions.

  2. #2
    back? dbaryl's Avatar
    Join Date
    Oct 2001
    Posts
    597
    Well, first off: you need the algebra of it.

    Whether you're trying to add or subtract, it's necessary to know the common denomenator. To do this, you can simply multiply top and bottom of each fraction by the denomenator of the other fraction. This will not be reduced, but we'll get to reducing later.

    So to +/-

    Say you have 2 fractions, numerator 1: n1, denomenator 1: d1, numerator 2: n2... and n3 & d3 for the final fraction. Assuming you have the values already stored (using a function like scanf):
    Code:
    int n1, n2, n3, d1, d2, d3;
    
    n3 = n1 * d2 + n2 * d1;
    
    d3 = d1 * d2; //common denomenator, not reduced
    To subtract, just change the + to - in code above.

    To multiply or divide:

    To multiply, just multiply the numerators for new numerator, den. for new denomenator.
    Code:
    int n1, n2, n3, d1, d2, d3;
    
    n3 = n1 * n2;
    
    d3 = d1 * d2; //common denomenator, not reduced
    To divide, just multiply by inverse.
    Code:
    int n1, n2, n3, d1, d2, d3;
    
    n3 = n1 * d2;
    
    d3 = d1 * n2; //denomenator not reduced
    One thing that's left is to reduce... to do this, we can get the greatest common factor first, then divide top/bottom of the fraction by this value. You can find how to do this here:

    http://www.cprogramming.com/cboard/s...threadid=22039

    If you have any other questions, just ask.
    This is my signature. Remind me to change it.

  3. #3
    Refugee face_master's Avatar
    Join Date
    Aug 2001
    Posts
    2,052

    Thumbs up

    Originally posted by Unregistered
    Can anyone help me I'm very new to programming and need some help. I need to find a way to +,-,*, and / fractions.
    Gee, dbaryl, dont cofuse him.
    Heres some code that adds one to a fraction (just fixed up that sentence). This fraction that we'll work with is
    7
    --
    10
    Code:
    // our fraction
    double fraction = 7 / 10; // remember, our fraction is equal to 7 divided by ten
    double total = 0; // our end result
    
    total = fraction + 1;
    Hopefully, I'm not tellin ya anything incorrect...just give that example a go.
    Last edited by face_master; 07-25-2002 at 06:17 AM.

  4. #4
    back? dbaryl's Avatar
    Join Date
    Oct 2001
    Posts
    597
    Man, you have confused ME now ...
    Code:
    total = fraction + 1;
    what in the world is that? sorry, I don't get that.

    Also, I was thinking they want it as a fraction, not a decimal, as that's what making it a double will do.
    This is my signature. Remind me to change it.

  5. #5
    Refugee face_master's Avatar
    Join Date
    Aug 2001
    Posts
    2,052
    its adding 1 to the fraction...um, theres no data structure that holds a fraction in any way other than a decimal, is there?

  6. #6
    flashing vampire black's Avatar
    Join Date
    May 2002
    Posts
    563
    Algebra is basis, and next you should learn the difference between programming language and mathematics.

    for instance, this line below is common in almost all kinds of programming languages but not in math.

    PHP Code:
    a=a+1

    Good luck~
    Never end on learning~

  7. #7
    back? dbaryl's Avatar
    Join Date
    Oct 2001
    Posts
    597
    Originally posted by face_master
    its adding 1 to the fraction...um, theres no data structure that holds a fraction in any way other than a decimal, is there?
    >>Heres some code that adds one to a fraction (just fixed up that sentence).

    OK, I guess I need to pead more throughly next time
    This is my signature. Remind me to change it.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. fractions
    By zdream8 in forum C Programming
    Replies: 2
    Last Post: 05-21-2008, 09:54 PM
  2. Greatest Common Factors of Fractions
    By sonict in forum C++ Programming
    Replies: 1
    Last Post: 01-15-2003, 04:33 AM
  3. Newbie Game Develpoers Unite!
    By Telenosis in forum Game Programming
    Replies: 10
    Last Post: 06-22-2002, 02:02 PM
  4. Fractions
    By Unregistered in forum C++ Programming
    Replies: 2
    Last Post: 04-01-2002, 07:51 AM
  5. Decimals to Fractions
    By ToasterPas in forum C++ Programming
    Replies: 4
    Last Post: 12-28-2001, 12:58 PM