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; }
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 :
Having to repost this as the last answer wasn't functional. Please help In the following program...
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...
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);...
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...
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; ...
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...
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 <<...
Rational will be our parent class that I included to this post, Implement a sub-class MixedRational. This class should Implement not limited to: 1) a Constructor with a mathematically proper whole, numerator and denominator values as parameters. 2) You will override the: toString, add, subtract, multiply, and divide methods. You may need to implement some additional methods, enabling utilization of methods from rational. I have included a MixedRational class with the method headers that I used to meet these expectations....
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...
I'm kind of new to programming, and I am having trouble figuring out why my program isn't running. Below is the code that I wrote for practice. I will comment where it says the error is. So the error that I'm getting is "error: no match for 'operator>>' (operand types are 'std::istream {aka std::basic_istream<char>}' ". I'm not sure why I'm getting this because I added the library <iostream> at the top. Thank you. Code: #include <iostream> using namespace std; class...
C++ Object Oriented assignment Can you please check the program written below if it has appropriately fulfilled the instructions provided below. Please do the necessary change that this program may need. I am expecting to get a full credit for this assignment so put your effort to correct and help the program have the most efficient algorithm within the scope of the instruction given. INSTRUCTIONS Create a fraction class and add your Name to the name fraction and use this...