Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon.

Pages: 1-

C++ Rational Numbers

Name: Anonymous 2012-07-10 12:55

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: Anonymous 2012-07-10 12:56

What's up with the bools?

Name: Anonymous 2012-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: Anonymous 2012-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: Anonymous 2012-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: Anonymous 2012-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: Anonymous 2012-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;
}

maybe something like that??

Name: Anonymous 2012-07-10 16:21

You're in the wrong course.

Don't change these.
Name: Email:
Entire Thread Thread List