0

I'm trying to write a fraction class and I want a function to reduce the fraction to lowest terms. The problem I'm having is trying to get the fractions to reduce. I need a way to find a common factor in both numbers. All other functions work the way I want them to.

class Fraction{
private:
	int numerator;
	int denominator;
public:
	Fraction(){
	}

	void setNumerator(int numerator){
		this->numerator=numerator;
	}
	void setDenominator(int denominator){
		this->denominator=denominator;
	}

	int getNumerator(){
		return numerator;
	}

	int getDenominator(){
		return denominator;
	}

	int reduce(){
		if(denominator%numerator==0){
			return denominator/numerator;
		}
		else{
			return 0;
		}
	}
};
4
Contributors
7
Replies
10
Views
6 Years
Discussion Span
Last Post by vijayan121
0

You need to find the factors of each number.

This is also helpful.

I know how to find factors of a number, I just need to figure out how to do it in a C++ function.

0

I know how to find factors of a number, I just need to figure out how to do it in a C++ function.

Yes you do. Or are you expecting us to do it for you?

You have to show us an attempt, then we can help you fix it if it doesn't work properly.

0
#include "iostream.h"
#include "Fraction1.h"
#include <cmath>

class CFraction  
{
private:
	int numerator;
	int denominator;
public:
	void Input();
	void Output();
	void Reduce();
	int CheckFration();

	CFraction()
	{
		numerator = 0;
		denominator = 1;
	}

	virtual ~CFraction();

};

int CFraction::CheckFration()
{
	if(denominator != 0)
		return 1;
	return 0;
}

void CFraction::Reduce()
{
	int a = abs(numerator);
	int b = abs(denominator);
	
	while(a * b != 0)
	{
		if(a > b)
			a = a - b;
		else
			b = b - a;
		
	}
	numerator = numerator / (a+b);
	denominator = denominator / (a+b);
	
}

void CFraction::Input()
{
	cout << "Input the numerator: ";
	cin >> numerator;
	do
	{
		cout << "Input the denominator: ";
		cin >> denominator;
	}while(CheckFration() == 0);
}
void CFraction::Output()
{
	Reduce();
	cout << "\nThe Fraction: " << numerator << "/" << denominator << endl << endl;
}

int main()
{
	CFraction f;
	f.Input();
	f.Output();
	return 0;
}
0

I don't know what your reduce is attempting to do, but it doesn't even attempt to get factors -- unless I don't understand the algorithm you're using.

Write down (don't code) the process you use to reduce a number into it's factors, in minute detail. When you get it into it's most basic steps, turn it into code at that time. I'd recommend turning the factoring into a function and have it return all the factors (array) of one number.

0

I find the greatest common divisor of numerator and denominator.

You are right; there is no need to find individual factors at all. Just find the gcd and divide both the numerator and denominator by it.

This is slightly faster:

inline unsigned int gcd( unsigned int a, unsigned int b ) // Euclid
{ return b==0 ? a : gcd( b, a%b ) ; }
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.