# Noob euclidean algortihm fractions

• February 26th, 2013, 02:50 PM
gobrien88
Noob euclidean algortihm fractions
Hi my teacher gave us an assignment and I have gotten every thing taken care of except for understanding where to apply the Euclidean Algorithm to reduce my fractions... below is the source code and where i have attempted to use the algorithm are commented out. Please help me figure out how and where to apply it..

#include <iostream>
using namespace std;

int main ()

{
char operation = ' ';
int numerator1;
int denominator1;
int numerator2;
int denominator2;
int finalNumerator;
int finalDenominator;
int small;
int large;
int remainder;

//enter operation number code
cout << "Addition -- 1" << endl;
cout << "Subtraction -- 2" << endl;
cout << "Multiplication -- 3" << endl;
cout << "Division -- 4" << endl;
cout << "Please enter the number of operation you want: ";
cin >> operation;

cout << "Please enter the numerator of the first value: ";
cin >> numerator1;
cout << "Please enter the denominator of the first value: ";
cin >> denominator1;
cout << "Please enter the numerator of the second value: ";
cin >> numerator2;
cout << "Please enter the denominator of the second value: ";
cin >> denominator2;

if (denominator1 == 0 || denominator2 == 0)
{
cout << "Invalid value for denominator"<< endl;
}
else
//endif
switch (operation)
{

finalNumerator = (numerator1 * denominator2) + (numerator2 * denominator1);
finalDenominator = denominator1 * denominator2;

if (finalNumerator > finalDenominator)
{
int wholeNumber;
int mixedNumerator;

wholeNumber = finalNumerator / finalDenominator;
mixedNumerator = finalNumerator % finalDenominator;

// mixedNumerator = small;
// finalDenominator = large;
// while (large % small != 0)
// {
// remainder = large % small ;
// large = small;
// small = remainder;
// }

if (mixedNumerator == 0)
{
cout <<"Result is: " << wholeNumber; // for when the answer is only whole number
}
else
{

cout << "Result is: " << wholeNumber << " " << mixedNumerator << "/" << finalDenominator;
}
}
else if (finalNumerator == finalDenominator)
{
int equals1;
equals1 = finalNumerator / finalDenominator;
cout << "Result is: " << equals1;
}
else
{
cout << "Result: " << finalNumerator << '/' << finalDenominator << endl;
}
break;

case '2': //subtraction

finalNumerator = (numerator1 * denominator2) - (numerator2 * denominator1);
finalDenominator = denominator1 * denominator2;
if (finalNumerator > finalDenominator)
{
int wholeNumber = 0;
int mixedNumerator = 0;
int mixedDenominator = 0;

wholeNumber = finalNumerator / finalDenominator;
mixedNumerator = finalNumerator % finalDenominator;
if (mixedNumerator == 0)
{
cout <<"Result is: " << wholeNumber; // for when the answer is only whole number
}
else
{
cout << "Result is: " << wholeNumber << " " << mixedNumerator << "/" << finalDenominator;
}
}
else if (finalNumerator == finalDenominator)
{
int equals1;
equals1 = finalNumerator / finalDenominator;
cout << "Result is: " << equals1;
}
else
{
cout << "Result: " << finalNumerator << '/' << finalDenominator << endl;
}
break;

case '3': //multiplication

finalNumerator = numerator1 * numerator2;
finalDenominator = denominator1 * denominator2;

if (finalNumerator > finalDenominator)
{
int wholeNumber = 0;
int mixedNumerator = 0;

wholeNumber = finalNumerator / finalDenominator;
mixedNumerator = finalNumerator % finalDenominator;

if (mixedNumerator == 0)
{
cout <<"Result is: " << wholeNumber; // for when the answer is only whole number
} // end if
else
{
mixedNumerator = small;
finalDenominator = large;

while (large % small != 0)
{
remainder = large % small ;
large = small;
small = remainder;
}

cout << "Result is: " << wholeNumber << " " << mixedNumerator << "/" << finalDenominator;
}
}// end if
else if (finalNumerator == finalDenominator)
{
int equals1;
equals1 = finalNumerator / finalDenominator;
cout << "Result is: " << equals1;
}
else
{
// finalNumerator = small;
// finalDenominator = large;

// while (large % small != 0)
// {
// remainder = large % small ;
// large = small;
// small = remainder;
// }// end while
// cout << "Result is: " << remainder << '/' << large << endl;
}
break;

case '4': //division

finalNumerator = numerator1 * denominator2;
finalDenominator = denominator1 * numerator2;
if (finalNumerator > finalDenominator)
{
int wholeNumber = 0;
int mixedNumerator = 0;
int mixedDenominator = 0;

wholeNumber = finalNumerator / finalDenominator;
mixedNumerator = finalNumerator % finalDenominator;
if (mixedNumerator == 0)
{
cout <<"Result is: " << wholeNumber; // for when the answer is only whole number
}
else
{
cout << "Result is: " << wholeNumber << " " << mixedNumerator << "/" << finalDenominator;
}
}
else if (finalNumerator == finalDenominator)
{
int equals1;
equals1 = finalNumerator / finalDenominator;
cout << "Result is: " << equals1;
}
else
{
cout << "Result: " << finalNumerator << '/' << finalDenominator << endl;
}
break;

default:
cout << "Invalid selection";
} //end switch

return 0;
• February 27th, 2013, 05:07 AM
D_Drmmr
Re: Noob euclidean algortihm fractions
Quote:

Originally Posted by gobrien88
Hi my teacher gave us an assignment and I have gotten every thing taken care of except for understanding where to apply the Euclidean Algorithm to reduce my fractions... below is the source code and where i have attempted to use the algorithm are commented out. Please help me figure out how and where to apply it..

Please use code tags when posting code. Without proper indenting your code is very hard to read.

Do you know how to reduce a fraction? If not, look up the wikipedia page on Greatest Common Divisor.