Noob euclidean algortihm fractions
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2

Thread: Noob euclidean algortihm fractions

  1. #1
    Join Date
    Feb 2013
    Posts
    1

    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)
    {
    case '1': //addition

    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;

  2. #2
    Join Date
    Jul 2005
    Location
    Netherlands
    Posts
    2,016

    Re: Noob euclidean algortihm fractions

    Quote Originally Posted by gobrien88 View Post
    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.
    Cheers, D Drmmr

    Please put [code][/code] tags around your code to preserve indentation and make it more readable.

    As long as man ascribes to himself what is merely a posibility, he will not work for the attainment of it. - P. D. Ouspensky

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center