Welcome! Log In Create A New Profile

Advanced

Assignment 2 Question 2

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 2
August 26, 2009 09:33AM
Question from a student:
I have a question about the 2nd question of assignment 02.

How do you want me to implement the add, sub, mul, and div member functions? Must a.add(b) affect the nominator / denominator values of object a, or must it return a third object of type Fraction containing the result?

I’ve also noticed that the formula for “Product of two fractions” is incorrect. Must I use correct one of (ac)/(bd)?

Reply:
a.add(b) should not affect a or b at all, i.e. it should return a third object of type Fraction. You should be able to use it as follows:
c = a.add(b);

Also, please use the correct formula (ac)/(bd) for the product of two fractions. Apologies for the mistake.

Mrs MA Schoeman
Re: Assignment 2 Question 2
September 07, 2009 03:57PM
Good day,

I am somewhat confused with the member functions for the add, mul, sub & divide calcs.

Kindly shed some light if i am following the correct path:

1. I have declared the member functions example: void sub (int a, int b, int c, int d);
2. I have created a defeintion per member function example:

void Fraction::sub(int a, int b, int c, int d)
{
(a/b).sub(c/d) = (a+d/b+d).sub(c+b/b+d);
}

Am i on the right track?
Re: Assignment 2 Question 2
September 09, 2009 06:14PM
I must admit that I am also quite stumped when it comes to this question.

I sincerely ask some of the more experienced coders to please lend a had and point us in the right direction as to the sub, add, mul and div functions.

Hoping for a speedy response

Chris Botha

=============================
Far and away the best prize that life has to offer is the chance to work hard at work worth doing.
Theodore Roosevelt
avatar Re: Assignment 2 Question 2
September 09, 2009 09:51PM
You need to start thinking more about fractions as whole objects instead of a collection of details.

To that end, your arithmetic operations are such that two Fraction objects are processed and a resultant Fraction object is returned. A slight difference is that you should take the one on the left hand side to be the one that actually does the operation and then creates a third Fraction. Your operations should thus be of the form:

Language: C++
Fraction Fraction::arithmeticOperation(const Fraction &rhs);

This may be a bit too abstract so I'll help you out with the sum function.

Language: C++
Fraction Fraction::sum(const Fraction &rhs){ int newNumerator, newDenominator;   newNumerator = numerator * rhs.denominator + rhs.denominator * denominator; newDenominator = denominator * rhs.denominator;   return Fraction(newNumerator, newDenominator); }

To use this you can do the following:

Language: C++
Fraction half (1, 2), quarter(1, 4); Fraction sixEighths = half.sum(quarter);

Note that the original Fraction objects are not altered in any way and a new Fraction (the sum) is created.

I hope this helps.
Re: Assignment 2 Question 2
September 10, 2009 04:36AM
Thanks a million robanaurochs .

You're a lifesaver

Chris

Chris Botha

=============================
Far and away the best prize that life has to offer is the chance to work hard at work worth doing.
Theodore Roosevelt
Sorry, only registered users may post in this forum.

Click here to login