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

9. ( numerical) a fraction- handing program contins this menu; A. add two fracti

ID: 662245 • Letter: 9

Question

9. ( numerical) a fraction- handing program contins this menu;
A. add two fractions.
B.convert a fraction to decimaal.
C. mutpliy two fractions.
Q.Quit
a. write C++ code for the program with sturb functions for choices.
b. Insert the fractionTodecimal()function from exercise 2 into the code with comands
to pass and display the parameters.
c. complete the program by replacing the stub functions with function that perfom appropiate
operations.

note.
exercise 2. a function is defined by the following code;
double fractionTodecimal ( doble numerator, double denominator)
{
return ( numerator/ denominator);
}

Explanation / Answer

C++ Program :

#include <iostream>
using namespace std;

double FractionToDecimal (double numerator, double denominator) {
return denominator == 0 ? 0 : numerator / denominator; //it's bad to divide by zero
}

double Add() {
double num1;
double num2;
double den1;
double den2;

cout << "Enter 1st fraction num: ";
cin >> num1;
cout << "den: ";
cin >> den1;
cout << "Enter 2st fraction num: ";
cin >> num2;
cout << "den: ";
cin >> den2;
return (num1*den2 + num2*den1) / (den1*den2);
}

double Multiply() {
double num1;
double num2;
double den1;
double den2;
//stub func
//do input somewhere for fractions
return (num1*num2) / (den1*den2);
}

using namespace std;

int main() {
& cout << "A. Add two fractions "
"B. Convert a fraction to decimal "
"C. Multiply two fractions "
"Q. Quit ";
& bool ok_choice = false;
& while (ok_choice != true) {
char choice;
cout << "Please enter your choice: ";
cin >> choice;
//in OK cases you can insert functions that you need
switch (choice) {
case 'A':
cout << "Add : " << Add() << endl;
ok_choice = true;
break;

case 'B':
cout << "ToDecimal: " << FractionToDecimal(1.24, 2.33) << endl; //stub
ok_choice = true;
break;
case 'C':
Multiply();
ok_choice = true;
break;
case 'Q':
cout << "Quit ";
ok_choice = true;
break;
default:
cout << "Bad choice! Try another one ";
break;
}
& }
& return 0;
}