Question: C++ Answer these questions and continue the code given below the questions: Write a function named addfractions that takes two fractions passed to it as
C++
Answer these questions and continue the code given below the questions:
Write a function named "addfractions" that takes two fractions passed to it as reference parameters and returns a fraction, using reference parameters, which is the sum of the two input fractions. Then call reduce1 and reduce2 functions to reduce the output fraction. Call it four times with different argument values to print the results as follows: (Your program will be tested by the TA for fractions with different numbers)
Sum of frac1 and frac2 is frac5
Frac5 reduced by reduce1 is frac5a
Frac5 reduced by reduce2 is x and frac5b
Sum of frac1 and frac3 is frac6
Frac6 reduced by reduce1 is frac6a
Frac6 reduced by reduce2 is x and frac6b
Sum of frac2 andfrac3 is frac7
Frac7 reduced by reduce1 is frac7a
Frac7 reduced by reduce2 is x and frac7b
Sum of frac2 and frac4 is frac8
Frac8 reduced by reduce1 is frac8a
Frac8 reduced by reduce2 is x and frac8b
5.Write a function named "IsGreater" which takes as input two fractions as parameters passed by value and returns a Boolean value. The returned value is 'True" is the first fraction is strictly larger than the second fraction and returns the value "false" otherwise. Call this function repeatedly to print theresults of the following comparisons:
Frac1 is (not) greater than Frac2
Frac2 is (not) greater than Frac3
Frac2 is (not) greater than Frac4
Frac3 is (not) greater than Frac4
6.Write a function named "FracPower" that takes a fraction passed by reference anda positive integer exponent passed by value, and returns a fraction by reference that is the result of raising the input power to the exponent. From within this function it calls reduce1 to reduce the resulting fraction before it is returned. Call this program four times to print output that looks like the following:
Frac1 raised to power n is frac9
Frac2 raised to power m is frac10
Frac3 raised to power k is frac11
Frac4 raised to power l is frac12
C++ code
#include
using namespace std;
int read_numerator() {
int n;
cout << "Enter numerator: ";
cin >> n;
while (n < 0) {
cout << "Numerator can't be negative. Enter numerator: ";
cin >> n;
}
return n;
}
int read_denominator() {
int n;
cout << "Enter denominator: ";
cin >> n;
while (n <= 0) {
if (n < 0) {
cout << "Denominator can't be negative. Enter denominator: ";
} else {
cout << "Denominator can't be zero. Enter denominator: ";
}
cin >> n;
}
return n;
}
void Input_Fractions(int &n1, int &d1, int &n2, int &d2, int &n3, int &d3, int &n4, int &d4) {
cout << "Enter fraction 1" << endl;
n1 = read_numerator();
d1 = read_denominator();
cout << "Enter fraction 2" << endl;
n2 = read_numerator();
d2 = read_denominator();
cout << "Enter fraction 3" << endl;
n3 = read_numerator();
d3 = read_denominator();
cout << "Enter fraction 4" << endl;
n4 = read_numerator();
d4 = read_denominator();
}
void reduce1(int n1, int d1, int &n, int &d) {
int gcd = 1;
for (int i = 1; i <= n1; ++i) {
if (n1 % i == 0 && d1 % i == 0) {
gcd = i;
}
}
n = n1 / gcd;
d = d1 / gcd;
}
void reduce2(int n1, int d1, int &value, int &n, int &d) {
reduce1(n1, d1, n, d);
value = n / d;
n = n % d;
}
int main() {
int n1, n2, n3, n4, d1, d2, d3, d4, n, d, val;
Input_Fractions(n1, d1, n2, d2, n3, d3, n4, d4);
cout << endl;
reduce1(n1, d1, n, d);
cout << n1 << "/" << d1 << " is reduced to " << n << "/" << d << endl;
reduce1(n2, d2, n, d);
cout << n2 << "/" << d2 << " is reduced to " << n << "/" << d << endl;
reduce1(n3, d3, n, d);
cout << n3 << "/" << d3 << " is reduced to " << n << "/" << d << endl;
reduce1(n4, d4, n, d);
cout << n4 << "/" << d4 << " is reduced to " << n << "/" << d << endl;
cout << endl;
reduce2(n1, d1, val, n, d);
cout << n1 << "/" << d1 << " is reduced to " << val << " and " << n << "/" << d << endl;
reduce2(n2, d2, val, n, d);
cout << n2 << "/" << d2 << " is reduced to " << val << " and " << n << "/" << d << endl;
reduce2(n3, d3, val, n, d);
cout << n3 << "/" << d3 << " is reduced to " << val << " and " << n << "/" << d << endl;
reduce2(n4, d4, val, n, d);
cout << n4 << "/" << d4 << " is reduced to " << val << " and " << n << "/" << d << endl;
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
