Basic c++ math question

I'm doing a practice problem that gives me a function header, and a function and expects me to write the code.

I'm given(assume lowestTerms is given):

1
2
3
4
5
6
7
8
9
10
11
12
13
void lowestTerms(int int& n, int& d);
/*ratAdd**************************************
Add rational numbers, result expressed in lowest terms.
@parms ln-numerator for left operand
       ld-denominator for left operand
       rn-numerator for right operand
       rd-denominator for right operand
       on- result numerator
       od-result denominator
@modifies on and od set to the numberator and denominator of the sum

@returns false if a denominator is zero, true otherwise.
********************************************** */


So I have a few questions, first of all, since it's returning true or false, do I make it a bool? It also is asking for a value(lowest terms) so I'm not sure.
Also, it says it modifies on and od, but how do I get on and od from my answer?(i.e. if I say (ln/ld)*(on/od) how can I get on and od alone? It returns me a whole number not the numerator/denominator. In C++ I coded it(below) to give me ln/ld+rn/rd=on/od, but how can I separate on/od? if I add ln to rn before I add the rational numbers it wouldn't be correct. Should I be using doubles or ints?

My codes below, this one is obviously wrong since I didn't use lowestTerms or od/on at all. My second code is below this one.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
// Example program
#include <iostream>
#include <string>
double ratAdd(double ln, double ld, double rn, double rd);
int main()
{
std::cout<<ratAdd(4,5,2,3);
}
double ratAdd(double ln, double ld, double rn, double rd){
int od=0;
int on=0;
double ans;
if(ld==0 || rd==0){
return 0; //is this correct for returning false?
}
else{
ans=(ln/ld)+(rn/rd); //this is the part I don't get. Should I have said od=ln+rn? I would think on/od would just be incorrect if I did it that way?
}
return ans;
}


Second attempt at code: This one seems more "correct" but I have no idea about the doubles, ints, bools, and if the math even makes sense. Saying (ln+rd)/(or+od) seems wrong to me.
1
2
3
4
5
6
7
8
9
10
11
12
13
double ratAdd(double ln, double ld, double rn, double rd){
int od=0;
int on=0;

if(ld==0 || rd==0){
return 0; //is this correct for returning false?
}
else{
int on=ld+rd;
int od=ln+ld;
}
return lowestTerms(on,od);
}

I'm really confused what the question is asking me for. I have no idea if I should be using bools, ints, or doubles, and what exactly to return.
Last edited on
Based on this documentation,
1
2
3
4
5
6
7
8
9
10
11
12
/*ratAdd**************************************
Add rational numbers, result expressed in lowest terms.
@parms ln-numerator for left operand
       ld-denominator for left operand
       rn-numerator for right operand
       rd-denominator for right operand
       on- result numerator
       od-result denominator
@modifies on and od set to the numberator and denominator of the sum

@returns false if a denominator is zero, true otherwise.
********************************************** */
the function header should look like this:
 
bool ratAdd(int ln, int ld, int rn, int rd, int & on, int & od)
returns false if a denominator is zero, true otherwise.

That means it returns a bool.

@parms ln-numerator for left operand
ld-denominator for left operand
rn-numerator for right operand
rd-denominator for right operand
on- result numerator
od-result denominator

That means there are 6 parameters: ln, ld, rn, rd, on, od. The last two are output parameters so you should pass them by reference.

Since these are numerators and denominators, I'd use ints or longs (or long longs) for their types.

Putting it all together, the function prototype should be:
bool ratAdd(int ln, int ld, int rn, int rd, int &on, int &od);

In your example, you set on and od even if you're going to return false. I usually leave output arguments unchanged if a function fails.

You've got the right idea in your example, but the logic to add the numbers when they are valid isn't right. You have:
1
2
int on=ld+rd;
int od=ln+ld;

Consider what happens if you add 1/2 + 1/3. Your logic will give 2/5 instead of the right answer (5/6). Figure out how to add fractions with pencil and paper and then translate that to code.

Okay, I changed my function to a bool and made it 6 parameters, I also changed the int's to doubles. I thought because of how the header was worded("add rational numbers') I should not use multiplication, but I changed my code to calculate it as you would normally. I'm not sure if it works because I have no lowestTerms code.

As for the part which says "@returns false if a denominator is zero, true otherwise." would it be sufficient to only say return false if od!=0, since od is only 0 when either ld, rd, or both are equal to zero?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
// Example program
#include <iostream>
#include <string>
int lowestTerms(int& n, int& d);
bool ratAdd(int ln, int ld, int rn, double rd, int& on, int& od);
int main()
{

}
bool ratAdd(int ln, int ld, int rn, int rd, int& on, int& od){
od=ln*ld;
bool ans=false;
if(ld==0 || rd==0){
ans=false;
}
else{
ans=true;
int on=(ln*rd)+(rn*ld);
int od=ln+ld;
lowestTerms(on,od);
}
return ans;
}
would it be sufficient to only say return false if od!=0

Yes, but it means that when you return false, you will have modified the output parameters. I always try to avoid that, but it's matter of taste I suppose.

I'm not sure if it works because I have no lowestTerms code

Well, it should still return a value that is equal to the sum, just not in lowest terms. Right now it still doesn't work. If adding 1/2 + 1/3, lines 18-19 will set the answer to 5/3
Opps, I forgot to remove line 19.

Is this correct?


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
// Example program
#include <iostream>
#include <string>
int lowestTerms(int& n, int& d);
bool ratAdd(int ln, int ld, int rn, double rd, int& on, int& od);
int main()
{

}
bool ratAdd(int ln, int ld, int rn, int rd, int& on, int& od){
od=ln*ld;
bool ans=false;
if(ld==0 || rd==0){
ans=false;
}
else{
ans=true;
int on=(ln*rd)+(rn*ld);
lowestTerms(on,od);
}
return ans;
}



Also, if I'm trying to call the function in main(without lowestTerms, just testing ratadd), what should I put for on/od? I have no value for them since they are solved in the function.
Would it be...
ratAdd(4,5,3,2,on,od);

or something like
x=0;
y=0;
ratAdd(4,5,3,2,x,y);

Here's what I tried but it returns nothing and doesn't compile. It says on and od are unused.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
// Example program
#include <iostream>
#include <string>
bool ratAdd(int ln, int ld, int rn, double rd, int& on, int& od);
int main()
{

int x=0;
int y=0;
ratAdd(4,5,3,2,x,y);
}
bool ratAdd(int ln, int ld, int rn, int rd, int& on, int& od){
od=ln*ld;
bool ans=false;
if(ld==0 || rd==0){
ans=false;
}
else{
ans=true;
int on=(ln*rd)+(rn*ld);
int od=ln+ld;
}
return ans;
}
Last edited on
what should I put for on/od? I have no value for them since they are solved in the function. Would it be...
ratAdd(4,5,3,2,on,od);

Yes

1
2
int on=(ln*rd)+(rn*ld);
int od=ln+ld;

That actually declares two new variables called od and on that hide the parameters. Just change this to:
1
2
on=(ln*rd)+(rn*ld);
od=ln+ld;

You can simplify the error checking also to:
1
2
3
4
5
6
if(ld==0 || rd==0){
    return false;
}
on=(ln*rd)+(rn*ld);
od=ln+ld;
return true;


od=ln+ld; ??? doesn't look right to me.
Doh! Right, he's setting it right at line 13:
od=ln*ld;
and then later has:
int od=ln+ld;

When I read the post I thought "Okay, he's got the denominator right" at line 13 and didn't pay attention later. My bad. Thanks for catching that.
How about od = ld*rd;, might that be better?
Topic archived. No new replies allowed.