can't get this function to reduce fractions

This is a discussion on can't get this function to reduce fractions within the C++ Programming forums, part of the General Programming Boards category; Hello, i have been working on this for a while now. The program works but i am getting the wrong ...

  1. #1
    Registered User
    Join Date
    Nov 2002
    Posts
    2

    can't get this function to reduce fractions

    Hello,

    i have been working on this for a while now. The program works

    but i am getting the wrong output. I know it is something wrong

    with the Reduce function in Fraction.h because i did a

    system("pause") right before I called the Reduce function and the

    answer was still ok.

    I would greatly appreciate it if someone could look at it and let me know what' s wrong.


    ---Thank you, bruce

    This is the .cpp file

    Code:
    #include<iostream.h>
    #include"Fraction.h"
    
    
    const int MAX = 7;
    
    //--------------------------------------------------------------------------------
    
    // gets the fractions from user
    
    int main()
    {
    	char ans;
    
    	do
    	{
    		Fraction input[MAX];
    	
    		cout << "Enter 7 fractions and press Enter after each one: " << endl;
    	
    		for (int i = 0; i < MAX; i++)
    		{
    			input[i].Get(); 
    		}
    	
    		cout << endl << endl;
    //--------------------------------------------------------------------------------
    
    		//calculates sum and displayes the reduced sum
    
    		Fraction sum;
    
    		for (int i = 0; i < MAX; i++)
    		{
    			sum = sum+ input[i];
    		}
    		
    		sum = sum.Reduce(sum);
    		cout << "The sum of those seven fractions is ";
    	    sum.Show();
    	
    
    
    //-----------------------------------------------------------------------------------
    
    	//calculates product and displayes the reduced product
    
    		Fraction product(1,1);
    
    		for (int i = 0; i < MAX; i++)
    		{
    			product = product* input[i];		
    		}
    		
    		product = product.Reduce(product);
    		cout << "The product of those seven fractions is ";
    		product.Show();
    	
    
    
    //--------------------------------------------------------------------------------------
    
    		//increments product by 1, then displays
    
    		cout << "The product added by 1 equals ";
    		product = product.Increment(product);
    		product.Show();
    
    //-----------------------------------------------------------------------------------
    
    		//decrements sum by 1, then displays
    
    		cout << "The sum subtracted by 1 equals ";
    		sum = sum.Decrement(sum);
    		sum.Show();
    		
    		cout << endl;
    
    		cout << "Would you like to enter another set of fractions? y or n? " << endl;
    		cin >> ans;
    
    		cout << endl << endl;
    
    	}while(ans == 'y' || ans == 'Y');
    return 0;
    }






    This is the .h file:

    Code:
    
    class Fraction
    {
    	friend Fraction operator + (Fraction, Fraction);
    	friend Fraction operator * (Fraction, Fraction);
    	
    	
    public:
       
    	Fraction(int n, int d = 1); 	   //constructor
       	Fraction();			               //Set numerator = 0, denominator = 1
      	
    	void Get();			               //Get a fraction from keyboard
      	void Show();		               //Display a fraction on screen
      	double Evaluate();	            //Return the decimal value of a fraction
    	
    	Fraction Increment(Fraction r);
    	Fraction Decrement(Fraction r);
    	Fraction Reduce(Fraction r);
    	
    private:
      		
    	int numerator;
      	int denominator;
    };
    
    Fraction operator + (Fraction f1, Fraction f2)
    {
      Fraction r;	                        //the return value of f1 + f2
     		                                 //compute numerator
      r.numerator = (f1.numerator * f2.denominator) + (f2.numerator * f1.denominator);
     		                                 //compute denominator
      r.denominator = f1.denominator * f2.denominator;
    
      return r;
    }
    
    Fraction operator * (Fraction f1, Fraction f2)
    {
    	Fraction r;
    
    	r.numerator = (f1.numerator * f2.numerator);
    
    	r.denominator = (f1.denominator * f2.denominator);
    
    	return r;
    }
    
    Fraction::Fraction(int n, int d)
    {
    	numerator = n;
        denominator = d;
    }
    
    Fraction::Fraction()
    {
    	numerator = 0;
        denominator = 1;
    }
    
    
    void Fraction::Get()
    {
       char div_sign;                      //consumes '/' character
       cin >> numerator >> div_sign >> denominator;
    }
    
    
    void Fraction::Show()  
    {
      cout << numerator << '/' << denominator << endl;
    }
    
    
    double Fraction::Evaluate()  
    {
      double n = numerator;	               //convert numerator to float
      double d = denominator;	            //convert denominator to float
      return (n / d);		                  //return float representation
    } 
    
    Fraction Fraction::Increment(Fraction r)
    {
    	r.numerator = r.numerator + r.denominator;
    	return r;
    }	
    
    Fraction Fraction::Decrement(Fraction r)
    {
    	r.numerator = r.numerator - r.denominator;
    	return r;
    }
    
    Fraction Fraction::Reduce(Fraction r)
    {
    	int remainder;
    	
    	if (r.denominator > r.numerator)
    	{	
    		remainder = r.denominator % r.numerator;
    
    		if (remainder != 0)
    		{
    			r.denominator = r.denominator / remainder;
    			r.numerator = r.numerator / remainder;
    			return r;
    		}
    		else if (remainder == 0)
    		{
    			r.denominator = r.denominator / r.numerator;
    			r.numerator = r.numerator / r.numerator;
    			
    			return r;
    		}
    	}	
    	
    	if (r.numerator > r.denominator)
    	{
    		remainder = r.numerator % r.denominator;
    
    		if (remainder != 0)
    		{
    			r.numerator = r.numerator / remainder;
    			r.denominator = r.denominator / remainder;
    			return r;
    		}
    		else if (remainder == 0)
    		{	
    			r = r.numerator / r.denominator;
    			return r;
    		}
    	}	
    }

  2. #2
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    Aside from the syntax errors *grumble grumble* I got the correct answers. Could you tell me what you where doing that was returning wrong results?

    [edit]
    Okay, nevermind it is broken.
    [/edit]
    Last edited by master5001; 11-12-2002 at 03:04 AM.

  3. #3
    booyakasha
    Join Date
    Nov 2002
    Posts
    208
    The program works but i am getting the wrong output.
    I like that line

  4. #4
    Registered User 4point5's Avatar
    Join Date
    Oct 2002
    Posts
    44

    Cool

    I'd like to help, but try and isolate the specific code thats giving you a problem and post just that.
    Don't try so hard. Just let it happen.

  5. #5
    Just a Member ammar's Avatar
    Join Date
    Jun 2002
    Posts
    953
    I didn't read the code, but I from the subject of the thread, I can tell that you are not able to reduce the fraction?
    if it's so, you don't know how - I mean the algorithm - or your code isn't working, if so post the function you are using.

    The idea is that you have to get the GCD Greatest Common Devisor, then devide both the numerator and denominator by it.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Beginner Needs help in Dev-C++
    By Korrupt Lawz in forum C++ Programming
    Replies: 20
    Last Post: 09-28-2010, 02:17 AM
  2. In over my head
    By Shelnutt2 in forum C Programming
    Replies: 1
    Last Post: 07-08-2008, 07:54 PM
  3. Brand new to C need favor
    By dontknowc in forum C Programming
    Replies: 5
    Last Post: 09-21-2007, 11:08 AM
  4. Including lib in a lib
    By bibiteinfo in forum C++ Programming
    Replies: 0
    Last Post: 02-07-2006, 02:28 PM
  5. Question..
    By pode in forum Windows Programming
    Replies: 12
    Last Post: 12-19-2004, 07:05 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21