In this task, we compare how data structures and their associated functions can be defined in Cand Cr As an example, we
-
answerhappygod
- Site Admin
- Posts: 899604
- Joined: Mon Aug 02, 2021 8:13 am
In this task, we compare how data structures and their associated functions can be defined in Cand Cr As an example, we
In this task, we compare how data structures and their associated functions can be defined in Cand Cr As an example, we consider rational numbers, which are represented as a pair of an integer numerator and an integer denominator. In this task, the numerator and denominator are represented as int (o Write a struct Rational containing numerator and denominator as public attributes. 2 points Go Write the following functions that create a Pational: • Rational create rational: Initialises the fraction with . Rational create rational(int n): Initialises the fraction with Rational create rational (int n, int d): Initialises the fraction with 2 points (Write a class RationalOpp that contains numerator and denominator as private attributes RationalCpp should have three public constructors (similarly to [i: . RationalOppO: Initialises the fraction with RationalCpp (int n): Initialises the fraction with RationalCpp(int n, int d): Initialises the fraction with Moreover, RationalOpp should have public methods that allow reading and setting the numer- ator and denominator. Reading the numerator or denominator of constant objects should be possible 6 points (iv) Write a free function int ged(int, int) (Greatest common Divisor) to determine the greatest common divisor of two numbers, The greatest common divisor can be determined using the Euclidean algorithm, see eg. https://en, vikipedia.org/wiki/Euclidean algorithe. - Implement this procedure as a recursive function. 2 points 2 (1) Write a free function void reduce_sational (Rationalcs r) that reduces r completely using god Extend Rational pp with the method void reduce(), which completely reduces the instance on which it is called using ged. How can you deal sensibly with a negative numerator or denominator? Explain your approach. 3 points (vi) Now modify the functions/methods and constructors that you have defined in and [so that fractions are always irreducible. Use the function/method that you have defined in for this purpose. Why is it impossible to ensure that this invariant is always maintained in RationalC?
Join a community of subject matter experts. Register for FREE to view solutions, replies, and use search function. Request answer by replying!