There are many types of number that C/C++ does not have, but which we can implem
ID: 3568564 • Letter: T
Question
There are many types of number that C/C++ does not have, but which we can implement with a a little work. One such type of number is called "Rational". Rational numbers are expressed as a ratio of integers (any number that cannot be expressed as a ratio of integers is called irrational). For example, 2/3 is a rational number; 3/1 is a rational number; so is 22/7; but pi (the circle constant) cannot be expressed as a rational number (one can approximate it, though). For our purposes, numbers don't have to be in "lowest terms", so 4/8 is a perfectly good rational number, even though it can be expressed also as 1/2.
We will use records of Rational type declared as follows:
struct Rational {
int num;
int denom;
}
A record of Rational type stores a rational number's numerator and denominator. A negative number is represented by a negative numerator and a positive denominator. The following tasks ask you to implement functions for carrying out operations on rational numbers or on arrays of rational numbers.
Notes: Functions 5 through 10 should never directly access the num and denom fields of a Rational record. In other words, they should use the numerator and denominator functions to access the numerator and denominator of a Rational, and they should use the function makeRational to create any new Rational numbers.
Implement the following functions:
Explanation / Answer
//Note: Use numerator() and denominator() to access the fields of r1 and r2; then use makeRational() to create your new record.
//Rational divide(Rational r1, Rational r2)
//Returns a new Rational number containing the result of dividing r1 by r2. You must not change r1 and r2. Remember: 2/3 divided by 2/5 is 5/3. [Hint: (x1/y1) / (x2/y2) = (x1/y1) * (y2/x2)]
#include<iostream>
using namespace std;
struct Rational {
int numerator;
int denominator;
};
bool lessThan(Rational r1, Rational r2)
{
int a = r1.numerator * r2.denominator;
int b = r2.numerator * r1.denominator;
if( a < b)
return true;
else
return false;
}
bool equal(Rational r1, Rational r2)
{
int a = r1.numerator * r2.denominator;
int b = r2.numerator * r1.denominator;
if( a == b)
return true;
else
return false;
}
struct Rational makeRational(int n,int d)
{
Rational c;
c.numerator = n;
c.denominator = d;
return c;
}
int denominator(Rational r)
{
return r.denominator;
}
int numerator(Rational r)
{
return r.numerator;
}
void display(Rational r)
{
cout<<numerator(r)<<"/"<<denominator(r)<<endl;
}
struct Rational divide( struct Rational a, struct Rational b)
{
int n = numerator(a) * denominator(b);
int d = numerator(b) * denominator(a);
return makeRational(n,d);
}
struct Rational sum( struct Rational a, struct Rational b) {
int num = numerator(a) * denominator(b) + numerator(b) * denominator(a);
int den = denominator(a) * denominator(b);
return makeRational(num,den);
}
struct Rational subtract( struct Rational a, struct Rational b) {
int num = numerator(a) * denominator(b) - numerator(b) * denominator(a);
int den = denominator(a) * denominator(b);
return makeRational(num,den);
}
struct Rational multi( struct Rational a, struct Rational b) {
int num = numerator(a) * numerator(b);
int den = denominator(b) * denominator(b);
return makeRational(num,den);
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.