Welcome! Log In Create A New Profile


Assignment 2 Question 3

Posted by schoema 
Announcements Last Post
Announcement SoC Curricula 09/30/2017 01:08PM
Announcement Demarcation or scoping of examinations and assessment 02/13/2017 07:59AM
Announcement School of Computing Short Learning Programmes 11/24/2014 08:37AM
Announcement Unisa contact information 07/28/2011 01:28PM
Assignment 2 Question 3
September 01, 2009 12:52PM
In order to clear up some confusion around question 3 of Assignment 2, please change it as follows:

Include a function to normalize the Fraction class so that, after normalization, the denominator is positive and the numerator and denominator are as small as possible. For example, after normalization 4/-8 would be presented the same as -1/2. Any sign is therefore carried by the numerator; keep the denominator positive.

A fraction can be reduced to its smallest value by computing the greatest common divisor (GCD) of the numerator and denominator, and then dividing each by this number.

A recursive definition to compute the GCD of two integers follows:

n if n divides evenly in m
gcd(m,n) =
gcd(n, remainder of m divided by n) otherwise

This definition can be coded as follows:
int gcd(int m, int n)
if ((m % n) == 0)
return n;
return gcd(n, m % n);

Modify the constructor(s) written in the previous exercise to include a call to normalize() so that every initialized fraction is in its lowest common terms. Also make sure that each overloaded operator function also uses normalize() to return a fraction in its smallest value.

Replace the display function with an overloaded insertion operator so that a Fraction object can be inserted directly into the cout stream. Also include an overloaded extraction operator that uses the cin stream with a Fraction object. Overload both the insertion and extraction operators as friend functions.

Note: The function to normalize the fractions is not part of the Fraction class although it should be included in the implementation file for separate compilation.
Sorry, only registered users may post in this forum.

Click here to login