Can't Figure Out How to Write Add Fraction Function

I have to write a function that takes two fraction instances and adds them together.

It is not allowed to be a mutation function, and it must be a const member function of my class

I've been trying to get something like this to work:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
class Rational
{
private:
	long int numerator;
	long int denominator;
public:
	Rational(); // Default constructor
	Rational(const long int, const long int);
}





Rational add(const int n, const Rational& instance2) const;

 

//This is where I have the most problems. There is psuedo-code for the general idea I want to do, but i'm not exactly sure how to execute my idea
Rational Rational::add(const int n, const Rational& instance2) const
	{
        //Psuedo-code 
	Rational result(numerator, denominator);

     result.numerator = ((numerator) * (instance2.denominator) + ((denominator) * instance2.numerator));
     result.denominator = (denominator * instance2.denominator);
     int j;
     j = lcm(denominator, instance2.denominator);
     result.numerator = result.numerator / j;
     result.denominator = result.denominator / j;
			
     return result;
	}


n = 10;
for (i = 0; i < n; i++)
	{
		Rational add(1, i);
		answer = answer.add(n, add);
	}


I just can't seem to wrap my head around how to accomplish this without mutating and keeping it a const function
Last edited on
Hi,

You can alter the result as much as you like, just don't alter either of the operands of the add function.

Consider checking if any of the numerators or denominators are zero. You should handle these different cases.

Consider writing the default constructor, so that it defaults to (1/1), then use it on line 23

Why do you add 1 to the numerators and denominators?

Why is n a parameter for the function, it's not used in the function? Although I can see what you are trying do on line 40, the add function should only take a Rational as an argument.

As an aside, in C++ one can declare and assign a value all in one statement:

int j = lcm(denominator, instance2.denominator);

In a for loop:

for (int i = 0; i < n; i++)
Although the scope of i is limited to the for loop.


Choose better names for your variables, I am not a fan of i or j as identifiers, they look too similar IMO
Thanks for the pointers! My default constructor must start at 0/0 but writing in specific cases should solve that!

Adding 1 was originally an attempt to get the 0/0 + 1/1 working, though any further additions would make that 1 redundant, also thanks for the catch on the n parameter, I was using it in an earlier attempt and forgot to remove it from the function parameters.

If anyone is wondering I solved it by changing it to:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
Rational result(numerator, denominator);

switch (instance2.denominator)
{
case 1: result.numerator = 1;
		result.denominator = 1;
		break;
default:result.numerator = ((numerator) * (instance2.denominator) + ((denominator) * instance2.numerator));
		result.denominator = (denominator * instance2.denominator);
		int j = gcd(result.numerator, result.denominator);
		result.numerator = result.numerator / j;
		result.denominator = result.denominator / j;
}	
		
	return result;
Last edited on
I don't think a default constructor starting at 0/0 is a sensible idea - that's an utterly meaningless fraction. It would also cause chaos if you tried to "add" it to any other fraction.

You should also look at your range of values of i in your for loop, and make sure that they actually correspond to the (truncated) series you are trying to compute, which I understand starts at 1/1 and ends on 1/n.
Last edited on
When I said you need to take care of zeros in the num /denom and handle those separate cases, I didn't mean specifically using a switch statement. I meant the separate cases of: the numerator is zero -> result is zero ; denominator is zero -> result is undefined -> an error.
Topic archived. No new replies allowed.