ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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.
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:
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.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.