I have written a summation program that involves fractions and my limitations are to use the Rational.hpp file. I have written the code so that it gives the out put of the sum of 1/2 + 2/3 + 3/4 … 98/99 + 99/100, but the program returns a negative value. Is it possible that the variable gets overloaded and corrupted? if so, how would I work around the issue? Any help is greatly appreciated!
Edit: In response to the comments, I apologize if my question was phrased poorly and to clarify the only code I am responsible for writing is the Summation.cpp as the Rational.h is something provided by the education institution that I am currently trying to learn C++ from. Again I apologize for any misunderstanding I am a student doing my best to learn C++ and first time using stack overflow. Thank you patience.
//Summation.cpp
#include <iostream>
#include "Rational.hpp"
using namespace std;
int main()
{
Rational sum(0, 1);
for( int i = 1 ; i < 99; i++){
cout << sum.doubleValue() << endl;
Rational interval(i, i + 1);
sum = sum.add(interval);
}
cout << "The sum of 1/2 through 99/100 is: " << sum.doubleValue() << endl;
return 0;
}
//Rational.hpp
#include <cstdlib> // For the abs function
#include <string>
using namespace std;
class Rational
{
public:
Rational();
Rational(int numerator, int denominator);
int getNumerator() const;
int getDenominator() const;
Rational add(const Rational& secondRational) const;
Rational subtract(const Rational& secondRational) const;
Rational multiply(const Rational& secondRational) const;
Rational divide(const Rational& secondRational) const;
int compareTo(const Rational& secondRational) const;
bool equals(const Rational& secondRational) const;
int intValue() const;
double doubleValue() const;
string toString() const;
private:
int numerator;
int denominator;
static int gcd(int n, int d);
};
Rational::Rational()
{
numerator = 0;
denominator = 1;
}
Rational::Rational(int numerator, int denominator)
{
int factor = gcd(numerator, denominator);
this->numerator = ((denominator > 0) ? 1 : -1) * numerator / factor;
this->denominator = abs(denominator) / factor;
}
int Rational::getNumerator() const
{
return numerator;
}
int Rational::getDenominator() const
{
return denominator;
}
// Find GCD of two numbers
int Rational::gcd(int n, int d)
{
int n1 = abs(n);
int n2 = abs(d);
int gcd = 1;
for (int k = 1; k <= n1 && k <= n2; k++)
{
if (n1 % k == 0 && n2 % k == 0)
gcd = k;
}
return gcd;
}
Rational Rational::add(const Rational& secondRational) const
{
int n = numerator * secondRational.getDenominator() +
denominator * secondRational.getNumerator();
int d = denominator * secondRational.getDenominator();
return Rational(n, d);
}
Rational Rational::subtract(const Rational& secondRational) const
{
int n = numerator * secondRational.getDenominator()
- denominator * secondRational.getNumerator();
int d = denominator * secondRational.getDenominator();
return Rational(n, d);
}
Rational Rational::multiply(const Rational& secondRational) const
{
int n = numerator * secondRational.getNumerator();
int d = denominator * secondRational.getDenominator();
return Rational(n, d);
}
Rational Rational::divide(const Rational& secondRational) const
{
int n = numerator * secondRational.getDenominator();
int d = denominator * secondRational.numerator;
return Rational(n, d);
}
int Rational::compareTo(const Rational& secondRational) const
{
Rational temp = subtract(secondRational);
if (temp.getNumerator() < 0)
return -1;
else if (temp.getNumerator() == 0)
return 0;
else
return 1;
}
bool Rational::equals(const Rational& secondRational) const
{
if (compareTo(secondRational) == 0)
return true;
else
return false;
}
int Rational::intValue() const
{
return getNumerator() / getDenominator();
}
double Rational::doubleValue() const
{
return 1.0 * getNumerator() / getDenominator();
}
string Rational::toString() const
{
if (denominator == 1)
return to_string(numerator); // See Ch7 for to_string
else
return to_string(numerator) + "/" + to_string(denominator);
}
MexicanoScante is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
9