Hey i have no clue what to put in the ReduceFration and CommonDemoninator sections.

I know that you call the gcd function for ReduceFration and lcm function for CommonDenominator.

Should be easy few lines of code but I'm a Beginner!

Here's the code:

#include <iostream>
using namespace std;

int gcd(int x,int y)
{
    int t;
    t = x % y;
    while (t != 0)
    {
        x = y;
        y = t;
        t = x % y;
    }
        return y;
}

int lcm(int x,int y)
{
    int ans, cnt;

    if (x > y)
    {
        cnt = x;
    }
    else
    {
        cnt = y;
    }
    ans = 0;
    while (ans == 0)
    {
        if (cnt % x == 0 && cnt % y == 0)
        {
            ans = cnt;
        }
        else
        {
            cnt++;
        }   
    }
    return ans;
}

void ReduceFraction(int &num,int &den)
{
   // Missing function goes here!
}
void CommonDenominator(int &num1, int &den1, int &num2, int &den2)
{
   // Missing function goes here!
}

void main()
{
    int num1, den1, num2, den2, newnum, newden;
    char slash1, slash2, op;

    cout << "\nFraction Calculator\n\n";
    cout << "Add, subtract, multiply & divide - positive fractions only\n";
    cout << "Enter '0/0 + 0/0' to quit.\n";

    // Input will be assumed to be in correct form for simplification
    // Input data before loop in case they want to exit right away

    cout << "\n> ";
    cin >> num1 >> slash1 >> den1 >> op >> num2 >> slash2 >> den2;
    while (num1 + den1 + num2 + den2 > 0)
    {
        // Reduce both fractions to keep integers as small as possible
        ReduceFraction(num1, den1);
        ReduceFraction(num2, den2);
        switch (op) 
        {
            case '+':
               // Find common denominator and add
               CommonDenominator(num1,den1,num2,den2);
               newnum = num1 + num2;
               newden = den1;
               break;
            case '-':
               // Find common denominator and subtract
               CommonDenominator(num1,den1,num2,den2);
               newnum = num1 - num2;
               newden = den1;
               break;
            case '*':
               // Multiply numerators and multiply denominators
               newnum = num1 * num2;
               newden = den1 * den2;
               break;
            case '/':
               // Invert and multiply
               newnum = num1 * den2;
               newden = den1 * num2;
        }
        // Reduce the answer to lowest terms
        ReduceFraction(newnum, newden);

        // Output the results
        cout << num1 << "/" << den1 << " " << op << " ";
        cout << num2 << "/" << den2 << " = ";
        cout << newnum << "/" << newden << endl;

        // Input data for next iteration of loop
        cout << "\n> ";
        cin >> num1 >> slash1 >> den1 >> op >> num2 >> slash2 >> den2;
    }
}

Recommended Answers

All 3 Replies

I'm not really sure of what you're asking. First of all that code is cryptic and you're not really saying what is the problem. I'm not going to write code for you.

I have to agree with Terminator1337 on this, the code for the main() function in particular is wretched (not to mention incorrect - in C++, main() should always return an int, with no exceptions to the rule). The fact that it appears to have been given to you by the instructor makes this even less acceptable.

As for the ReduceFraction() function, simply think through how you would do it manually: you first find the greatest common divisor of the numerator and the denominator, then divide them each by said divisor. Simple, no?

Getting the common denominator of two ratios is almost as easy: you multiply the denominators, then multiply num1 by den2 for the new num1, and num2 by den1 for the new num2.

Should be easy few lines of code but I'm a Beginner!

I'd suspect that the problem is you don't understand how to reduce a fraction using the gcd, or normalize two fractions from the lcm. Like you said, it's an easy few lines, but if you don't know how to do it manually, you'll never be able to write the code for it.

So your first step is to break out the old elementary math book and read up on fractions. The "hard" parts are already done: gcd and lcm are already written and can be used to reduce and normalize.

First of all that code is cryptic and you're not really saying what is the problem.

How is the code cryptic? It looks straightforward to me. Obviously the question is how to write fraction reduction and common denominator for addition and subtraction. When gcd and lcm are already written, those two functions are absolutely trivial (two or three lines of code apiece), provided one knows the math behind 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.