Help on "Rational" problem

Help on "Rational" problem

Post by pblegen.. » Wed, 02 Feb 2000 04:00:00



I am trying to solve a problem out of Deitels' C++: HTP book, #6.7

I need to add, multiply, divide, etc. rational numbers.

i can't get the functions right.

if anyone knows how to do this or has any sample code, please help me
out.

what i have so far:
int common(int den1, int den2)
{
int search;
for(search=den2; search>0; search--)
{
if(((den1%search)==0) && ((den2%search)==0))

Quote:}
return search;
}

void addRat(int, int, int);

void addRat(int n1, int n2, d) //n1, n2 are numerators, d is value
{                               //returned by common function

??????????????????
any ideas?

thanks a million

Sent via Deja.com http://www.deja.com/
Before you buy.

 
 
 

Help on "Rational" problem

Post by Neil Duran » Thu, 03 Feb 2000 04:00:00


Quote:>I am trying to solve a problem out of Deitels' C++: HTP book, #6.7
>I need to add, multiply, divide, etc. rational numbers.
>i can't get the functions right.
>if anyone knows how to do this or has any sample code, please help me
>out.

Your code:

int common(int den1, int den2)
{
   int search;
   for(search=den2; search>0; search--)
   {
     if(((den1%search)==0) && ((den2%search)==0))
   }
   return search;

Quote:}

...clearly isn't going to work - whenever it finds a suitable common
denominator, it just throws it away and carries on, eventually always
returning zero...

What you want is something more like this, with the return statement
within your if block:

int common(const int den1, const int den2)
{
   for(int search = den2; search > 0; search--)
   {
     if((den1 % search)==0 && (den2 % search)==0)
       return search;
   }

Quote:}

 >void addRat(int n1, int n2, d) //n1, n2 are numerators, d is value
 >{                               //returned by common function

As for your addRat function, what are you actually trying to do?  Add
two rational numbers?  If so, you really need to pass in both numerators
and both denominators.  However, I really can't help thinking the whole
exercise could be made better and you could learn more by writing a C++
class called Rational, with +,-,/,* operators.  Your "common" function
would be a private member of your Rational class, and you'd expose
public arithmetic operators which act on the rational numbers
represented by objects of the Rational class.  That way you could do
stuff like this:

Rational r1(1,3);   // Declare r1 to be 1/3
Rational r2(5,23)   // Declare r2 to be 5/23

Rational sum = r1 + r2;  // Would set sum to be 1/3 + 5/23

cout << sum;        // Would write the value out as 38/69

I'll leave you that class to write as an exercise.....*8)

Neil
--
===================================================================

Tel: 01293 454736      Mobile: 07931 164 346      Fax: 08700 520159
===================================================================

 
 
 

Help on "Rational" problem

Post by Neil Duran » Thu, 03 Feb 2000 04:00:00


Ooops!

Quote:>int common(const int den1, const int den2)
>{
>  for(int search = den2; search > 0; search--)
>  {
>    if((den1 % search)==0 && (den2 % search)==0)
>      return search;
>  }
>}

You'll need to add a return 1; just before the closing brace, otherwise
it won't compile.  Actually thinking about it, the function would be
better with a while, rather than a for loop:

int common(const int den1, const int den2)
{
   int nSearch = min(den1, den2);

   while(((den1 % nSearch) || (den2 % nSearch)) && nSearch > 1)
     nSearch--;

   return search;

Quote:}

Neil
--
===================================================================

Tel: 01293 454736      Mobile: 07931 164 346      Fax: 08700 520159
===================================================================
 
 
 

Help on "Rational" problem

Post by pblegen.. » Thu, 03 Feb 2000 04:00:00


Thanks Neil,
yes actually this assignment is in the "Classes" chapter, and just as I
was getting that syntax in my head, i am stuck with this algorithm.
I just didn't know where to start.

it says to create a class Rational, then use member functions to add,
multiply, divide, and subtract two rational numbers, then output them in
"1/2" format..

thanks for the ideas..

gary



> Ooops!

> >int common(const int den1, const int den2)
> >{
> >  for(int search = den2; search > 0; search--)
> >  {
> >    if((den1 % search)==0 && (den2 % search)==0)
> >      return search;
> >  }
> >}

> You'll need to add a return 1; just before the closing brace,
otherwise
> it won't compile.  Actually thinking about it, the function would be
> better with a while, rather than a for loop:

> int common(const int den1, const int den2)
> {
>    int nSearch = min(den1, den2);

>    while(((den1 % nSearch) || (den2 % nSearch)) && nSearch > 1)
>      nSearch--;

>    return search;
> }

> Neil
> --
> ===================================================================

> Tel: 01293 454736      Mobile: 07931 164 346      Fax: 08700 520159
> ===================================================================

Sent via Deja.com http://www.deja.com/
Before you buy.
 
 
 

1. "own" vs "uses" vs "contains" vs "is a"

I am studing for a VB exam and the book I have mentions there are
relation types between objects. (New Riders pg. 22 "MCSD Visual Basic
6 Exams 70-175 and 70-176".  (FYI - I did take a course in Object
Oriented Design!)

The book does not give any examples, but I have come up with the
following examples for the relation types that the book says exist. I
came up with the examples etc. from another book by Kurata ( Doing
objects in Visual Basic 6".

My question is how is "contains" different from "has a" I cannot come
up with an example.

"is a" - example a programer "is a" employee. In VB this might be
called a subclass.

"uses" - a data screen "uses a" employee object. In VB this might be
called a collaborator.

"has a" - and employee "has a" time sheet. In VB this might be called
a container.

"contains" -

2. Keyboard pinout?

3. Removing "My Computer", "Recycle Bin" and "Network Neighborhood" from desktop

4. Smacker utilities are now FREE!

5. Why "public" "private" "protected"

6. Outlook Express installed with new Windows 98 platform.

7. extern "FORTRAN", extern "Pascal", extern "Ada" ... ?

8. World's best lossless image compression?

9. Windows "Large Fonts" <--> "Small Fonts" problem...

10. Problem with "string" in a "class".

11. VC++ problem - head files - "except" and "process"

12. Urgent,please help: Using "CreateFile" & "TransmitCommChar" for Parallel Port?

13. Help: operator ">>", "<<" with double