# Fraction Problem C++

Page 1 of 1

## 1 Replies - 2291 Views - Last Post: 22 October 2008 - 10:24 PM

### #1 peoplefinder

Reputation: 0
• Posts: 2
• Joined: 22-October 08

# Fraction Problem C++

Posted 22 October 2008 - 10:09 PM

fraction.doc (38.5K)

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.
________________________________________
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. ( 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.
6. Change the “main.cpp” to use the decimal value of the fractions in the 5 object array, and show the decimals in sorted order from largest to smallest using a Linked List. Change the display menu so the user can select to see this list in decimal format. Allow the user to return to the main menu when done.
________________________________________

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.
________________________________________

PART IV: Add “overloaded operators” to the Fractions Class. Remember, all manipulation of fractions must be in their reduced state.
1. Add four arithmetic operators (+, -, *, and /) to the Fraction Class.
2. Add the overloaded operator ==( ) function to compare the value of two fractions.
3. Add the overloaded operator >( ) and <( ) functions to compare the value of two functions.
4. Add extraction and insertion operators to the Fraction Class.
5. Change the main menu to include an additional selection for fraction arithmetic and comparisons. After selection of this item, a new menu appears and shows a selection of operations from the above arithmetic and overloaded operators. Using the fractions entered in the 5 fraction object array from above, allow the user to use the operators and overloaded operators on the fractions represented by the five object array. Be sure the user can return to the main menu.
________________________________________

PART V: Applying Inheritance from the Fraction Class.
1. Create a new “MathProblem Class” that holds four Fraction objects:
o The first fraction operand
o The second fraction operand
o The user's answer to the problem
o The correct answer to the problem.
o The class must also contain a character field to store an operator
o The class must also have a bool field named “isAnswerCorrect”
o Include a function “setProblem( )”. This function provides the arguments for the problem by showing two operands and the operator. The function calculates the correct answer and sets “isAnswerCorrect” to 0 (e.g. False).
2. Include a function “displayProblem( )” that shows the problem as a question.
4. Include a function “randomGenerator( )” that assigns two randomly generated fractions to each of the 5 MathProblem objects.
5. Write into the “main.cpp” a change to the user's menu a selection to launch a program that declares 5 MathProblem objects as a test of student skills.
6. Display the 5 problems, the student's answer, the correct answer, and a message indicating whether the student got the question right or wrong. Return to the main menu.

________________________________________
PART VI: Simplify the problem with templates. The templates must work whether the problem needs an integer, double, character, or Fraction answer.
1. Create a function template called “problem( )” that accepts three arguments. The first and third arguments are generic values representing values a student will use as an arithmetic problem. The middle argument is a character representing a mathematical operator. The template will display the first, second, and third arguments and allow the student to submit an answer. The function template also returns the student's answer to the program.
2. Create another function template called “solution( )” that accepts three arguments. The first and third arguments are generic values representing values a student will use as an arithmetic problem. The middle argument is a character representing a mathematical operator. The function template also returns the correct answer to the program.
3. Create a third function template called “congrats( )” that accepts the correct answer. It displays the correct answer and then a congratulatory message if the answer is right. Display the congratulations message 5 times with the answer. If the answer is incorrect, display only one time “Sorry, try again”.
4. In main, use the same “randomGenerator( )” for the problems with these templates. There must be 5 problems for integers and 5 for doubles. Return to the main menu when done with the 5 problems.
5. Create a Class Template called “Containers Class”. In this template place everything you need for the 5 Fraction Object Array, and the sorted linked list. Modify your code to use these templates instead of direct inline functions in main( ).

Is This A Good Question/Topic? 0

## Replies To: Fraction Problem C++

### #2 Martyr2

• Programming Theoretician

Reputation: 5201
• Posts: 13,944
• Joined: 18-April 07

## Re: Fraction Problem C++

Posted 22 October 2008 - 10:24 PM

First of all we ask that people provide a "Best effort" attempt at solving the problem first. This means we like to see what you have already coded so far and it should be a good attempt at trying to solve it yourself. We DO NOT do people's homework for them. We simply guide people to the solution when they are stuck.

Second of all, why is this posted in Flash & Actionscript? This is a C++ program and that means it should be in the C and C++ forum.