Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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.

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  #include  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< 

main.cpp

int main() { using namespace std; int numOne, denOne, numTwo, denTwo; cout<<"Please enter the first fraction's numerator and denominator"<>numOne; cin>>denOne; Rational r1(numOne, denOne); r1.print(); r1.printFloat(); cout<<"============================="<>numTwo; cin>>denTwo; Rational r2(numTwo,denTwo); r2.print(); r2.printFloat(); cout<<"============================="<return 0; }

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

More Books

Students also viewed these Databases questions

Question

3. Existing organizations and programs constrain behavior.

Answered: 1 week ago