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

Code should be done in C++ and be able to compile/run in Visual Studio 2012 or l

ID: 670415 • Letter: C

Question

Code should be done in C++ and be able to compile/run in Visual Studio 2012 or later. The code cannot use #include statements ending in .h (such as stdlib.h, conio.h, etc.). DO NOT ANSWER UNLESS YOU CAN COMPLETE ALL PARTS OF THE PROBLEM! Thank you in advance for your help!

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:

(1) 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.

(2) 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.

(3) A displayFraction()function that displays any Fraction object passed to it. This function displays a fraction with a slash ("/") between the numerator and denominator.

(4) 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

#include "stdafx.h"
#include<iostream>
#include <cstdlib>
#include<string>
using namespace std;
// Class Declaration
class fraction
{
//Access - Specifier
public:
   int number,denominator,numerator;
   fraction enterFraction(){
   fraction f;
   cout<<"Enter the Number :";
cin>> f.number;

cout<<"Enter the Numerator :";
cin>>f.numerator;
   do{
   cout<<"Enter the Denominator not equal to zero:";
cin>>f.denominator;
   }while(f.denominator==0);

   return f;
  
   }
   fraction reduceFraction(fraction f){
       int n;
       n=f.number*f.denominator+f.numerator;
       cout<<n<<endl;
       int i=0,factor=1;
       int min=f.denominator;
       if(min>n){
           min=n;
       }
       for(i=1;i<=min;i++){
           if(f.denominator%i==0&&n%i==0){
           factor=i;
           }
       }
       n=n/factor;
       f.denominator=f.denominator/factor;
       f.number=n/f.denominator;
       f.numerator=n%f.denominator;
       return f;
   }
   void display(fraction f){
       cout<<f.number<<" "<<f.numerator<<"/"<<f.denominator<<endl;
   }
};

int _tmain(int argc, _TCHAR* argv[])
{
  
   fraction t;
   t=t.enterFraction();
   t.display(t);
   t=t.reduceFraction(t);
   t.display(t);
  

  

cout << " ";
   system("pause");
   return 0;
}