Thread: Help with fraction problem

  1. #1
    Set Apart -- jrahhali's Avatar
    Join Date
    Nov 2002
    Posts
    256

    Help with fraction problem

    Hello. I'm trying to put a fraction in lowest terms, but i can't seem to get it working; I work it out on paper, everything seems it should be ok.
    NOTE: den is for denominator, num is for numerator, gcd is for greatest common divisor. I start out with the number 2 for gcd and then test if that number can divide into the numerator and denominator evenly. If not, gcd is increased by one until it reaches den/2. if it does divide evenly into both, then num and den are both diveded by gcd and gcd is set to 1, whereon looping, setting gcd = 2, and repeating the process over agian.

    Here's the code:
    Code:
    void Fraction::lowterms()
    {
    	for(int gcd = 2; gcd <= den/2; gcd++ )
    		while(!(num%gcd) && !(den%gcd) )
    		{
    			num = num / gcd;
    			den = den / gcd;
    			gcd = 1;
    		}
    }
    It's really strange.
    1) Sometimes the nothing is displayed on the screen ( i presume it's going into an infitite loops )

    2) the numerator equates to some super high number like 185764 and the denominator is correct

    3) the numerator equates to some super high number liek 185764 and the denominator is wrong ( but a low number );

    i can't seem to figure out why...

  2. #2
    Software Developer jverkoey's Avatar
    Join Date
    Feb 2003
    Location
    New York
    Posts
    1,905
    Quote Originally Posted by jrahhali
    Code:
    void Fraction::lowterms()
    {
    	for(int gcd = 2; gcd <= den/2; gcd++ )
    		while(!(num%gcd) && !(den%gcd) )
    		{
    			num = num / gcd;
    			den = den / gcd;
    			gcd = 1;
    		}
    }
    try fixing how far you go in your for loop...I think i have some working code, but i'd like to see if you can figure it out if you are really stuck, just PM me and i'll tell you what i did, it's actually pretty simple, so it shouldn't be too hard

    -edit-
    also, about the infinite loop, notice that when you modulus a number by one, it will never have a remainder, so you end up dividing by one, over and over and over in the while loop
    Last edited by jverkoey; 04-04-2004 at 10:14 PM.

  3. #3
    Set Apart -- jrahhali's Avatar
    Join Date
    Nov 2002
    Posts
    256
    >>I think i have some working code
    ?? can i see it? hehe.

    >>notice that when you modulus a number by one...
    i actually set gcd to one so that when control goes to the for loop again gcd is incremented, and making it have a value of 2, so 1 is never used for the modulous division.

  4. #4
    Software Developer jverkoey's Avatar
    Join Date
    Feb 2003
    Location
    New York
    Posts
    1,905
    well, in your while loop, you set the gcd to one once it finds a common denominator. But then right after that you test the denominators again, and the remainder of any number divided by 1 is going to be 0. So unless you break from the while loop, it's just going to go forever

    also, in my code, i made it loop until it hit either the numerator or the denominator. I think it's best to choose the one that's lowest and go until that one

    Code:
    void Fraction::lowterms()
    {
    	int countTo=den;
    	if(num<den)
    		 countTo=num;
    	for(int gcd = 2; gcd <= countTo; gcd++ )
    		while(!(num%gcd) && !(den%gcd) )
    		{
    			num = num / gcd;
    			den = den / gcd;
    			gcd = 1;
    			break;
    		}
    }

  5. #5
    Set Apart -- jrahhali's Avatar
    Join Date
    Nov 2002
    Posts
    256
    >> But then right after that you test the denominators again, and the remainder of any number divided by 1 is going to be 0
    great great.. thanks alot.. iot must have slipped my mind that the WHILE LOOPS ACTUALLY CONTINUES if it's true. hehe .. cheers

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Memory problem with Borland C 3.1
    By AZ1699 in forum C Programming
    Replies: 16
    Last Post: 11-16-2007, 11:22 AM
  2. Someone having same problem with Code Block?
    By ofayto in forum C++ Programming
    Replies: 1
    Last Post: 07-12-2007, 08:38 AM
  3. A question related to strcmp
    By meili100 in forum C++ Programming
    Replies: 6
    Last Post: 07-07-2007, 02:51 PM
  4. WS_POPUP, continuation of old problem
    By blurrymadness in forum Windows Programming
    Replies: 1
    Last Post: 04-20-2007, 06:54 PM
  5. Laptop Problem
    By Boomba in forum Tech Board
    Replies: 1
    Last Post: 03-07-2006, 06:24 PM