help with K&R2 
Author Message
 help with K&R2

Hi all i am a newbie to c
and have been reading 2ed the c language  by K&R
i am at 1.7 functions and i am looking at the raise an int to a power and
have two questions
heres the prog
#include <stdio.h>
int power(int m,int n);
/* test power function  */
main()
{
  int i;

for (i=0;i<10;++i)
   printf("%d %d %d\n",i power(2,i),power(-3,i));
return 0;

Quote:
}

/*  power raise base to n-th power; n >=0  */
int power(int base, int n)
{
     int i ,p;

    p =1;
   for (i=1;i<=n;++i)
       p = p * base;
return p;

Quote:
}

my questions are 1:
what does i have to do in the for loop p isnt being increased why a  for
loop??
question2:
 where does p come from (declared assigned ) and what has it to do with
working out the power of some thing

sorry if i have missed something most obvious but a iam a new learner

thanks for takeing a moment of your time

regards
aade



Sun, 11 Jan 2004 18:55:06 GMT  
 help with K&R2

Quote:

> Hi all i am a newbie to c
> and have been reading 2ed the c language  by K&R
> i am at 1.7 functions and i am looking at the raise an int to a power and
> have two questions
> heres the prog
> #include <stdio.h>
> int power(int m,int n);
> /* test power function  */
> main()
> {
>   int i;

> for (i=0;i<10;++i)
>    printf("%d %d %d\n",i power(2,i),power(-3,i));

You're missing a comma.
printf("%d %d %d\n", i, power(2, i), power(-3, i));

- Show quoted text -

Quote:
> return 0;
> }

> /*  power raise base to n-th power; n >=0  */
> int power(int base, int n)
> {
>      int i ,p;

>     p =1;
>    for (i=1;i<=n;++i)
>        p = p * base;
> return p;
> }

> my questions are 1:
> what does i have to do in the for loop
> p isnt being increased why a  for loop??

power() could have been written without i

int power(int base, int n)
{
    int p;

    p = 1;
    while(n--)
       p *= base;
    return p;

Quote:
}
> question2:
>  where does p come from (declared assigned )
> and what has it to do with working out the power of some thing

p is where the results of the calculations go.
n is being used to count, and
base is used as a constant, so
the calculations have to go somewhere else.

--
 pete



Sun, 11 Jan 2004 20:02:06 GMT  
 help with K&R2

Quote:
aade writes:
> and have been reading 2ed the c language  by K&R
> i am at 1.7 functions and i am looking at the raise an int to a power and
> have two questions
> heres the prog
> #include <stdio.h>
> int power(int m,int n);
> /* test power function  */
> main()
> {
>   int i;

> for (i=0;i<10;++i)
>    printf("%d %d %d\n",i power(2,i),power(-3,i));
> return 0;
> }

> /*  power raise base to n-th power; n >=0  */
> int power(int base, int n)
> {
>      int i ,p;

>     p =1;
>    for (i=1;i<=n;++i)
>        p = p * base;
> return p;
> }

> my questions are 1:
> what does i have to do in the for loop p isnt being increased why a  for
> loop??
> question2:
>  where does p come from (declared assigned ) and what has it to do with
> working out the power of some thing

> sorry if i have missed something most obvious but a iam a new learner

Both i and p are 'inventions' of the person who wrote the power function.  i
is a counting variable, it provides a means to explicitly determine, when
the code is written,  how many times to execute the loop.  A for loop is the
simplest way to do a loop when you can satisfy the a priori requirement.  i
stands for 'index', a better choice might have been 'count'.  The writers
chose i because this variable often *does* serve as an index.  But at this
point in your learning process, you don't know about indexes yet, that has
to do with arrays.

p is acting as an accumulator.  A better name might have been
'current_power'.  Note that after one execution of the loop, p will be p^1.
And the loop is always executed at least one time.

When I say "a better choice" above it should in no way be construed as
criticism of the book.  It is a fantastic book!  But it is written for
people who already have some notion of programming and the common idioms, as
above.   It would only be a better choice if the book were intended for
absolute beginners.



Sun, 11 Jan 2004 20:21:26 GMT  
 help with K&R2
thanks people for the reply but i think iknow enought to know that p=p *base
will never work out the nth rasied to b power espeacially as p is
initialised to =1 and never changes ???
i am truly stumped
regards
aade


Quote:
> aade writes:

> > and have been reading 2ed the c language  by K&R
> > i am at 1.7 functions and i am looking at the raise an int to a power
and
> > have two questions
> > heres the prog
> > #include <stdio.h>
> > int power(int m,int n);
> > /* test power function  */
> > main()
> > {
> >   int i;

> > for (i=0;i<10;++i)
> >    printf("%d %d %d\n",i power(2,i),power(-3,i));
> > return 0;
> > }

> > /*  power raise base to n-th power; n >=0  */
> > int power(int base, int n)
> > {
> >      int i ,p;

> >     p =1;
> >    for (i=1;i<=n;++i)
> >        p = p * base;
> > return p;
> > }

> > my questions are 1:
> > what does i have to do in the for loop p isnt being increased why a  for
> > loop??
> > question2:
> >  where does p come from (declared assigned ) and what has it to do with
> > working out the power of some thing

> > sorry if i have missed something most obvious but a iam a new learner

> Both i and p are 'inventions' of the person who wrote the power function.
i
> is a counting variable, it provides a means to explicitly determine, when
> the code is written,  how many times to execute the loop.  A for loop is
the
> simplest way to do a loop when you can satisfy the a priori requirement.
i
> stands for 'index', a better choice might have been 'count'.  The writers
> chose i because this variable often *does* serve as an index.  But at this
> point in your learning process, you don't know about indexes yet, that has
> to do with arrays.

> p is acting as an accumulator.  A better name might have been
> 'current_power'.  Note that after one execution of the loop, p will be
p^1.
> And the loop is always executed at least one time.

> When I say "a better choice" above it should in no way be construed as
> criticism of the book.  It is a fantastic book!  But it is written for
> people who already have some notion of programming and the common idioms,
as
> above.   It would only be a better choice if the book were intended for
> absolute beginners.



Sun, 11 Jan 2004 21:19:15 GMT  
 help with K&R2

Quote:


> > Hi all i am a newbie to c
> > and have been reading 2ed the c language  by K&R
> > i am at 1.7 functions and i am looking at the raise an int to a power and
> > have two questions
> > heres the prog
> > #include <stdio.h>
> > int power(int m,int n);
> > /* test power function  */
> > main()
> > {
> >   int i;

> > for (i=0;i<10;++i)
> >    printf("%d %d %d\n",i power(2,i),power(-3,i));

> You're missing a comma.
> printf("%d %d %d\n", i, power(2, i), power(-3, i));

> > return 0;
> > }

> > /*  power raise base to n-th power; n >=0  */
> > int power(int base, int n)
> > {
> >      int i ,p;

> >     p =1;
> >    for (i=1;i<=n;++i)
> >        p = p * base;
> > return p;
> > }

> > my questions are 1:
> > what does i have to do in the for loop
> > p isnt being increased why a  for loop??

> power() could have been written without i

> int power(int base, int n)
> {
>     int p;

>     p = 1;
>     while(n--)
>        p *= base;
>     return p;
> }

Change that to:

int power(int base, int n)
{
    int p;

    p = 1;
    if (p > 0)
        while(n--)
            p *= base;
    return p;

Quote:
}

--
 pete


Sun, 11 Jan 2004 21:30:40 GMT  
 help with K&R2

Quote:

> /*  power raise base to n-th power; n >=0  */
> int power(int base, int n)
> {
>      int i ,p;

>     p =1;
>    for (i=1;i<=n;++i)
>        p = p * base;
> return p;
> }

> what does i have to do in the for loop p isnt being increased

Yerwhat? What do you mean, isn't being increased? What do you think this
line does:
Quote:
>        p = p * base;

It multiplies p by base, thereby increasing its magnitude (provided, of
course, abs(base)>1...).

Richard



Sun, 11 Jan 2004 21:28:54 GMT  
 help with K&R2
Quote:

> Hi all i am a newbie to c
> and have been reading 2ed the c language  by K&R
> i am at 1.7 functions and i am looking at the raise an int to a power and
> have two questions
> heres the prog
> #include <stdio.h>
> int power(int m,int n);
> /* test power function  */
> main()
> {
>   int i;

> for (i=0;i<10;++i)
>    printf("%d %d %d\n",i power(2,i),power(-3,i));

                         ^^
Oops! Missed a comma ;) !

Quote:
> return 0;
> }

> /*  power raise base to n-th power; n >=0  */
> int power(int base, int n)
> {
>      int i ,p;

>     p =1;
>    for (i=1;i<=n;++i)
>        p = p * base;
> return p;
> }

> my questions are 1:
> what does i have to do in the for loop p isnt being increased why a  for
> loop??

Everything! You may consider i as some sort of counter that tells you where
you are in the process of repeated multiplication (to obtain the power). As
for p, it /is/ being increased in every iteration of the loop -- in fact, it
increases by a factor of base, every iteration.

Quote:
> question2:
>  where does p come from (declared assigned ) and what has it to do with
> working out the power of some thing

The first statement in the function power() declares p, and the second assigns
the value 1 to it. By repeatedly multiplying p by the base (n times), you get
base raised to the power n (from the definition of exponentiation).

Quote:

> sorry if i have missed something most obvious but a iam a new learner

> thanks for takeing a moment of your time

> regards
> aade

I don't know if I've understood your question properly, but anyway, I hope
this helps. Perhaps you may want to look at K&R2, Section 1.3 again.

Cheers!

Regards,

M.Q.



Sun, 11 Jan 2004 21:35:55 GMT  
 help with K&R2

Quote:

> thanks people for the reply but i think iknow enought to know
> that p=p *base will never work out the nth rasied to b power
> espeacially as p is initialised to =1 and never changes ???
> i am truly stumped

Should be "base raised to the nth power".
If you had compiled and run the program,
you would know whether or not it works.
If you can't compile and run C programs,
then there's not much you can do here.

--
 pete



Sun, 11 Jan 2004 21:39:57 GMT  
 help with K&R2

Quote:



> > > Hi all i am a newbie to c
> > > and have been reading 2ed the c language  by K&R
> > > i am at 1.7 functions and i am looking at the raise an int to a power and
> > > have two questions
> > > heres the prog
> > > #include <stdio.h>
> > > int power(int m,int n);
> > > /* test power function  */
> > > main()
> > > {
> > >   int i;

> > > for (i=0;i<10;++i)
> > >    printf("%d %d %d\n",i power(2,i),power(-3,i));

> > You're missing a comma.
> > printf("%d %d %d\n", i, power(2, i), power(-3, i));

> > > return 0;
> > > }

> > > /*  power raise base to n-th power; n >=0  */
> > > int power(int base, int n)
> > > {
> > >      int i ,p;

> > >     p =1;
> > >    for (i=1;i<=n;++i)
> > >        p = p * base;
> > > return p;
> > > }

> > > my questions are 1:
> > > what does i have to do in the for loop
> > > p isnt being increased why a  for loop??

> > power() could have been written without i

> > int power(int base, int n)
> > {
> >     int p;

> >     p = 1;
> >     while(n--)
> >        p *= base;
> >     return p;
> > }

> Change that to:

> int power(int base, int n)
> {
>     int p;

>     p = 1;
>     if (p > 0)
>         while(n--)
>             p *= base;
>     return p;
> }

int power(int base, int n)
{
    int p;

    p = 1;
    if (n > 0)
        while(n--)
            p *= base;
    return p;

Quote:
}

Good night.

--
 pete



Sun, 11 Jan 2004 21:42:25 GMT  
 help with K&R2
thanks guys i can now see that p=p*base does infact increase p
i am sorry i must have had a matrix moment
regards
aade



Quote:
> > Hi all i am a newbie to c
> > and have been reading 2ed the c language  by K&R
> > i am at 1.7 functions and i am looking at the raise an int to a power
and
> > have two questions
> > heres the prog
> > #include <stdio.h>
> > int power(int m,int n);
> > /* test power function  */
> > main()
> > {
> >   int i;

> > for (i=0;i<10;++i)
> >    printf("%d %d %d\n",i power(2,i),power(-3,i));
>                          ^^
> Oops! Missed a comma ;) !

> > return 0;
> > }

> > /*  power raise base to n-th power; n >=0  */
> > int power(int base, int n)
> > {
> >      int i ,p;

> >     p =1;
> >    for (i=1;i<=n;++i)
> >        p = p * base;
> > return p;
> > }

> > my questions are 1:
> > what does i have to do in the for loop p isnt being increased why a  for
> > loop??

> Everything! You may consider i as some sort of counter that tells you
where
> you are in the process of repeated multiplication (to obtain the power).
As
> for p, it /is/ being increased in every iteration of the loop -- in fact,
it
> increases by a factor of base, every iteration.

> > question2:
> >  where does p come from (declared assigned ) and what has it to do with
> > working out the power of some thing

> The first statement in the function power() declares p, and the second
assigns
> the value 1 to it. By repeatedly multiplying p by the base (n times), you
get
> base raised to the power n (from the definition of exponentiation).

> > sorry if i have missed something most obvious but a iam a new learner

> > thanks for takeing a moment of your time

> > regards
> > aade

> I don't know if I've understood your question properly, but anyway, I hope
> this helps. Perhaps you may want to look at K&R2, Section 1.3 again.

> Cheers!

> Regards,

> M.Q.



Sun, 11 Jan 2004 22:13:05 GMT  
 
 [ 10 post ] 

 Relevant Pages 

1. help in K&R2

2. Help->Can't Compile Ex in pp.140, K&R2

3. K&R1 vs. K&R2

4. Newbie getting runtime Exception (STATUS_ACCESS_VIOLATION) trying to implement Ex. 2-4 from K&R2

5. K&R2's implicit declaration of exit()

6. New K&R2 exercise: fix getint

7. Have K&R2, looking for simpler resource on pointers

8. In the reference manual of K&R2 (2)

9. k&r2 question 4.3

10. k&r2 advice

11. k&r2 question

12. One more trivial question on K&R2?

 

 
Powered by phpBB® Forum Software