From: sathya_me on
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! ]

From: Ulrich Eckhardt on
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! ]

From: Francis Glassborow on
In article <43bb7811$0$15791$14726298(a)news.sunsite.dk>, sathya_me
<sathyashrayan(a)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! ]

From: Meador Inge on
> 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! ]

From: Dilip on
Francis Glassborow wrote:
> In article <43bb7811$0$15791$14726298(a)news.sunsite.dk>, sathya_me
> <sathyashrayan(a)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! ]

 |  Next  |  Last
Pages: 1 2
Prev: Tree container library
Next: Calling C++ from Fortran