Question

Having to repost this as the last answer wasn't functional. Please help In the following program...

Having to repost this as the last answer wasn't functional. Please help

In the following program written in C++, I am having an issue that I cannot get the reducdedForm function in the Rational.cpp file to work. I cant figure out how to get this to work correctly, so the program will take what a user enters for fractions, and does the appropriate arithmetic to the two fractions and simplifies the answer.

Rational.h

class Rational {
private:
    int num;
    int denom;


public:
    int* Rational::reducedForm(int num, int denom);
    Rational(int =0, int=0);
    Rational add(const Rational &);//read as result = ("this"+  arg)
    Rational subtract(const Rational &); //read as result = ("this" -  arg)
    Rational multiply(const Rational &); //read as result = ("this" *  arg)
    Rational divide(const Rational &); //read as result  ("this" /  arg)
    void print();
    void printFloat();

};


#endif

Rational.cpp

#include "Rational.h"
#include <iostream>
#include <iomanip>

Rational::Rational(int anum, int adenom){
    int * reduced = reducedForm(anum, adenom);
    num = reduced[0];
    denom = reduced[1];
    delete reduced;  //delete the array that was on the heap (garbage collect)
}

int* Rational::reducedForm(int num, int denom){
    //first get the GCD for a and b
    int a = num, b = denom, c;
    int* ans = new int[2];          //array created on heap
    while ( a != 0 ) {
        c = a; a = b%a;  b = c;
    }
    //then reduce
    ans[0]= num/b;    //reduced numerator
    ans[1]= denom/b;  //reduced denominator
    return ans;
}


Rational Rational::add(const Rational & r2){//read as result = (this + arg)

    int numerator = (num * r2.denom) + (r2.num * denom);
    int denominator = denom * r2.denom;
    Rational sum(numerator, denominator);
    return sum;
}


Rational Rational::subtract(const Rational & r2){ //read as result = (this - arg)


    int numerator = (num * r2.denom) - (r2.num * denom);
    int denominator = denom * r2.denom;
    Rational sum(numerator, denominator);
    return sum;


}
Rational Rational::multiply(const Rational & r2){ //read as result = (this * arg)


    int numerator = (num * r2.num);
    int denominator = denom * r2.denom;
    Rational sum(numerator, denominator);
    return sum;
}

Rational Rational::divide(const Rational & r2){ //read as result  (this / arg)

    int numerator = num * r2.denom;
    int denominator = denom * r2.num;
    Rational sum(numerator, denominator);
    return sum;
}
void Rational::print(){

    std::cout<< num<<"/" << denom << std::endl;
}

void Rational::printFloat(){
    double f = (double) num/denom;
    std::cout<<std::fixed <<std::setprecision(3) << f << std::endl;
}

main.cpp

int main() {
    using namespace std;

    int numOne, denOne, numTwo, denTwo;

    cout<<"Please enter the first fraction's numerator and denominator"<<endl;
    cin>>numOne;
    cin>>denOne;
    Rational r1(numOne, denOne);
    r1.print();
    r1.printFloat();
    cout<<"============================="<<endl;

    cout<<"Pleae enter the second fraction's numerator and denominaytor"<<endl;
    cin>>numTwo;
    cin>>denTwo;
    Rational r2(numTwo,denTwo);
    r2.print();
    r2.printFloat();
    cout<<"============================="<<endl;


    cout<<"The addition is: "<<endl;
    Rational ans = r1.add(r2);
    ans.print();
    ans.printFloat();
    cout<<"============================="<<endl;


    cout<<"The subtraction is: "<<endl;
    ans=r1.subtract(r2);
    ans.print();
    ans.printFloat();
    cout<<"============================="<<endl;


    cout<<"The multiplication is: "<<endl;
    ans=r1.multiply(r2);
    ans.print();
    ans.printFloat();
    cout<< "============================="<<endl;


    cout <<"The division is: " <<endl;
    ans=r1.divide(r2);
    ans.print();
    ans.printFloat();
    cout<<"============================="<<endl;


    return 0;

}
0 0
Add a comment Improve this question Transcribed image text
Answer #1

The solution for the above question is given below with the screenshot of output.
---------------------------------------------------------------------------------------------------------------

I have kept the logic simple and output as per the question.

If there is anything else do let me know in comments

---------------------------------------------------------------------------------------------------------------

--------------- CODE TO COPY -----------------------------------------------------------------------

Rational.h

#ifndef _RATIONAL_H_
#define _RATIONAL_H_

class Rational {
  
private:
int num;
int denom;


public:

  
  
Rational(int =0, int=0);
  
int* reducedForm(int num, int denom);
  
Rational add(const Rational &);//read as result = ("this"+ arg)
Rational subtract(const Rational &); //read as result = ("this" - arg)
Rational multiply(const Rational &); //read as result = ("this" * arg)
Rational divide(const Rational &); //read as result ("this" / arg)
  
void print();
void printFloat();

};


#endif

Rational.cpp

#include "Rational.h"
#include <iostream>
#include <iomanip>


Rational::Rational(int anum, int adenom){
  
int * reduced = reducedForm(anum, adenom);
num = reduced[0];
denom = reduced[1];
delete reduced; //delete the array that was on the heap (garbage collect)
}


int* Rational::reducedForm(int num, int denom){
  
//first get the GCD for a and b
int a = num, b = denom, c;
int* ans = new int[2]; //array created on heap
  
while ( a != 0 ) {
  
c = a;
a = b%a;
b = c;
}
//then reduce
ans[0]= num/b; //reduced numerator
ans[1]= denom/b; //reduced denominator
return ans;
}


Rational Rational::add(const Rational & r2){//read as result = (this + arg)

int numerator = (num * r2.denom) + (r2.num * denom);
int denominator = denom * r2.denom;
Rational sum(numerator, denominator);
return sum;
}


Rational Rational::subtract(const Rational & r2){ //read as result = (this - arg)


int numerator = (num * r2.denom) - (r2.num * denom);
int denominator = denom * r2.denom;
Rational sum(numerator, denominator);
return sum;


}
Rational Rational::multiply(const Rational & r2){ //read as result = (this * arg)


int numerator = (num * r2.num);
int denominator = denom * r2.denom;
  
Rational sum(numerator, denominator);
  
return sum;
}

Rational Rational::divide(const Rational & r2){ //read as result (this / arg)

int numerator = num * r2.denom;
int denominator = denom * r2.num;
  
Rational sum(numerator, denominator);
  
return sum;
  
}

void Rational::print(){

std::cout<< num<<"/" << denom << std::endl;
}

void Rational::printFloat(){
  
double f = (double) num/denom;
std::cout<<std::fixed <<std::setprecision(3) << f << std::endl;
  
}

main.cpp

#include <iostream>
#include "Rational.h"
using namespace std;


int main() {
  
  
int numOne, denOne, numTwo, denTwo;

cout<<"Please enter the first fraction's numerator and denominator"<<endl;
cin>>numOne;
cin>>denOne;
Rational r1(numOne, denOne);
r1.print();
r1.printFloat();
cout<<"============================="<<endl;

cout<<"Pleae enter the second fraction's numerator and denominaytor"<<endl;
cin>>numTwo;
cin>>denTwo;
Rational r2(numTwo,denTwo);
r2.print();
r2.printFloat();
cout<<"============================="<<endl;


cout<<"The addition is: "<<endl;
Rational ans = r1.add(r2);
ans.print();
ans.printFloat();
cout<<"============================="<<endl;


cout<<"The subtraction is: "<<endl;
ans=r1.subtract(r2);
ans.print();
ans.printFloat();
cout<<"============================="<<endl;


cout<<"The multiplication is: "<<endl;
ans=r1.multiply(r2);
ans.print();
ans.printFloat();
cout<< "============================="<<endl;


cout <<"The division is: " <<endl;
ans=r1.divide(r2);
ans.print();
ans.printFloat();
cout<<"============================="<<endl;


return 0;
  
}

---------------------------------------------------------------------------------------------------------------

Output :

gcc version 4.6.3 Please enter the first fractions numerator and denominator 4 8.500 Pleae enter the second fractions numer

Add a comment
Know the answer?
Add Answer to:
Having to repost this as the last answer wasn't functional. Please help In the following program...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • C++ I am using visual studio for it. Make a copy of the Rational class you...

    C++ I am using visual studio for it. Make a copy of the Rational class you created in the previous Lab. Modify the class. Replace all your mathematical, input, and output functions with overloaded operators. Overload the following 12 operators: + - * / < > = = ! = <= >= >> << In order to test your class in your main function, prompt the user for a numerator and a denominator for your first object. Repeat the prompt...

  • 13.21 Lab: Rational class This question has been asked here before, but every answer I have...

    13.21 Lab: Rational class This question has been asked here before, but every answer I have tested did not work, and I don't understand why, so I'm not able to understand how to do it correctly. I need to build the Rational.cpp file that will work with the main.cpp and Rational.h files as they are written. Rational Numbers It may come as a bit of a surprise when the C++ floating-point types (float, double), fail to capture a particular value...

  • C++ 1st) [Note: This assignment is adapted from programming project #7 in Savitch, Chapter 10, p.616.]...

    C++ 1st) [Note: This assignment is adapted from programming project #7 in Savitch, Chapter 10, p.616.] Write a rational number class. Recall that a rational number is a ratio-nal number, composed of two integers with division indicated. The division is not carried out, it is only indicated, as in 1/2, 2/3, 15/32. You should represent rational numbers using two int values, numerator and denominator. A principle of abstract data type construction is that constructors must be present to create objects...

  • C++ I need help adding to this program. Here's my assignment: (1) Add the following functions...

    C++ I need help adding to this program. Here's my assignment: (1) Add the following functions to your working mixed number class: Overload the ==, <, and > comparison operators Overload the + and * arithmetic operators Overload the insertion (<<) operator: It should display whole part, space, numerator, forward slash, denominator: 5 4/13 Overload the extraction (>>) operator: It should read integer, integer, character, integer. That is, it can assume that the numerator, forward slash, and denominator are input...

  • Adapt your Rational class : public class Rational { private int num; private int denom; public...

    Adapt your Rational class : public class Rational { private int num; private int denom; public Rational() { num = 0; denom = 1; } public Rational(int num, int denom) { this.num = num; this.denom = denom; } int getNum() { return num; } int getDenom() { return denom; } public Rational add(Rational rhs) { return new Rational(num * rhs.denom + rhs.num * denom, denom * rhs.denom); } public Rational subtract(Rational rhs) { return new Rational(num * rhs.denom - rhs.num...

  • If you have already answered this question, please do not repost your old solutions, you will be thumbs downed. I'm...

    If you have already answered this question, please do not repost your old solutions, you will be thumbs downed. I'm looking for NEW solutions only! Frac.h: // a Fraction object holds one Fraction number, one fraction #ifndef FRAC_H #define FRAC_H #include <iostream> using namespace std; //Creaing a Fraction class class Fraction { public: Fraction(int = 0, int = 1); // Function Declarations which performs operations on Fraction class Fraction add(const Fraction &); Fraction subtract(const Fraction& a); Fraction multiply(const Fraction& a);...

  • Header file for the Rational class: #ifndef RATIONAL_H #define RATIONAL_H class Rational { public: Rational( int...

    Header file for the Rational class: #ifndef RATIONAL_H #define RATIONAL_H class Rational { public: Rational( int = 0, int = 1 ); // default constructor Rational addition( const Rational & ) const; // function addition Rational subtraction( const Rational & ) const; // function subtraction Rational multiplication( const Rational & ) const; // function multi. Rational division( const Rational & ) const; // function division void printRational () const; // print rational format void printRationalAsDouble() const; // print rational as...

  • Hello, im looking for help on adding overload operators to my code. It needs to consist...

    Hello, im looking for help on adding overload operators to my code. It needs to consist of + (addition) for the Fraction class * (multiplication), no need not reduce == (equality operator) = (assignment operator) << and >> so in main you can use << and >> as follows:  cin>>frac1; cout<< frac1; where frac1 is a an object of the Fraction class   the original code is: #include <iostream> using namespace std; class Fractions { private:    int numerator;    int denominator;...

  • NEED ASAP PLEASE HELP Task: --------------------------------------------------------------------------------------...

    NEED ASAP PLEASE HELP Task: --------------------------------------------------------------------------------------------------------------------------------- Tasks to complete: ---------------------------------------------------------------------------------------------------------------------------------------- given code: ----------------------------------------------------------------------------------------------------------------------------------------------- main.cpp #include <iostream> #include <iomanip> #include "fractionType.h" using namespace std; int main() { fractionType num1(5, 6); fractionType num2; fractionType num3; cout << fixed; cout << showpoint; cout << setprecision(2); cout << "Num1 = " << num1 << endl; cout << "Num2 = " << num2 << endl; cout << "Enter the fraction in the form a / b: "; cin >> num2; cout << endl; cout <<...

  • SEE THE Q3 for actual question, The first Two are Q1 and Q2 answers . Q1...

    SEE THE Q3 for actual question, The first Two are Q1 and Q2 answers . Q1 #include<iostream> using namespace std; // add function that add two numbers void add(){    int num1,num2;    cout << "Enter two numbers "<< endl;    cout << "First :";    cin >> num1;    cout << "Second :";    cin >>num2;    int result=num1+num2;    cout << "The sum of " << num1 << " and "<< num2 <<" is = "<< result;   ...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT