Thread: simplifiing fractions part 2

  1. #1
    Unregistered
    Guest

    Red face simplifiing fractions part 2

    thanks everyone for your help, but i have a second question.... is there a way to simplify the fractions using loops? because that is about where i am in my programming. and i dont want to get ahead of my self. any assitance would be much appreciated thanks again.

  2. #2
    Blank
    Join Date
    Aug 2001
    Posts
    1,034
    Code:
    int gcd(int a, int b)
    {
        int r;
    
        while (b != 0) {
            r = a % b;
            a = b;
            b = r;
        }        
      
        return a;
    }

  3. #3
    Registered User
    Join Date
    Dec 2001
    Posts
    421
    this isn't the ideal solution, but it'll give you an idea of how to do it.

    I have compiled this on MSVC++ 6.

    Code:
    #include <iostream.h>
    #include <math.h>
    
    int main(void)
    {
    	int i = 0;
    
    	// we are assuming that numerator >= denominator
    	int numerator = 9;
    	int denominator = 12;
    	int simplified = 0;
    
    	int newNumerator = numerator;
    	int newDenominator = denominator;
    
    	while(!simplified)
    	{
    		int i = 0;
    
    		for(i = 2; i < newNumerator; i++)
    		{
    			// if they are both divisible by the number
    			if(newNumerator % i == 0 && newDenominator % i ==0)
    			{
    				// divide both by the number
    				newNumerator /= i;
    				newDenominator /= i;
    
    				// reset i to make sure that the exit condition is met properly
    				i = 0;
    				break;
    			}
    		}
    
    		// if we've made it all the way through the loop, we have no more factors
    		if(i == newNumerator)
    		{
    			simplified++;
    		}
    	}
    
    	cout << "The simplifed version of " << numerator << "/" << denominator;
    	cout << " is " << newNumerator << "/" << newDenominator << endl;
    
    	return(0);
    }
    hope this helps
    U.
    Quidquid latine dictum sit, altum sonatur.
    Whatever is said in Latin sounds profound.

  4. #4
    Registered User
    Join Date
    Dec 2001
    Posts
    421
    the greatest common divisor wasn't the answer he was looking for. didn't he want to simplify the fraction??
    Quidquid latine dictum sit, altum sonatur.
    Whatever is said in Latin sounds profound.

  5. #5
    Blank
    Join Date
    Aug 2001
    Posts
    1,034
    Once you have the gcd all that's need to simplify is
    Code:
    g = gcd(numer, denom)
    (numer / g)
    -----------
    (denom / g)

  6. #6
    Registered User
    Join Date
    Dec 2001
    Posts
    421
    i can't believe that i didn't see that the greatest common divisor is the way ...... i'm going to hang my head in shame after that!
    Quidquid latine dictum sit, altum sonatur.
    Whatever is said in Latin sounds profound.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How to get a part of a string from a point onwards
    By pseudonoma in forum C Programming
    Replies: 2
    Last Post: 03-22-2008, 04:09 PM
  2. Replies: 9
    Last Post: 07-11-2006, 04:28 AM
  3. Can someone look over my code? part 2
    By brooklyn in forum C++ Programming
    Replies: 10
    Last Post: 04-18-2006, 06:33 AM
  4. Suspicious Pointer Conversion
    By mr_spanky202 in forum C Programming
    Replies: 35
    Last Post: 04-11-2003, 12:35 PM