Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Due Date Friday, July 6, 201811:59 PM Points Possible 55.55 Exercise 1: (Fractio

ID: 3891262 • Letter: D

Question

Due Date

Friday, July 6, 201811:59 PM

Points Possible

55.55

Exercise 1:

(Fraction calculator) Write a program that lets the user perform arithmetic
operations on fractions. Fractions are of the form a/b, in which a and b are
integers and b != 0. Your program must be menu driven, allowing the user to
select the operation (+, -, *, or /) and input the numerator and denominator
of each fraction. Furthermore, your program must consist of at least the
following functions:

a. Function menu: This function informs the user about the program's purpose, explains how to enter data, and allows the user to select the
operation, returning that operation.

b. Function addFractions: This function takes as input four integers representing the numerators and denominators of two fractions, adds the fractions, and returns the numerator and denominator of the result. (Notice that this function has a total of six parameters.)

c. Function subtractFractions: This function takes as input four integers representing the numerators and denominators of two fractions, subtracts the fractions, and returns the numerator and denominator of the result. (Notice that this function has a total of six parameters.)

d. Function multiplyFractions: This function takes as input four integers representing the numerators and denominators of two fractions, multiplies the fractions, and returns the numerators and denominators of the result. (Notice that this function has a total of six parameters.)

e. Function divideFractions: This function takes as input four integers representing the numerators and denominators of two fractions, divides the fractions, and returns the numerator and denominator of the result. (Notice that this function has a total of six parameters.)

Some sample outputs are:

3 / 4 + 2 / 5 = 23 / 20
2 / 3 * 3 / 5 = 6 / 15

Your answer need not be in the lowest terms.
-           Noncompiling (-25)
-           No documentation (-5)
-            The following functions are required at a minimum (menu, add, subtract, multiply, and divide) (-3 each one missing)
·      Menu – should not take any parameters and simply return the user’s choice such as add, subtract, multiply, divide, etc (-2 if you have parameters) (-2 if it does something other than displaying menu and getting the user’s entry/choice… (-2).
-           Add, subtract, multiply, divide – take 6 parameters -> 2 numerators and 2 denominators for fractions as input and 1 denominator and 1 numerator for the output. (-5 for parameter and function return type setup). (-4 each function that does not calculate correctly).

Explanation / Answer

#include
using namespace std;

struct fraction {
int num;
int denom;
};

enum choices { add = 'a', subtract = 's', multiply = 'm', divide = 'd', quit = 'q'};

void displayMenu(){
//displays the menu for the user.
cout << "a) Addition:" << endl;
cout << "s) Subtraction:" << endl;
cout << "m) Multiplication:" << endl;
cout << "d) Division:" << endl;
cout << "q) Quit:" << endl;
}


void addFractions(const fraction &f1, fraction &f2, fraction &r) {
//Takes fraction one and two and adds both
//Has 3 Parameters f1 which is fraction 1 and f2 which is fraction 2, r is the awnser
//Returns nothing
r.denom = f1.denom * f2.denom;
r.num = (f1.num * f2.denom) + (f2.num * f1.denom);
}

void subtractFractions(const fraction &f1, fraction &f2, fraction &r) {
//Takes fraction one and two and subtracts to provide an awnser
//Has 3 Parameters f1 which is fraction 1 and f2 which is fraction 2, r is the awnser
//Returns nothing
r.denom = f1.denom * f2.denom;
r.num = (f1.num * f2.denom) - (f2.num * f1.denom);
}

void multiplyFractions(const fraction &f1, fraction &f2, fraction &r) {
//Takes 2 fractions and mutltiplies
//Has 3 Parameters f1 which is fraction 1 and f2 which is fraction 2, r is the awnser
//returns nothing
r.denom = f1.denom * f2.denom;
r.num = f1.num * f2.num;
}

void divideFractions(const fraction &f1, fraction &f2, fraction &r) {
//Takes two fractions, reciprocates one and multiples.
//Has 3 Parameters f1 which is fraction 1 and f2 which is fraction 2, r is the awnser
//returns nothing  
  
r.denom = f1.denom * f2.num;
r.num = f1.num * f2.denom;
}

int gcf(fraction r) {
// Finds and returns the Greatest Common Factor between a & b.
// Parameters: a and b are integers.
// returns an integer which is the GCF of a and b.
int i;
if(r.num == r.denom){
i = r.num;
}
else if (r.num > r.denom) {
for(i = r.denom ; i > 0; i--) {
if(((r.num % i) == 0) && ((r.denom % i) == 0))
{
break;
}
}
}

else {
for(i = r.num; i > 0; i--) {
if (((r.num % i) == 0) && ((r.denom % i) == 0)) {
break;
}
}
}
return(i);
}

void reduce(fraction &r) {
// Reduces the fraction n/d to lowest terms.
// Parameters: n and d are PBR.
// returns nothing, but n & d will be reduced to lowest terms.


int i = gcf(r);
r.num = r.num /i;
r.denom = r.denom/i;
cout << "GCF: " << i << endl;
cout << "Simplified Answer = " << r.num << "/" << r.denom << endl << endl;
}

void getFraction(fraction &f) {
//Gets fraction form the user
//returns nothing
cout << "Numerator: ";
cin >> f.num;
cout << "Denominator: ";
cin >> f.denom;
cout << endl;
}

void showFraction(fraction &r) {
cout << "Answer = " << r.num << "/" << r.denom << endl;
}

int main() {
fraction f1, f2, ans;
char input;

do {
displayMenu();

cin >> input;

switch (input) {
case (add): {
getFraction(f1);
getFraction(f2);
addFractions(f1, f2, ans);
showFraction(ans);
reduce(ans);
break;
}
case (subtract): {
getFraction(f1);
getFraction(f2);
subtractFractions(f1, f2, ans);
showFraction(ans);
reduce(ans);
break;
}
case (multiply): {
getFraction(f1);
getFraction(f2);
multiplyFractions(f1, f2, ans);
showFraction(ans);
reduce(ans);
break;
}
case (divide): {
getFraction(f1);
getFraction(f2);
divideFractions(f1, f2, ans);
showFraction(ans);
reduce(ans);
break;
}
}
}while (input != 'q');

cout << "You have quit the Main Menu.";
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote