I need help using the GCD Algo. in my program, I can't figure out to use the algo in my program. The algo:

int gcd(int a, int b){
 assert(b != 0);
    int rem = a % b;
    while(rem !=0 ){
       a = b;
       b = rem;
       rem = a % b;
    }
    return b;

The program:

#include <iostream>
using namespace std;


int Num, Num2, Denom, Denom2;
int res_up, res_down;
int Low_Value, High_Value;
char opchar,answer,symbol;


void Calculate()
{
    switch (opchar) {
    case '+':
        res_up=(Denom2*Num) + (Denom*Num2);
                    res_down= Denom*Denom2;
                        symbol = '+';
                    break;
    case '-':
        res_up=(Denom2*Num) - (Denom*Num2);
                    res_down= Denom*Denom2;
                         symbol = '-';
                    break;
    case '*':
        res_up= Num * Num2;
                    res_down= Denom * Denom2;
                        symbol = '*';
                    break;
    case '/':
        res_up= Num * Denom2;
                    res_down= Denom * Num2;
                        symbol = '/';
                    break;

    default: // If operator is illegal shut program down
        cout << "Invalid operator." << endl;
}
}

void input()

{
    cout << "Enter an operation to perform { + - / * } ";
    cin >> opchar;
    cout << endl;
    cout << "Enter the numerator and denominator for the first fraction; include      a space: ";
        cin >> Num >> Denom;
        cout << endl;
        cout << "Enter the numerator and denominator for the second fraction; include a space: ";
        cin >> Num2 >> Denom2;
        cout << endl;

}


//-------------------------------------------------------------------


void output()

{
         cout << "n " << Num << "      " << Num2 << "       " << res_up << "      " << endl;
         cout << "---  " << symbol << " ---  =  ---  =n";
         cout << " " << Denom << "      " << Denom2 << "       " << res_down << endl;
     cout <<"Would you like to do another fraction? ";
         cin >> answer;
         cout << endl;

}

   int main()
   {
     do
     { input();                                         
      Calculate();
      output();
      } while ((answer == 'y') || (answer == 'Y')); 
      return 0;  
   }       







}

Edited 3 Years Ago by Dani: Formatting fixed

By the way i figured out how to reduce the fractions using the following but I have to use the gcd algo. and i have to use it to reduce the frac. :

 void reduce()  
      {
         if(res_up > res_down)
         {
            Low_Value = res_down;
            High_Value = res_up;
         }
         else
         {
            High_Value = res_down;
            Low_Value = res_up;
         }
         for(int i = Low_Value; i > 0;i--)
         {
            if( (Low_Value % i == 0) && (High_Value % i == 0) )
            {
               res_up=res_up/i;
               res_down= res_down/i;
            }

Edited 3 Years Ago by Dani: Formatting fixed

sorry, with code tags now:

int gcd(int a, int b){
 assert(b != 0);
    int rem = a % b;
    while(rem !=0 ){
       a = b;
       b = rem;
       rem = a % b;
    }
    return b;

The program:
#include <iostream>
using namespace std;


int Num, Num2, Denom, Denom2;
int res_up, res_down;
int Low_Value, High_Value;
char opchar,answer,symbol;


void Calculate()
{
	switch (opchar) {
	case '+':
	  	res_up=(Denom2*Num) + (Denom*Num2);
            		res_down= Denom*Denom2;
            			symbol = '+';
					break;
	case '-':
		res_up=(Denom2*Num) - (Denom*Num2);
            		res_down= Denom*Denom2;
            			 symbol = '-';
					break;
	case '*':
		res_up= Num * Num2;
            		res_down= Denom * Denom2;
            			symbol = '*';
					break;
	case '/':
		res_up= Num * Denom2;
            		res_down= Denom * Num2;
            			symbol = '/';
					break;

	default: // If operator is illegal shut program down
		cout << "Invalid operator." << endl;
}
}

void input()

{
cout << "Enter an operation to perform { + - / * } ";
cin >> opchar;
cout << endl;
cout << "Enter the numerator and denominator for the first fraction; include a space: ";
cin >> Num >> Denom;
cout << endl;
cout << "Enter the numerator and denominator for the second fraction; include a space: ";
        cin >> Num2 >> Denom2;
        cout << endl;
        
}


//-------------------------------------------------------------------


void output()

{
cout << "\n " << Num << "      " << Num2 << "       " << res_up << "      " << endl;
cout << "---  " << symbol << " ---  =  ---  =\n";
cout << " " << Denom << "      " << Denom2 << "       " << res_down << endl;
cout <<"Would you like to do another fraction? ";
cin >> answer;
cout << endl;
      
}

   int main()
   {
     do
     { input();                                         
      Calculate();
      output();
      } while ((answer == 'y') || (answer == 'Y')); 
      return 0;  
   }

Reducing a fraction is pretty easy with the GCD:

#include <iostream>

using namespace std;

int gcd ( int m, int n )
{
  return n ? gcd ( n, m % n ) : m;
}

int main()
{
  int num, den;

  cout<<"Enter a fraction: ";
  cin>> num;
  cin.get();
  cin>> den;

  int div = gcd ( num, den );
  cout<<"The reduced fraction is "<< num / div <<'/'<< den / div <<endl;
}

Though you should take that GCD function with a grain of salt. It's quick and dirty. ;)

Reducing a fraction is pretty easy with the GCD:

#include <iostream>

using namespace std;

int gcd ( int m, int n )
{
  return n ? gcd ( n, m % n ) : m;
}

int main()
{
  int num, den;

  cout<<"Enter a fraction: ";
  cin>> num;
  cin.get();
  cin>> den;

  int div = gcd ( num, den );
  cout<<"The reduced fraction is "<< num / div <<'/'<< den / div <<endl;
}

Though you should take that GCD function with a grain of salt. It's quick and dirty. ;)

Thanks for the help.

This article has been dead for over six months. Start a new discussion instead.