Hey guys, i have a programming assignment to do for my c++ course, it involves rational numbers and i have to add/subtract/multiply/divide them in 4 seperate functions
Heres the header file, any help??
bool ratAdd(int ln, int ld, int rn, int rd, int& on, int& od);
bool ratSub(int ln, int ld, int rn, int rd, int& on, int& od);
bool ratMult(int ln, int ld, int rn, int rd, int& on, int& od);
bool ratDiv(int ln, int ld, int rn, int rd, int& on, int& od);
Name:
Anonymous2012-07-10 12:56
What's up with the bools?
Name:
Anonymous2012-07-10 13:05
Not totally sure, that is what was given to me by the prof. But heres some more information.
ln/ld and rn/rd are the left and right operand, respectively,
on/od is the result, which must be expressed in lowest terms, and
the returned value is false if and only if a denominator is 0.
Name:
Anonymous2012-07-10 13:14
That's a strange way to go about it. I assume your class has not gone over exceptions.
Assuming you know your math, writing the code should be pretty easy.
Hint: find the greatest common factor before trying to figure out on and od.
Name:
Anonymous2012-07-10 13:16
Heres some code, can anyone tell me if im right or what else i have to do?
bool ratAdd(int ln, int ld, int rn, int rd, int& on, int& od){
return on=(ln*rd)+(ld*rn);
return od=rd*ld;
}
bool ratSub(int ln, int ld, int rn, int rd, int& on, int& od){
return on=(ln*rd)-(ld*rn);
return od=rd*ld;
}
bool ratMult(int ln, int ld, int rn, int rd, int& on, int& od){
return on=(ln*rd)*(ld*rn);
return od=rd*ld;
}
bool ratDiv(int ln, int ld, int rn, int rd, int& on, int& od){
return on=(ln*rd)/(ld*rn);
return od=rd*ld;
}
Name:
Anonymous2012-07-10 13:25
Do you understand how the return statement works? Read up on that. Keep in mind that you should be returning a bool.
Name:
Anonymous2012-07-10 13:40
yeah okay i understand i have to return false if od is zero and true if it is any other value, but im not quite sure how to impliment that...
if {
on == 0;
return on;
}
else {
return on == 1;
}
if {
od == 0;
return od;
}
else {
return od == 1;
}