The parts I am having trouble with are:
The private section of the class should also include two member functions that will
a) reduce a fraction to lowest terms
b) normalize a fraction (the only sign appearing will be a negative sign in the
numerator.)

for part a) I have put together the reduce member function by using GCD. and am trying to add them to my arithmetic methods but having issues am I going in the right direction so far

part b) all I know so far that if there is a negative sign in the denominator it has to be switched to numerator
I was thinking if else statements ie:
if denominator has -
change numerator to -
although I do not know how to go about changing signs

any help would be appreciated
thank you

#include <iostream>
#include <iomanip>
#include <cmath>
using namespace std;

class Rational{
 private:
	  int numerator;
	  int denominator;
	  float deci;
	  //int result;
	  void reduce(Rational);
	  void normalize();

  public:
	Rational() : numerator(0), denominator(0)
	{}
	Rational(int num, int den) : numerator(num), denominator(den)			//constuctor
	{}
	void sglfrac();						//input a single fraction
	void add(Rational, Rational);		//add two fractions
	void sub(Rational, Rational);		//subtract two fractions
	void mult(Rational, Rational);		//multiply two fractions
	void div(Rational, Rational);		//divide two fractions
	void displaysgl();					//display a single fraction in n/d format
	void displayfloat(Rational);		//display a single fraction in floating point format	
  
};	//ends class Rational

		//private member functions
	void Rational::reduce(Rational){
		int tnum, tden, temp, gcd;
		tnum = labs(numerator);          						// use non-negative copies
		tden = labs(denominator);          						//    (needs cmath)
		if(tden==0 ){   											// check for n/0  
			cout << "Illegal fraction: division by 0"; exit(1); }
		else if( tnum==0 ){        							    // check for 0/n
			numerator=0; denominator = 1; return; }
						// this ‘while’ loop finds the gcd of tnum and tden
		while(tnum != 0)
			if(tnum < tden){         // ensure numerator larger
				temp=tnum; tnum=tden; tden=temp;  // swap them
				tnum = tnum - tden;}     // subtract them
		gcd = tden;                // this is greatest common divisor
		numerator = numerator / gcd;           // divide both num and den by gcd
		denominator = denominator / gcd;           // to reduce frac to lowest terms
	}
		//sets the numerator and denominator to 0
	void Rational::sglfrac() {
		
		cout << "\nnumerator: "; cin >> numerator;  
		cout << "denominator: "; cin >> denominator;
	}	//end function sglfrac
	
	void Rational::add(Rational f1, Rational f2) {
		//	a / b + c / d = [n](a * d + b * c) [d](b * d)
		numerator = (f1.numerator * f2.denominator) + (f1.denominator * f2.numerator); //adds the numerator
		denominator = (f1.denominator * f2.denominator);	//adds the denominator	
		
		//numerator.reduce();
		//result.reduce();
//			result.normalize();
		//	return result;
	}	//end function add
		
	void Rational::sub(Rational f1, Rational f2) {				
		//	a / b - c / d =[n]( a * d - b * c)[d](b * d)
		numerator = (f1.numerator * f2.denominator) - (f1.denominator * f2.numerator); //subtracts the numerator
		denominator = (f1.denominator * f2.denominator);							   //subtracts the denominator
	}	//end function sub
	
	void Rational::mult(Rational f1, Rational f2) {			
		//a / b * c / d = [n]( a * c) [d](b * d)
		numerator = (f1.numerator * f2.numerator);									   //multiplys the numerator
		denominator = (f1.denominator * f2.denominator);							   //multiplys the denominator
	}	//end function mult
	
	void Rational::div(Rational f1, Rational f2) {			
		//a / b / c / d =[n](a * d)[d](b * c)
		numerator = (f1.numerator * f2.numerator);									   //divides the numerator
		denominator = (f1.denominator * f2.denominator);							   //divides the denominator
	}	//end function div
	
	void Rational::displaysgl() {		
		cout << numerator << "/" << denominator;	
	}	//end function displaysgl

	void Rational::displayfloat(Rational f3) {
		deci = ((float)f3.numerator) / f3.denominator;
		cout << fixed << setprecision( 3 )<< deci;
	}	//end function displayfloat
	
int main() {
	Rational f1;
	Rational f2;
	Rational f3;
	Rational d;
	 int a;
	 do 
	 {
		 cout << endl; 
		 cout <<"1. Input rationals" << endl;
		 cout <<"2. Add rationals" << endl; 
		 cout <<"3. Subtract rationals" << endl;
		 cout <<"4. Multiply rationals" << endl;
		 cout <<"5. Divide rationals" << endl;
		 cout <<"6. Quit" << endl << endl;
		 cout <<"Selection choice: "; 
		 cin >> a;
		 switch(a)
		 {
			 case 1:
				 cout<<endl<<"Enter info for first Rational";
				 f1.sglfrac();
				 cout<<endl<<"Enter info for second Rational";
				 f2.sglfrac();
			 break;
			 case 2:
				f3.add(f1, f2);
						cout << setw(8);
					 f1.displaysgl();
						cout << " + "; 
					 f2.displaysgl();
						cout << " = ";
					 f3.displaysgl();
						cout << " or ";
					 d.displayfloat(f3);
						cout << endl;
			 break;
			 case 3:
				f3.sub(f1, f2);
					 f3.add(f1, f2);
						cout << setw(8);
					 f1.displaysgl();
						cout << " + "; 
					 f2.displaysgl();
						cout << " = ";
					 f3.displaysgl();
						cout << " or ";
					 d.displayfloat(f3);
						cout << endl;								 
			 case 4: 
				f3.mult(f1, f2);
					 f3.add(f1, f2);
						cout << setw(8);
					 f1.displaysgl();
						cout << " + "; 
					 f2.displaysgl();
						cout << " = ";
					 f3.displaysgl();
						cout << " or ";
					 d.displayfloat(f3);
						cout << endl;			 
			 break;
			 case 5:
				f3.div(f1, f2);
						cout << setw(8);
					 f1.displaysgl();
						cout << " + "; 
					 f2.displaysgl();
						cout << " = ";
					 f3.displaysgl();
						cout << " or ";
					 d.displayfloat(f3);
						cout << endl;			 
			 break;
			 case 6:
			 exit(1);
			 break;
		 }
	 } while (a!=6);
 }	//end main

Hello,
I have just looked at your thread and have come up with a rather simple solution to part 'b' of your question.

What you could do is.

1) Check if (denominator < 0 ) //this would determine if the value of the denominator is negative.

2)If (denominator < 0 ) returns false, you will have no problem. However if it is true ... You will need to the following.

3) Multiply -1 with denominator and multiply numerator with -1 .

And then thats it .. I GUESS your problem will be solved.


-------
And as far as part 'a' is concerned. You should note that the changes in the value of the numerator and denominator will not affect the variable passed in main as it is being passed-by-value.

Instead pass a reference as the parameter to the function. (pass by reference)


Hope this helps

Thank you so much ok pass by reference I've heard about it and read up stuff on it but I do not completely understand it.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.