LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 02-05-2013, 12:25 AM   #1
batman4
Member
 
Registered: Jul 2012
Posts: 47

Rep: Reputation: Disabled
Please explain a example from knr book


I am going through this book and i m getting confused with one program which i am copy/pasting below .

Code:
#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;
}
algorithm waht i have understood is:
for i =0
then power function is called power(2,0) =1

then i=1
power(2,1)=2

then i=2
in power(2,2)
first p(2,1) =2
second p(2,2) =4

then i=3
int power(2,3)
first p(2,1) =2
second p(2,2) =4
third p(2,3)=8

it should print for each case .
and by doing this it should generate duplicate copies but it is not happening .
Please explain the reason for same.
 
Old 02-05-2013, 12:46 AM   #2
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,830

Rep: Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308
sorry guy, it should generate duplicate copies of what? Please explain what do you expect!
 
Old 02-05-2013, 02:12 AM   #3
rigor
Member
 
Registered: Sep 2003
Location: 19th moon ................. ................Planet Covid ................Another Galaxy;............. ................Not Yours
Posts: 705

Rep: Reputation: Disabled
Hi batman4!

It's been many years since I had a copy of K&R around. So unfortunately, I can't just look up the example.

As has been mentioned by pan64, from what you've said, it doesn't seem entirely clear, what you're expecting, especially when compared to the code you've shown us.

I'm hoping that the most important thing to you, is to get output similar to what you've shown is. So I've included here, code modified to produce output somewhat like what you've shown us. Even then, the output that you've shown us, doesn't appear to me, to be entirely consistent even with itself. But I hope that what I've shown you here, helps you understand the problem better.

Here's the code after I modified it:

Code:
#include <stdio.h>


int power(int m, int n) ;


/* test power function */
main()
{
    int i ;


    for (i = 0 ; i < 10 ; ++i)
    {
        printf(  "\ni=%d:\n" ,  i  ) ;
        power( 2 ,  i  ) ;
    }

    return 0 ;
}


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


    p = 1 ;

    printf(  "power(%d,0)=%d\n" ,  base ,  p  ) ;

    for (  i = 1 ;  i <= n ; ++i  )
    {
        p = p * base ;
        printf(  "power(%d,%d)=%d\n" ,  base ,  i ,  p  ) ;
    }

    return p ;
}
When I run the program above, it produces the following output:

Code:
                                                                                                                                                                                                                                                                      
i=0:
power(2,0)=1

i=1:
power(2,0)=1
power(2,1)=2

i=2:                                                                                                                                                                                                                                                                  
power(2,0)=1
power(2,1)=2
power(2,2)=4

i=3:
power(2,0)=1
power(2,1)=2
power(2,2)=4
power(2,3)=8

i=4:
power(2,0)=1
power(2,1)=2
power(2,2)=4
power(2,3)=8
power(2,4)=16

i=5:
power(2,0)=1
power(2,1)=2
power(2,2)=4
power(2,3)=8
power(2,4)=16
power(2,5)=32

i=6:
power(2,0)=1
power(2,1)=2
power(2,2)=4
power(2,3)=8
power(2,4)=16
power(2,5)=32
power(2,6)=64

i=7:
power(2,0)=1
power(2,1)=2
power(2,2)=4
power(2,3)=8
power(2,4)=16
power(2,5)=32
power(2,6)=64
power(2,7)=128

i=8:
power(2,0)=1
power(2,1)=2
power(2,2)=4
power(2,3)=8
power(2,4)=16
power(2,5)=32
power(2,6)=64
power(2,7)=128
power(2,8)=256

i=9:
power(2,0)=1
power(2,1)=2
power(2,2)=4
power(2,3)=8
power(2,4)=16
power(2,5)=32
power(2,6)=64
power(2,7)=128
power(2,8)=256
power(2,9)=512
 
Old 02-05-2013, 05:14 AM   #4
PrinceCruise
Member
 
Registered: Aug 2009
Location: /Universe/Earth/India/Pune
Distribution: Slackware64 -Current
Posts: 890

Rep: Reputation: 186Reputation: 186
Quote:
Originally Posted by batman4 View Post
it should print for each case .
and by doing this it should generate duplicate copies but it is not happening .
Please explain the reason for same.
I probably understand why you believe that main should print every time from the power(2,0), it's because the confusing return statement and the for loop in function power().
Now look at this way with proper quotes:-

Quote:
for (i = 1; i <= n; ++i)
{
p = p * base;
}
return p;
It returns the final value after completing the whole loop.

PS : Learning gdb to debug C codes will be a good idea. Lots of things get clear.

Regards.
 
  


Reply

Tags
programing



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Need RH124 and RH135 book, please anybody know where i can download the book for free aderinto Linux - Newbie 5 07-31-2012 08:42 PM
LXer: Book Review: The Official Ubuntu Server Book LXer Syndicated Linux News 0 08-31-2009 03:00 AM
LXer: 'The Book of PF' -- absolutely the newest OpenBSD book available LXer Syndicated Linux News 0 04-10-2008 11:10 AM
Can someone explain to me this code from K&R's 'The C Programming Language' book? frankie_DJ Programming 10 11-25-2006 01:35 PM
LXer: Book Review: The Official Ubuntu Book LXer Syndicated Linux News 0 08-30-2006 04:54 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 03:25 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration