Good Afternoon,

I'm having problems with my program that deals with fractions. The problems are with the functions multiply and divide that are not working correctly. So far I have this code.

CPP FILE:

#include <iostream>
#include <cmath>

#include "hw4_head.h"
using namespace std;

int main ()
{
    int a, b, c, d, e, f, gcd;
    int option;
    char op;
    char answer = 'y';
    while (answer != 'n')
    {
    showMenu();

    cout <<"Enter a choice ";
    cin>>option;

    switch(option)
    {
    case 1:
            cout <<"Going to perform the ADD operation on a fraction question"<<endl;
            cout << "Please enter a fraction question \n"
                 <<"in the form of a / b operation c / d ";
            getFract (a, b);
            op = getOperator ();
            getFract (c, d);

            disPlayFract (a, b);
            cout << " " << op << " ";
            disPlayFract (a, d);
            cout <<endl;

            Add (a, b, c, d, e, f);
            cout<<"The answer is equal to"<<endl;
            disPlayFract (e, f);
            cout <<endl;

            gcd = gcf (e, f);
            e = e / gcd;
            f = f / gcd;
            cout <<"The answer in reduce form is"<<endl;
            disPlayFract (e, f);
            cout<<endl;
            break;
    case 2:
            cout <<"Going to perform the SUBSTRACT operation on a fraction question"<<endl;
            cout << "Please enter a fraction question \n"
                 <<"in the form of a / b operation c / d ";
            getFract (a, b);
            op = getOperator ();
            getFract (c, d);

            disPlayFract (a, b);
            cout << " " << op << " ";
            disPlayFract (a, d);
            cout <<endl;

            Substract (a, b, c, d, e, f);
            cout<<"The answer is equal to"<<endl;
            disPlayFract (e, f);
            cout <<endl;

            gcd = gcf (e, f);
            e = e / gcd;
            f = f / gcd;
            cout <<"The answer in reduce form is"<<endl;
            disPlayFract (e, f);
            cout<<endl;
            break;
    case 3:
            cout <<"Going to perform the DIVIDE operation on a fraction question"<<endl;
            cout << "Please enter a fraction question \n"
                 <<"in the form of a / b operation c / d ";
            getFract (a, b);
            op = getOperator ();
            getFract (c, d);

            disPlayFract (a, b);
            cout << " " << op << " ";
            disPlayFract (a, d);
            cout <<endl;

            Divide (a, b, c, d, e, f);
            cout<<"The answer is equal to"<<endl;
            disPlayFract (e, f);
            cout <<endl;

            gcd = gcf (e, f);
            e = e / gcd;
            f = f / gcd;
            cout <<"The answer in reduce form is"<<endl;
            disPlayFract (e, f);
            cout<<endl;
            break;
    case 4:
            cout <<"Going to perform the MULTIPLY operation on a fraction question"<<endl;
            cout << "Please enter a fraction question \n"
                 <<"in the form of a / b operation c / d ";
            getFract (a, b);
            op = getOperator ();
            getFract (c, d);

            disPlayFract (a, b);
            cout << " " << op << " ";
            disPlayFract (a, d);
            cout <<endl;

            Multiply (a, b, c, d, e, f);
            cout<<"The answer is equal to"<<endl;
            disPlayFract (e, f);
            cout <<endl;

            gcd = gcf (e, f);
            e = e / gcd;
            f = f / gcd;
            cout <<"The answer in reduce form is"<<endl;
            disPlayFract (e, f);
            cout<<endl;
            break;
    default:
            cout <<"You entered an invalid input"<<endl;
    }

    cout << "Would you like to workout another problem (y/n) ";
    cin>>answer;

    }
    cout <<"Good Bye."<<endl;


    return 0;
}

Header file:

#include <iostream>
#include <cmath>

using namespace std;

#ifndef     HW4_HEAD_H
#define     HW4_HEAD_H

void getFract( int& a, int& b)
{
    char dummy;
    cin>>a>>dummy>>b;
}

char getOperator ()
{
    char op;
    cin>>op;
    return op;
}

void disPlayFract (const int a, const int b)
{
    cout << a << " / "<< b;
}

void Add (const int a, const int b, const int c, const int d, int& e, int& f)
{
    e = a * d + b * c;
    f = b * d;
}

void Substract (const int a, const int b, const int c, const int d, int& e, int& f)
{
    e = a * d - b * c;
    f = b * d;
}

void Divide (const int a, const int b, const int c, const int d, int& e, int& f)
{
    e = (a * d) / (c *b);
    f = c * d;
}

void Multiply (const int a, const int b, const int c, const int d, int& e, int& f)
{
    e = (a / d) * (b / c);
    f = b * d;
}
int gcf (int A, int B)
{
    int R;
    R = A % B;
    while (R != 0)
    {
        A = B;
        B = R;
        R = A % B;
    }
    return B;
}

void showMenu()
{
    cout<<"**************Fractional Arithmetic Program Menu***********************"<<endl;
    cout<<"Enter 1 to perform ADD operation on a fraction question"<<endl;
    cout<<"Enter 2 to perform SUBSTRACT operation on a fraction question"<<endl;
    cout<<"Enter 3 to perform DIVIDE operation on a fraction question"<<endl;
    cout<<"Enter 4 to perform MULTIPLY operation on a fraction question"<<endl;
}

#endif

I really appreciate any help that I can get.

Dani AI

Generated

Immediate problems in the posted program: the Multiply and Divide implementations are incorrect; the second operand is printed with the wrong variable (calls disPlayFract(a, d) instead of the second fraction); the gcd routine can misbehave on zero/negative inputs; and there are no checks for zero denominators or dividing by a zero fraction. and rightly flagged the arithmetic errors — the next step is to make the code robust and keep invariants (denominator ≠ 0, reduced form, positive denominator).

A safer design is to encapsulate fractions in a small type that enforces those invariants and provides operators. Example (illustrative, compact):

#include <iostream>
#include <stdexcept>

struct Fraction {
    long long n, d;
    Fraction(long long num = 0, long long den = 1) : n(num), d(den) { normalize(); }

private:
    static long long gcd(long long a, long long b) {
        a = a < 0 ? -a : a; b = b < 0 ? -b : b;
        if (b == 0) return a == 0 ? 1 : a;
        while (b) { long long r = a % b; a = b; b = r; }
        return a;
    }
    void normalize() {
        if (d == 0) throw std::invalid_argument("zero denominator");
        if (d < 0) { n = -n; d = -d; }
        long long g = gcd(n, d);
        n /= g; d /= g;
    }

public:
    friend Fraction operator+(const Fraction &a, const Fraction &b)
        { return Fraction(a.n * b.d + b.n * a.d, a.d * b.d); }
    friend Fraction operator-(const Fraction &a, const Fraction &b)
        { return Fraction(a.n * b.d - b.n * a.d, a.d * b.d); }
    friend Fraction operator*(const Fraction &a, const Fraction &b)
        { return Fraction(a.n * b.n, a.d * b.d); }
    friend Fraction operator/(const Fraction &a, const Fraction &b) {
        if (b.n == 0) throw std::domain_error("divide by zero fraction");
        return Fraction(a.n * b.d, a.d * b.n);
    }
    void print(std::ostream &os = std::cout) const { os << n << " / " << d; }
};

Quick checklist to apply to the original program:

  • Fix the display call so the second fraction is disPlayFract(c, d).
  • Replace the bad Multiply/Divide formulas with the correct arithmetic (or use the Fraction type above).
  • Always validate input denominators and check for division by a zero fraction before doing the operation.
  • Make gcd robust for zero/negative inputs (or use std::gcd when available).
  • Avoid defining non-inline function bodies in headers and avoid using namespace std; in headers.
  • Consider larger integer types or intermediate 128-bit arithmetic to detect/avoid overflow when multiplying numerators/denominators.

These changes correct the immediate bugs flagged by and and make the program safer and easier to maintain.

Recommended Answers

All 4 Replies

jaw drops

You'll need help if this is the way you're being taught how to do things. You don't even know how much you need it.

If you don't mind me asking, has your coursework covered basic data structures - using either struct or class - yet, and if so, did the instructor show you how they would be applied to this project? If so, then you've misunderstood the project; if not, then the instructor has misunderstood it, which is even worse. Given what I've seen so far of the way your course is being taught, I can hazard a guess, but I'd prefer to give the professor the benefit of a doubt.

OK, personal revulsion at the particular way this is being implemented aside, let's fix the division and multiplication methods. Right now, your formulae are entirely wrong for these two functions, and I'm not sure just how you (or, I'm guessing, your professor) managed to come up with the approach given. The correct formula for division is

//  e/f = (a/b) / (e/f) = (b * c) / (a * d) 

void Divide (const int a, const int b, const int c, const int d, int& e, int& f)
{
    e = b * c;   
    f = a * d;
}

and

// e/f = (a/b) * (c/d) = (a * c) / (b * d)
void Multiply (const int a, const int b, const int c, const int d, int& e, int& f)
{
    e = a * c;
    f = b * d;
}

Except, of course, that no experienced programmer in his or her right mind would do it this way. The whole point of assigning a fractions project is to teach the students how to use compound data structures; it is both uniformative and confusing to do this exercise otherwise, unless the intention is to have the students re-do the whole thing later using a struct type or a class. Even if that is what the professor has in mind, it's still a rather wasteful way to do this.

Sorry if I'm ranting again, but this sort of incompetent teaching is inexcusable.

// e/f = (a/b) / (e/f) = (b * c) / (a * d)

e/f = (a/b) / (c/d) = (a/b) * (d/c) = (a * d) / (b * c), you mean?

commented: Oopps, thanks for the correction. +8

Ugh, you are correct, I got it backwards. Sorry if I misled you PoloBlue, especially given my ranting about careless instructions.

Schol-Rea-Lea and pyTony,

Thank you for your help, I really appreciated your input.

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.