Question

Write a C++ program and algorithm to create a class of fraction having variables numerator and...

Write a C++ program and algorithm to create a class of fraction having variables numerator and denominator, and member functions setting and getting values of the fractions. Also create a static member variable of the class having fraction value tolerance of 0.1. Instantiate 10 fraction objects and find out how many of them are below and above tolerance level. Decrement the tolerance to 0.01 by a static member function and find out how many of them are now above and below the new tolerance level.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

ANSWER:

GIVEN THAT:

Write a C++ program and algorithm to create a class of fraction having variables numerator and denominator, and member functions setting and getting values of the fractions.

PROGRAM:

#include <iostream>

using namespace std;

class Fraction
{
private:
    int numerator, denominator; 
public:
    Fraction()
    {
        numerator = 1;
        denominator = 1;
    }
    Fraction(int n, int d)
    {
        numerator = n;
        if (d==0) 
        {
            cout << "ERROR: ATTEMPTING TO DIVIDE BY ZERO" << endl;
            exit(0); // will terminate the program if division by 0 is attempted
     }
  Fraction Division(Fraction  int gcd(int n, int d) { 
int remainder; 
while (d != 0)
 { 
remainder = n % d; 
n = d; 
d = remainder;
If(remainder==0.01)
cout<<"tolerance";
 } 
return n;
} 
void show() // Display method
 { 
 if(denominator == 1)
cout << numerator << endl;
 else 
cout << numerator << "/" << denominator << endl; 
} 
}
int main() 
{
char n;
do
{
    Fraction a(1,2);
    Fraction b(1,4);
    Fraction c;
    c = a.Division(b); 
    c.show();
count<<"want to try one more?(y for yes, n for no)";
cin>>n;

}while(n=='y' or n=='Y');

return 0;

}

Add a comment
Know the answer?
Add Answer to:
Write a C++ program and algorithm to create a class of fraction having variables numerator and...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Consider a class Fraction of fractions. Each fraction is signed and has a numerator and a...

    Consider a class Fraction of fractions. Each fraction is signed and has a numerator and a denominator that are integers. Your class should be able to add, subtract, multiply, and divide two fractions. These methods should have a fraction as a parameter and should return the result of the operation as a fraction. The class should also be able to find the reciprocal of a fraction, compare two fractions, decide whether two fractions are equal, and convert a fraction to...

  • This in in C# There are two classes, class Fraction and class FractionDemo The user needs...

    This in in C# There are two classes, class Fraction and class FractionDemo The user needs to be able to input a value for the numerator and the denominator Create a Fraction class with private fields that hold a positive int numerator and a positive int denominator. In addition, create Properties for each field with the set mutator such that the numerator is greater than or equal to 0 and the denominator is greater than 0 (illegal values should be...

  • Create a fraction class. This will have two attributes, a numerator and a denominator, both int....

    Create a fraction class. This will have two attributes, a numerator and a denominator, both int. This class will have constructors accessors and mutators (for the attributes) a toString method which will allow us to print the fraction in the form 3/4 an Add method so we can add two fractions a subtract method (subtracts one fraction from the other) a multiply method (multiply two fractions) a divide method (divides one fraction by the other) DO NOT (for now) worry...

  • Create a structure Fraction which contains a numerator and a denominator then do the following (In...

    Create a structure Fraction which contains a numerator and a denominator then do the following (In C++):    a. Write a function void printFraction(Fraction f) which prints out a fraction in the following format; e.g. if the numerator is 2 and the denominator is 5, it will print out 2/5    b. Write a function Fraction mult(Fraction f1, Fraction f2) which returns a new fraction which is the product of f1 and f2.    c. Write a function Fraction add(Fraction...

  • Write a Fraction class. An example of a fraction is 1/2. Note that C/C++ will convert...

    Write a Fraction class. An example of a fraction is 1/2. Note that C/C++ will convert it to 0.5, but for this problem, it should still be displayed as 1/2. You should have at least the following two private member variables: numerator (top part), and denominator (bottom part). Overload the following operators: ==, +, << and >>. Also, implement the default constructor and a second constructor that takes two arguments for the numerator and the denominator. Make sure the denominator...

  • java only no c++ Write a Fraction class whose objects will represent fractions. You should provide...

    java only no c++ Write a Fraction class whose objects will represent fractions. You should provide the following class methods: Two constructors, a parameter-less constructor that assigns the value 0 to the Fraction, and a constructor that takes two parameters. The first parameter will represent the initial numerator of the Fraction, and the second parameter will represent the initial denominator of the Fraction. Arithmetic operations that add, subtract, multiply, and divide Fractions. These should be implemented as value returning methods...

  • Create a new class named Fraction that models fractions, such as 2/3 or 45/9. Implement the...

    Create a new class named Fraction that models fractions, such as 2/3 or 45/9. Implement the following API: Constructor: Fraction(int numerator, int denominator) . String toString() so instances of the class can be printed. The format used above is sufficient. Fraction simplify() This method returns a new Fraction that is arithmetically equal to that given, but for which the numerator and denominator have no common factors. Create a testing program, TestFraction, that prompts the user to provide the numerator and...

  • (Rational class) Create a class called Rational for performing arithmetic with fractions. Write a program to...

    (Rational class) Create a class called Rational for performing arithmetic with fractions. Write a program to test your class. Use integers variables to represent the private data of the class- the numerator and the denominator. Provide a constructor that enables an object of this class to be initialized when it is declared. The constructor should contain default values in case are no initializers are provided and should store the fraction in reduced form. For example, the fraction 3/6 would be...

  • C++ Question Your class should support the following operations on Fraction objects: • Construction of a...

    C++ Question Your class should support the following operations on Fraction objects: • Construction of a Fraction from two, one, or zero integer arguments. If two arguments, they are assumed to be the numerator and denominator, just one is assumed to be a whole number, and zero arguments creates a zero Fraction. Use default parameters so that you only need a single function to implement all three of these constructors. You should check to make sure that the denominator is...

  • C++ For this assignment you will be building on the Original Fraction class you began last...

    C++ For this assignment you will be building on the Original Fraction class you began last week. You'll be making four major changes to the class. [15 points] Delete your set() function. Add two constructors, a default constructor that assigns the value 0 to the Fraction, and a constructor that takes two parameters. The first parameter will represent the initial numerator of the Fraction, and the second parameter will represent the initial denominator of the Fraction. Since Fractions cannot have...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT