COMPGROUPS.NET | Search | Post Question | Groups | Stream | About | Register

### exercise in GCD function

• Email
• Follow

```I have brought a book called "objected-oriented programming using C++"
by "IRA POHL", a "Grady Booch" series. In that book's chapter 2 which talks
about basic programming concepts, given a function to find the gcd of two
number.

#include<iostream.h>
#include<assert.h>

int gcd(int m, int n)
{
int r;
while(n != 0)
{
r = m % n;
m = n;
n = r;
}
return m;
}

int main()
{
int x,y,g;
cout << "\nprogram gcd C++";
do
{
cout << "\n enter two ints:";
cin >> x >> y;
assert( x * y != 0);
cout << "\n GCD(" << x << ", " << y << ") = "
<< (g = gcd(x,y)) << endl;
assert( x % g == 0 && y % g == 0);
}while( x != y);
}

In the exercise section of the chapter is based on the above code snippet:

"Rewrite the gcd() function with a for loop replacing the while loop"

As a beginner i am able to convert a simple while statements to for loop.
But in the above I am un-able to change the while loop into a for loop.
I tried the following program which is wrong:

int gcd(int m , int n)
{
int r;
for(n = r,n != 0; m = n ; r = m % n)

return m;

}

Can anybody help me in the following:

1) in the book version gcd program what is a assignment expression

m = n;
does?

2) Can any one give me a hint for the above while loop to for loop
conversion (don't give the answer, just give me some clue)

Thanks:

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated.    First time posters: Do this! ]

```
 0
Reply sathya_me 1/4/2006 1:34:53 PM

See related articles to this posting

```sathya_me wrote:
> I have brought a book called "objected-oriented programming using C++"
> by "IRA POHL", a "Grady Booch" series.

I'd first look up every book I buy at accu.org. This book isn't listed
there, so it's hard to tell if it's any good.

> In that book's chapter 2 which talks about basic programming concepts,
> given a function to find the gcd of two number.
>
> #include<iostream.h>

If the book really uses these obsolete headers, the C++ taught in that book
is probably not up to date and it should probably not be used for learning
C++. It might teach other things though which have not changed, I can't
tell.

> #include<assert.h>
>
>
> int gcd(int m, int n)
> {
>     int r;
>     while(n != 0)
>     {
>             r = m % n;
>             m = n;
>             n = r;
>     }
>     return m;
> }
>
> int main()
> {
>     int x,y,g;
>     cout << "\nprogram gcd C++";
>     do
>     {
>          cout << "\n enter two ints:";
>          cin >> x >> y;
>          assert( x * y != 0);
>          cout << "\n GCD(" << x << ", " << y << ") = "
>                << (g = gcd(x,y)) << endl;
>                assert( x % g == 0 && y % g == 0);
>     }while( x != y);
> }
>
> In the exercise section of the chapter is based on the above code snippet:
>
>   "Rewrite the gcd() function with a for loop replacing the while loop"
[...]

> 1) in the book version gcd program what is a assignment expression
>
>     m = n;
> does?

Yes, this is an assignment expression.

> 2) Can any one give me a hint for the above while loop to for loop
> conversion (don't give the answer, just give me some clue)

Sure. A for() loop has four parts:
for( _1_; _2_; _3_){
_4_
}
(Note: the {} around _4_ aren't necessary, they are just to illustrate
things better here.)

Now, these parts are executed in these places:
1. before the loop, once.
2. before every loop iteration, the result of this expression determines
whether another iteration is to be done.
3. after every loop iteration
4. during the loop iteration

The order is thus first 1 and then 243 until 2 finally returns false, e.g.
12432432432.

Now, the only thing to do is identify which part of the while loop belongs
to which part of the for loop. Just as a hint: the choice what corresponds
to 3 and what corresponds to 4 is partially a matter of taste, so there is
not always just one right solution for this.

Uli

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated.    First time posters: Do this! ]

```
 0
Reply Ulrich 1/4/2006 6:01:44 PM

```In article <43bb7811\$0\$15791\$14726298@news.sunsite.dk>, sathya_me
<sathyashrayan@REMOVEgmail.com> writes
>I have brought a book called "objected-oriented programming using C++"
>by "IRA POHL", a "Grady Booch" series. In that book's chapter 2 which talks
>about basic programming concepts, given a function to find the gcd of two
>number.
You have my commiserations. That author knows an awful lot of erroneous
things about C++. He is quite exceptional among authors published by
Addison-Wesley in that IMHO he has no idea what he is writing about.

--
Francis Glassborow      ACCU
Author of 'You Can Do It!' see http://www.spellen.org/youcandoit
For project ideas and contributions: http://www.spellen.org/youcandoit/projects

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated.    First time posters: Do this! ]

```
 0
Reply Francis 1/5/2006 12:40:29 AM

```> Sure. A for() loop has four parts:
>   for( _1_; _2_; _3_){
>     _4_
>   }
> (Note: the {} around _4_ aren't necessary, they are just to illustrate
> things better here.)
>
The braces are definitely necessary if _4_ has more than one statement.

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated.    First time posters: Do this! ]

```
 0

```Francis Glassborow wrote:
> In article <43bb7811\$0\$15791\$14726298@news.sunsite.dk>, sathya_me
> <sathyashrayan@REMOVEgmail.com> writes
> >I have brought a book called "objected-oriented programming using C++"
> >by "IRA POHL", a "Grady Booch" series. In that book's chapter 2 which talks
> >about basic programming concepts, given a function to find the gcd of two
> >number.
> You have my commiserations. That author knows an awful lot of erroneous
> things about C++. He is quite exceptional among authors published by
> Addison-Wesley in that IMHO he has no idea what he is writing about.

Are you sure?  His academic credentials seem impressive:
http://www.cse.ucsc.edu/~pohl/

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated.    First time posters: Do this! ]

```
 0
Reply Dilip 1/5/2006 12:44:23 PM

```In article <qh1t83-1on.ln1@satorlaser.homedns.org>, Ulrich Eckhardt
<eckhardt@satorlaser.com> writes
>sathya_me wrote:
>> I have brought a book called "objected-oriented programming using C++"
>> by "IRA POHL", a "Grady Booch" series.
>
>I'd first look up every book I buy at accu.org. This book isn't listed
>there, so it's hard to tell if it's any good.

It is ten years old (unless there is a very new edition of which I am
unaware) and so I think we probably dropped the review (which if my
memory serves me correctly was not exactly good) Perhaps you had not
noticed that the public reviews do not go back to books published in
1996.

And quite apart from the problems with the book as then written, C++ has
changed immensely over the last decade, not least in that that book was
written before there was a Standard by an author who did not appear to
have even noticed what was in the working draft.

Exactly why did you buy a C++ book with a 1996 copyright date?

--
Francis Glassborow      ACCU
Author of 'You Can Do It!' see http://www.spellen.org/youcandoit
For project ideas and contributions: http://www.spellen.org/youcandoit/projects

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated.    First time posters: Do this! ]

```
 0
Reply Francis 1/5/2006 1:30:05 PM

```In article <1136430816.670304.320290@g43g2000cwa.googlegroups.com>,
Dilip <rdilipk@lycos.com> writes
>> You have my commiserations. That author knows an awful lot of erroneous
>> things about C++. He is quite exceptional among authors published by
>> Addison-Wesley in that IMHO he has no idea what he is writing about.
>
>Are you sure?  His academic credentials seem impressive:
>http://www.cse.ucsc.edu/~pohl/

Well I judge writers by what they write not by their academic
credentials. However it is probably not fair to make any further comment
in a forum where the person is not able to defend himself. In fact I
think my comment above is pretty close to the edge of flaming.

However I will reiterate that the book was published 10 years ago and
very few books of that era about C++ have any validity today however
good they were then. I was unimpressed when I reviewed it way back then
and I am surprised that his publishers still have it in print. However
publishers often keep academic authors books in print because of a
steady sale to the author's students.

--
Francis Glassborow      ACCU
Author of 'You Can Do It!' see http://www.spellen.org/youcandoit
For project ideas and contributions: http://www.spellen.org/youcandoit/projects

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated.    First time posters: Do this! ]

```
 0
Reply Francis 1/5/2006 6:59:22 PM

```On 5 Jan 2006 08:30:05 -0500, Francis Glassborow
<francis@robinton.demon.co.uk> wrote:

> Exactly why did you buy a C++ book with a 1996 copyright date?

Looks like the second edition is only nine years old and there is
another interesting title by the same author.  Perhaps he learned
something in that year?

C++ Distilled: A Concise ANSI/ISO Reference and Style Guide: 1/e
� 1997 | ISBN: 0201695871
Ira Pohl

Object-Oriented Programming Using C++: 2/e
� 1997 | ISBN: 0201895501
Ira Pohl

John

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated.    First time posters: Do this! ]

```
 0
Reply John 1/6/2006 9:55:55 AM

```"Francis Glassborow" <francis@robinton.demon.co.uk> wrote in message
news:LFWN\$mVIeGvDFwvZ@robinton.demon.co.uk...
> In article <qh1t83-1on.ln1@satorlaser.homedns.org>, Ulrich Eckhardt
> <eckhardt@satorlaser.com> writes
> >sathya_me wrote:
> >> I have brought a book called "objected-oriented programming using C++"
> >> by "IRA POHL", a "Grady Booch" series.
> >
> >I'd first look up every book I buy at accu.org. This book isn't listed
> >there, so it's hard to tell if it's any good.
>
> It is ten years old (unless there is a very new edition of which I am
> unaware) and so I think we probably dropped the review (which if my
> memory serves me correctly was not exactly good) Perhaps you had not
> noticed that the public reviews do not go back to books published in
> 1996.
>
> And quite apart from the problems with the book as then written, C++ has
> changed immensely over the last decade, not least in that that book was
> written before there was a Standard by an author who did not appear to
> have even noticed what was in the working draft.
>
> Exactly why did you buy a C++ book with a 1996 copyright date?
>

just practice some excesses in oop and c++.Any suggestions? Or may
be I must try special edition of Bjarne Stroustrup first?

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated.    First time posters: Do this! ]

```
 0
Reply sathya_me 1/6/2006 10:36:58 AM

8 Replies
399 Views

Similar Articles

12/6/2013 8:21:38 PM
page loaded in 49595 ms. (0)