| |||||||
| Programming PHP, Perl, Ruby on Rails, AJAX, HTML, XHTML, CSS, JavaScript, MySQL and any other coding topics. |
![]() |
| | LinkBack | Thread Tools |
| | #1 (permalink) |
| Senior Member | simple recursive function problem Hey I am trying to figure out how to do this I need to make a recursive function which does b^e = b*b^e-1 so far..i have a function that will just compute b^e This is in C by the way...but do it in PHP or any other language...i just need to know how to do this..i can translate it into C ![]() heres the code for just b^e Code: #include<stdio.h>
int intPow(int b,int e); /*Function prototype*/
int main(){
int b,e;
printf("Please enter two integers: ");
scanf("%d%d",&b,&e);
printf("%d to the power of %d is %d\n",b,e,intPow(b,e));
}
int intPow(int b,int e){
int power = 1;
for(int i=1;i<=e;i++){
power *= b;
}
return power;
}
|
| |
| | #2 (permalink) |
| Senior Member | What are you actually trying to do here? Are you trying to find the values of b and e programmatically? If I remember my maths correctly (and I am possibly wrong due to precedence etc) b is actually 1 (and I suspect e might be).
__________________ Manage your portfolio using my new Domain Portfolio Management script. Securing Your Domain Name From Theft |
| |
| | #3 (permalink) |
| Senior Member | no b and e are two integers that a user will input so for example..if the user inputs b = 2 e = 4 the program will do 2^4 but i have to do it without using the "power" function already provided in the C library...and i have to use recursive function in a way: 2^4 = 2*2^3 in the program above..it does 2^4 ...i have to break it and call that program again from the inside [recursive] but the problem here is that i dont know what the stop condition should be, since i only break it once.. |
| |
| | #4 (permalink) |
| Senior Member | so basically you need to do a recursive function that that in your instance will do 2*2*2*2. If that is the case it would be something like the following in PHP:- PHP Code:
__________________ Manage your portfolio using my new Domain Portfolio Management script. Securing Your Domain Name From Theft |
| |
| | #6 (permalink) |
| Senior Member | I gave him a php version so that he could translate it into c. If you look through his posts he does suggest that. Having working code in another language makes it easier to translate it into another language as the concepts are the same (in fact in this case a lot of the syntax and constructs will be the same as well)
__________________ Manage your portfolio using my new Domain Portfolio Management script. Securing Your Domain Name From Theft |
| |
| | #7 (permalink) |
| Senior Member | Isn't there a C function like Math.pow() or Int.pow()? Jason
__________________ Web Development |
| |
| | #8 (permalink) |
| Senior Member | yes there is but he has been asked to do it without using any c built in functions.
__________________ Manage your portfolio using my new Domain Portfolio Management script. Securing Your Domain Name From Theft |
| |
![]() |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| |