Great Deal! Get Instant $10 FREE in Account on First Order + 10% Cashback on Every Order Order Now

Microsoft Word - EXTRA CREDITS ASSIGNMENT3.docx Extra credits -3 - BCS230, M. Alrajab 1 EXTRA CREDITS ASSIGNMENT Problem:...

1 answer below »
Microsoft Word - EXTRA CREDITS ASSIGNMENT3.docx
Extra    credits    -3        -    BCS230,    M.    Alrajab         1    
    
EXTRA    CREDITS    ASSIGNMENT    
    
    
Problem:        
A    rational    number    is    a    quotient    of    two    integers    such    as    4/7    and    5/9.    We    consider    a    
number    to    be    in    a    reduced    form    if    the    rational    number    denominator    and    numerator    have    
no    gcd    greater    than    1.    For    example    the    reduced    form    for    4/6    is    2/3.    
    
The    following    code    provides    a    skeleton    code    implementation    to    this    problem.    The    
Rational    class    has    a    constructor    Rational(int,    int)    that    takes    two    integers    and    stores    two    
values    in    reduced    form    in    co
esponding    private    numbers.    The    class    has    an    overloaded    
insertion    operator    
    that    is    used    for    output    of    objects    of    the    class.    
    
Your    need    to:    
- Read    and    understand    the    skeleton    code.    
- Modify    the    class    Rational    to    add    overloaded    operators    +,    -,    *,    /    to    be    used    for    
addition,    subtraction,    multiplication    and    division.        
- For    simplicity,    assume    the    numbers    and    the    arithmetic    operators    are    
separated    by    whitespaces    such    as                                                2        /        5            â€“            1            /            7    
    
    
Submission    
• Test    your    code    thoroughly    before    submission.    
• Submit    the    source    code    only    (not    project    submission).    
• Write    comments.    
• Should    be    completed    before    Dec    10th    2017    at    11:59    pm.    
• No    late    submission    is    accepted.        
• Check    ru
ic    
    
Extra    credits    -3        -    BCS230,    M.    Alrajab         2    
    
    
    
Rational Arithmetic I
#include
#include
using namespace std;




class Rational
{

Declaration of overloaded stream insertion operator
friend ostream & operator
(ostream &, Rational r);
private:
int numerator, denominator;
public:

Constructor builds a rational number n/d
Rational(int n, int d):numerator(n), denominator(d)
{
reduce();
}
private:

This member function transforms a rational number into

reduced form where the numerator and denominator have 1

as greatest common factor
void reduce();
};





************************************************************
This member function transforms a rational number into *
reduced form where the numerator and denominator have 1 *
as greatest common factor. *
************************************************************
void Rational::reduce()
{
bool negative = (numerator < 0) != (denominator < 0);
numerator = abs(numerator);
denominator = abs(denominator);

int factor = 2;
while (factor <= min(numerator, denominator))
{
if (numerator % factor == 0 && denominator % factor == 0)
{
XXXXXXXXXXnumerator = numerator / factor;
XXXXXXXXXXdenominator = denominator / factor;
XXXXXXXXXXcontinue;
}
factor ++;
}
if (negative)
numerator = - numerator;
}




************************************************
Overloaded stream insertion operator *
************************************************
ostream & operator
(ostream &out, Rational r)
{
out
r.numerator
"/"
r.denominator;
return out;
}

int main()
{
cout
Rational(6, -12);
return 0;
}
Answered Same Day May 07, 2020 BCS230

Solution

Tanya answered on May 08 2020
144 Votes
Assignment
Operator Overloading with Rational Numbers
Rational Arithmetic I
#include #include using namespace std;
class Rational
{

Declaration of overloaded stream insertion operato
friend ostream & operator
(ostream &, Rational r);
private:
int numerator, denominator;
public:

Constructor builds a rational number n/d
Rational(int n, int d):numerator(n), denominator(d)
{
        reduce();
}
void reduce();
void operator+(Rational);
void operator-(Rational);
void operator*(Rational);
void operato
(Rational);
};
************************************************************
This member function transforms a rational number into *
reduced form where the numerator and denominator have 1 *
as greatest common factor. *
************************************************************
void Rational::reduce()
{
    bool negative = (numerator < 0) != (denominator < 0);
    numerator = abs(numerator);
denominator = abs(denominator);
int factor = 2;
while (factor <= min(numerator, denominator))
{
        if (numerator % factor == 0 && denominator % factor == 0)
{
numerator = numerator / factor;
denominator = denominator / factor;
continue;
}
factor ++;
}
...
SOLUTION.PDF

Answer To This Question Is Available To Download

Related Questions & Answers

More Questions »

Submit New Assignment

Copy and Paste Your Assignment Here