Must be done in C++. The following can use strings, loops, arrays, and functions
ID: 668910 • Letter: M
Question
Must be done in C++. The following can use strings, loops, arrays, and functions. The code must be able to accept and perform operations on the whole number portion of a mixed number (like the '2' in 2 1/4):
You are developing a Fraction structure for Teacher’s Pet Software. The structure contains
three public data fields for whole number, numerator, and denominator. Using the same
structure, write the functions described below:
» An enterFractionValue()function that declares a local Fraction object and
prompts the user to enter values for the Fraction. Do not allow the user to enter a
value of 0 for the denominator of any Fraction; continue to prompt the user for a
denominator value until a valid one is entered. The function returns a data-filled
Fraction object to the calling function.
» A reduceFraction()function that accepts a Fraction object and reduces it to proper
form, returning the reduced Fraction. For example, a Fraction entering the function as 0
2/6 would be reduced to 0 1/3, and a Fraction entered as 4 18/4 would be reduced to 8 1/2.
» A displayFraction()function that displays any Fraction object passed to it. This
function displays a fraction with a slash between the numerator and denominator.
» A main()function that declares a Fraction object and continues to get Fraction values
from the user until the user enters a Fraction with value 0 (both the whole number and
the numerator are 0). For each Fraction entered, display the Fraction, reduce the
Fraction, and display the Fraction again.
Explanation / Answer
Basic Algo for reduction :
1. if numerator > denominator
convert number into numerator/denominator form by using following formula
num = quotient * divisor + remainder
and then convert it back to previous form
this will reduce examples like 3 3/2 to 4 1/2
2. for examples like 2/6 to 1/3
find HCF of both numerator and denominator.
divide both numerator and denominator by hcf
Below is your code :
Most of the part is self Explanatory but I've inserted comments to explain typical parts.
#include <iostream>
using namespace std;
void enterFractionValue(int *whole, int *numerator, int *denominator);
void reduceFraction(int *whole, int *numerator, int *denominator);
void displayFraction(int whole, int numerator, int denominator);
int main()
{
cout << "Hello world!" << endl;
int whole = 0;
int numerator = 0;
int denominator = 0;
do{
enterFractionValue(&whole, &numerator, &denominator);
reduceFraction(&whole, &numerator, &denominator);
displayFraction(whole, numerator, denominator);
denominator = 0; // Initialising denominator as zero for next number
}while ((whole != 0) || (numerator != 0));
return 0;
}
void enterFractionValue(int *whole, int *numerator, int *denominator)
{
cout << "Enter a whole number part : ";
cin >> *whole;
cout << endl;
cout << "Enter numerator of fraction : ";
cin >> *numerator;
cout << endl;
while (*denominator <= 0)
{
cout << "Enter denominator of fraction : ";
cin >> *denominator;
cout << endl;
}
}
void reduceFraction(int *whole, int *numerator, int *denominator)
{
int num = 0;
int remainder = 0;
int quotient = 0;
int i,hcf = 1;
num = ((*whole)*(*denominator) + (*numerator));
/* Reducing numbers where numerator is greater than denominator like 6/4 etc */
quotient = num / (*denominator);
remainder = num - (quotient * (*denominator));
(*whole) = quotient;
(*numerator) = remainder;
/* End of Reduction */
/* Calculating Highest Common Factor to reduce fraction */
for(i=1; i<=(*numerator) || i<=(*denominator); ++i)
{
if((*numerator)%i==0 && (*denominator)%i==0) /* Checking whether i is a factor of both number */
hcf=i;
}
if (hcf > 1)
{
(*numerator) = (*numerator)/hcf;
(*denominator) = (*denominator)/hcf;
}
}
void displayFraction(int whole, int numerator, int denominator)
{
cout << whole;
cout << " ";
if (numerator != 0)
{
cout << numerator;
cout << "/";
cout << denominator;
}
cout << endl;
cout << endl;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.