Please help me with this code! Here is the assignment.
Project Specifications:
Each part of this assignment is a piece of the next part--it builds upon itself. You should submit each part as a separate program. But, the final program is a complete program built upon all the other parts. In other words--this is a step by step walk up to the final product. But, I want to see that you took each step and did not skip to the finish.
PART I: Develop a “Fraction Class” containing two public data fields for the numerator and denominator.
1. Inside the “main.cpp” declare an array of five fraction objects. Create a function in the “main.cpp” that instantiates a Fraction Object and calls “enterFractionValue( )” prompting the user to enter values for the numerator and denominator into each cell. Remember, 0 cannot be in the denominator. Two of the fractions must be in a “non-reduced” format. For example: 3/6 instead of ½.
2. Create a “displayFraction( )” in the “main.cpp” function permitting the user to see the results. To view the results, the user must be able to select from a menu the following displays of the fractions entered:
o Show each of the fractions in floating point format.
o Display each of the fractions and give the array position of the fraction with the highest value and the lowest value from the list shown.
o Display the fractions in sorted order from smallest to largest.
o Return to the menu so the user can repeat the operations.
Submit this portion to the Digital Drop Box.
________________________________________
PART II: Expand the Class and reduce the size of “main.cpp”.
1. Using the same “Fraction Class”, change the two public data fields from “public” to “private”.Add two more private fields: (a) “double” to hold the fractions decimal value. (b) a “static” to hold the slash all fractions use when displaying their values.
2. In the public portion of the Class, prototype the “enterFractionValue( )” function.
3. After the values are entered, use a “private” function in the Class to “calculateDecimalValue( )” upon entry.In the public portion of the class, prototype the “displayFraction( )” function. Change the function so the fractions are displayed properly with a slash between them using the static “slash” data type included in this class.
4. Add two constructors to the class.
o The first accepts two integers for numerator and denominator.
If a single integer is passed to the constructor, use it as the numerator and default the denominator to “1”. If no integers are passed than default the numerator to a “0” and the denominator to “1”. Anytime a “0” is attempted for the denominator, force the numerator to “0” and the denominator to “1”.
Calculate the greatest common denominator using two functions in the “Fraction” class.
The first function called “greatestCommonDenominator( )” finds the largest number that can divide evenly into both the numerator and denominator.
o The second function called “reduceFraction( )” reduces a fraction to its proper format. By using the greatest common denominator the fraction can be reduced by dividing both the numerator and denominator by that number.
5. Change the “main.cpp” to declare a “Fraction” object and confirm the class works correctly.
Submit this portion to the Digital Drop Box.
________________________________________
PART III: Add some functions that are “friends” to the Fractions Class.
1. The first “friend function” takes two “Fraction” arguments and sums them together creating a third fraction. Make sure the sum fraction is in the proper format using the “static” slash from the “Fraction” Class and the fraction is properly reduced. For example: ¼ + ¼ = ½ not 2/4.
2. The second “friend function” takes two “Fraction” arguments and compares them, returning a 1 if the fractions are equal and a 0 if they are not. Fractions are equal when their reduced values are equal. For example ½ and 3/6 should be considered equal.
3. Change the menu so the user can select to see each of the objects in the array in reduced format. When done the user must be able to return to the main menu.
Submit this portion to the Digital Drop Box.
________________________________________
The entire final program is completed (both Installments 1 and 2) the program will be a main.cpp that uses Fraction Class, MathProblem Class, Container Class, 2 friend functions, 3 function templates, overriden operators, and numerous other supporting functions.
Two things you might want to consider in the final form: Did I maximize my use of STLs? Can I make better use of Preprocessor Directives?
________________________________________
I have been struggling with this for weeks and I just can't get it right. If someone could help me to correct my code, I would be so greatful!
class Fraction
{
// friend list
friend int addFractions(); // adds two fractions together
friend bool compareFractions();//compares two fractions
private:
int num, denom; // variable declarations
float deciVal;
const static char slash = '/';
void calculateDecimalValue(); // private member function
public:
int fracArray[2][5];
Fraction();
Fraction(int, int); //
int getNum(); //
int getDenom();//accessors to class' private member variables
void enterFractionValue();
void displayFractionValue();
int greatestCommonDenominator();
void reduceFraction();
};
#include "fraction.h"
// member function definitions
// constructor
Fraction::Fraction(int, int)
{
int num;
int denom;
enterFractionValue();/// gets use to input fraction
if(denom == 0) // checks to see if denom = 0
{
num = 0;
denom = 1; // forces a 1 to the denominator
}
if (num == " " && denom != " " || denom == " " && num != " ")// is only one number input
{
denom = 1;
}
else if(num == " " && num == " ")
{
num = 0;
denom = 1;
}
}
// num member variable accessor
Fraction::getNum()
{
return num;
}
// denom member variable accessor
Fraction::getDenom()
{
return denom;
}
// method that gets user input fractions
void Fraction::enterFractionValue(int, int)
{
cout << "You are going to be asked for five fractions. \n";
cout << "Two of them need to be non-reduced fractions, i.e. 2/4 not 1/2. \n";
cout << "Enter a number for the numerator. \n";
cin >> num;
cout << "Enter a number for the denominator. \n";
cin >> denom;
while (denom == 0)
{
cout << "The denominator cannot be 0. Enter another number for the denominator. \n";
cin >> denom;
}
cout << num << slash << denom;
}
void Fraction::calculateDecimalValue(float deciVal)
{
deciVal = num / denom;
cout << "fraction in decimal form: " << deciVal << endl;
}
void Fraction::displayFractionValue(int, int, int, int)
{
int i, input, highest, lowest;
cout << "*************************************MENU************************************ \n";
cout << "Enter 1 to display all fractions in decimal form. \n";
cout << "Enter 2 to display all fractions + array position of highest and lowest value. \n";
cout << "Enter 3 to display the fractions sorted from lowest to highest. \n";
cout << "Enter 4 to display the fraction in reduced form. \n";
cin >> input;
while(input != 1 && input !=2 && input != 3 && input != 4)
{
cout << "That is not a valid option. Please choose 1, 2, 3, or 4. \n";
cin >> input;
}
switch(input)
{
case 1:
for (i=0;i<ARRAYSIZE;i++)
{
calculateDecimalValue();
}
case 2:
for (i=0;i<ARRAYSIZE;i++)
{
cout << fractionObject;
}
highest = fracArray[0];
for(i=0;i<ARRAYSIZE;i++)
{
if(fracArray[i+1]>fracArray[i])
{
highest=fracArray[i+1];
}
else
{
highest=fracArray[i];
}
}
cout << "The highest value in the array is "<<highest << endl;
lowest=fracArray[0];
for(i=0;i<ARRAYSIZE;i++)
{
if(fracArray[i+1]<fracArray[i])
{
lowest=fracArray[i+1];
}
else
{
lowest=fracArray[i];
}
}
cout << "The lowest value in the array is "<<lowest << endl;
case 3:
sort(fracArray, i+5);
for(i=0;i<ARRAYSIZE;i++)
{
cout << fracArray[i] << " ";
}
case 4:
for(i=0;i<ARRAYSIZE<i++)
{
reduceFraction();
}
}
}
Fraction::greatestCommonDenominator(int)
{
int temp, num, denom;
num = getNum;
denom = getDenom;
while (denom != 0)
{
temp=denom;
denom=num%denom;
num=temp;
}
return num;
}
Fraction::reduceFraction(int, int, int, int, int)
{
int num, denom;
num = getNum();
denom = getDenom;
int remainder;
int dividend;
int divisor;
dividend = abs(num);
divisor = abs(denom);
while(true)
{
remainder = dividend % divisor;
if (remainder == 0)
break;
else
{
dividend = divisor;
divisor = remainder;
}
}
num = num/divisor;
denom = denom/ divisor;
}
int addFractions (int one, int two)
bool result;
fracArray[2] = fractionThree;
fractionThree.getNum();
fractionThree.getDenom();
fracArray[3] = fractionFour;
fractionFour.getNum();
fractionFour.getDenom();
if (fractionThree.num == fractionFour.num && fractionThree.denom == fractionFour.denom)
{
return true;
}
else
{
return false;
}
}
#include <iostream>
#include <algorithm>
#include "Fraction.h"
using namespace std;
int main()
{
int num, denom;
const int ARRAYROWS = 2;
const int ARRAYCOLS = 5;
int i, fracArray[ARRAYROWS][ARRAYCOLS]; // creates and array of five objects called fracArray
;
Fraction fractionOne;
fractionOne.getNum();
fractionOne.getDenom();
fractionOne.num = fracArray[0][0];
fractionOne.denom = fracArray[1][0];
Fraction fractionTwo;
fractionTwo.num = fracArray[0][1];
fractionTwo.denom = fracArray[1][1];
Fraction fractionThree;
fractionThree.num = fracArray[0][2];
fractionThree.denom = fracArray[1][2];
Fraction fractionFour;
fractionFour.num = fracArray[0][3];
fractionFour.denom = fracArray[1][3];
Fraction fractionFive;
fractionFive.num = fracArray [0][4];
fractionFive.denom = fracArray [1][4];
fractionOne.enterFractionValue(num, denom);
fractionOne.reduceFraction();
fractionOne.calculateDecimalValue();
fractionOne.greatestCommonDenominator();
fractionTwo.enterFractionValue(num, denom);
fractionTwo.reduceFraction();
fractionTwo.calculateDecimalValue();
fractionTwo.greatestCommonDenominator();
fractionThree.enterFractionValue(num, denom);
fractionThree.reduceFraction();
fractionThree.calculateDecimalValue();
fractionThree.greatestCommonDenominator();
fractionFour.enterFractionValue(num, denom);
fractionFour.reduceFraction();
fractionFour.calculateDecimalValue();
fractionFour.greatestCommonDenominator();
fractionFive.enterFractionValue(num, denom);
fractionFive.reduceFraction();
fractionFive.calculateDecimalValue();
fractionFive.greatestCommonDenominator();
for (i=0, i<ARRAYROWS; i++;)
{
for (j=0; j<ARRAYCOLS; i++)
{
displayFractionValue();
}
}
for (i=0, i<2; i++;)
{
for (j=0; j<2; i++)
{
addFractions();
}
}
for (i=0, i<2; i++;)
{
for (j=3; j<5; i++)
{
compareFractions();
}
}
}
return 0;
}