asked 122k views
3 votes
1.write a class RationalCpp that contains numerator and denominator as private attributes.RationalCpp should have three public constructors

RationalCpp():initialize the fraction with 0/1.
RationalCpp(int n):intialize the fraction with n/1.
RationalCpp(int n,int d): intialize the fraction with n/d.
2. write the following function that create RationalC:
RationalC create_rational():initialize the fraction with 0/1.
RationalC create_rational(int n):intialize the fraction with n/1.
RationalC create_rational(int n,int d): intialize the fraction with n/d.
3. write a free function void reduce_rational(RationalC& r) that reduces r completely using gcd.
Extend RationalCpp with the void reduce(),which completely reduces the instance on which it is called using gcd.
How can you deal sensibly with a negative numerator or denominator? Explain your approach?
4. Now modify the functions / methods and constructors that you have defined in (1)and (2) so that fractions are always irreducible.use the fraction /method that you have defined in (3) for this purpose.
why is it impossible to ensure that this invariant is always maintained in RationalC?

1 Answer

3 votes

1. **Class RationalCpp** is a C++ class that encapsulates a rational number, represented by a numerator and a denominator as private attributes. It provides three public constructors.

The first constructor initializes the fraction with 0/1, the second constructor initializes it with a given numerator n and denominator 1, and the third constructor initializes it with given numerator n and denominator d.

The **RationalCpp class** contains three constructors that allow flexible initialization of rational numbers. The first constructor sets the fraction to 0/1, representing zero. The second constructor takes an integer parameter n and sets the fraction to n/1, effectively making it an integer. The third constructor takes two integer parameters n and d and sets the fraction to n/d, allowing the representation of any rational number.

2. The **create_rational() function** is a helper function that returns a RationalC object. It has three overloaded versions of the function. The first version initializes the fraction with 0/1, representing zero. The second version takes an integer parameter n and initializes the fraction with n/1, making it an integer. The third version takes two integer parameters n and d and initializes the fraction with n/d, representing any rational number.

The **create_rational() function** provides flexibility in creating RationalC objects with different initial values. It allows the user to create fractions representing zero, integers, or any arbitrary rational number by providing appropriate arguments.

3. The **reduce_rational() function** is a free function that takes a reference to a RationalC object as a parameter. It reduces the given rational number completely using the greatest common divisor (gcd) algorithm. The function modifies the numerator and denominator of the rational number, simplifying it to its irreducible form.

The **reduce() method** is an extension added to the RationalCpp class. When called on an instance of the class, it reduces the rational number completely using the gcd algorithm. By simplifying the numerator and denominator, it ensures that the fraction is always represented in its irreducible form.

When dealing with a negative numerator or denominator, the approach is to handle the sign separately. The negative sign is conventionally assigned to the numerator, and the denominator remains positive. This approach ensures consistency in representing negative rational numbers and facilitates arithmetic operations on them.

4. To ensure fractions are always irreducible, the functions, methods, and constructors defined in (1) and (2) can be modified to utilize the **reduce() method** defined in (3). Whenever a fraction is initialized or created, the reduce() method can be called to simplify the fraction to its irreducible form.

However, it is **impossible to ensure** that the irreducibility invariant is always maintained in the RationalC class. This is because external operations or modifications performed on the rational numbers, which are beyond the control of the class, may result in intermediate fractions that are not irreducible. Continuous checking and reduction after every operation would impose significant computational overhead. Hence, it is left to the responsibility of the user to explicitly call the reduce() method when necessary to ensure irreducibility.

answered
User Iamsuman
by
8.3k points