Object-Oriented Programming Using C++ 4th Edition, Author: Farrell; Gilbertson,
ID: 3583356 • Letter: O
Question
Object-Oriented Programming Using C++ 4th Edition, Author: Farrell; Gilbertson, Claudia Bienias
I am looking to see if there is an step by step solution for Chapter 7 and 9, Case Project 1, if so where might I find them cause so far I've benn unable to locate them.
Thank you for any help with this.
Object-Oriented Programming Using C++ (4th Edition)
Chapter 7, Case Project 1, Page 330
In previous chapters, you have been developing a Fraction structure for Teacher’s Pet Software. Now you will develop a class that contains the fields and functions that a Fraction needs. Create a Fraction class with three private data fields for whole number, numerator, and denominator. Also create a constant static public field to hold the symbol that separates a numerator and denominator when a Fraction is displayed—the slash. Create three public member functions for the class, as follows:
» An enterFractionValue()function that 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.
» A reduceFraction()function that reduces a Fraction to proper form. For example, a Fraction with the value 0 2/6 would be reduced to 0 1/3 and a Fraction with the value 4 18/4 would be reduced to 8 1/2.
» A displayFraction()function that displays the Fraction whole number, numerator, slash, and denominator.
Add any other functions to the Fraction class that will be useful to you. Create 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 numerator
are 0). For each Fraction entered, display the Fraction, reduce the Fraction, and display the Fraction again.
Explanation / Answer
#include<stdio.h>
void main()
{
private int wholenumber,numerator,denominator;
public const char separator='/';
cout<<"enter the wholenumber,numerator,denominator";
cin>>wholenumber>>numerator>>denominator;
if(denominator==0)//check if denominator is 0 if its ask the user to reenter the value
{
cout>>"reenter the denominator";
}
else
{
cout<<"the original fraction is " wholenumber numeratorseparatordenominator;
reduceFraction(wholenumber,numerator,denominator);//call this method
}
}
void reduceFraction(int w,int n,int d)
{
int reducedfraction1,reducedfraction2,newnumerator,newdenominator,int fraction;
fraction=((d*w)+n)/d;//suppose the fraction to be reduced is 2 1/3 then convert it into proper fraction first(((2*3)+1)/3)=7/3
newnumerator=(d*w)+n; //take the numerator of the new fraction (7)
newdenominator=d;//take the denominator of the new fraction(3)
reducedfraction1=newnumerator/gcd(newnumerator,newdenominator); //compute the gcd of the new numerator and denominator and divide newnumerator by the gcd(18/2=9)
reducedfraction2=newdenominator/gcd(newnumerator,newdenominator);//similar as above but here numerator is newdenomitor variablevalue(4/2=2) hence the new fraction will be 9/2 we need to convert it to mixed number
//converting new reduced fraction to mixed number as mentioned in the question
int remainder=reducedfraction1%reducedfraction2; //calculate remainder of the new fraction
int quotient=reducedfraction1/reducedfraction2; //calculate quotient of new fraction
displayFraction(quotient,remainder,reducedfraction2); //call this method to display the mixed number in the console.
}
displayFraction(int quotient,int remainder,int reducedfraction2)
{
cout<<"proper form is " quotient remainder separator reducedfraction2; //displays the output..separator=/
}
//get gcd of the two numbers(numerator,denominator) and return it
int gcd(int n,int d) //Ex:i will take 18 annd 4
{
int gcd,remainder;
while(n!=0)
{
remainder=d%n; //remainder=2
d=n; //d=18
n=remainder; //n=2 continue until n becomes 0 ,
}
gcd=d; //d will be 2
return gcd; return 2
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.