# Thread: Help with a recursive function that computes a to the n

1. ## Help with a recursive function that computes a to the n

Hello!

I have been working on this for several days now and the program has no errors but it does not calculate. Here's the code:
Code:
```#include "stdafx.h"
#include "stdio.h"

float power(float a, int n); // function prototype//

int main (void)
{
float a;

int n;

printf("\n enter a value of a");
scanf ("&#37;f", &a);
printf("\n enter value of n");
scanf ("%f", &n);
printf("\n %.4f to the power of % d is %.4f",a,n,power(a,n));

return 0;
}
float  power(float a, int n)
{

if (a==0)
{
return 0;

}
else if(n==0)
{
return 1;

}
else if (n>0)
{
return( a* power(a,n-1));
}
else
{
return ((1/a)*power(a,n+1));
}
}```

2. What's seems to be the problem? What is the output?

3. >> return ((1/a)*power(a,n+1));

Really?

The other problem with doing it this way is that your order of operations is out of whack too.

4. Originally Posted by master5001
>> return ((1/a)*power(a,n+1));

Really?

The other problem with doing it this way is that your order of operations is out of whack too.
No that looks right. a^-3 = (1/a)*a^-2 = (1/a)*(1/a)*a^-1=(1/a)*(1/a)*(1/a)*1.

He could compute 1/(a^n) in that final condition, but this should work almost as well.

5. Ah ok, fair enough, it is going "less than 0." Though I am still going to point out that one could theoretically be compounding floating point errors. I would still only do my division once to minimize floating point errors. But I am also one of those crazy types of guys who would typically subtract 1.0f from a floating value too....

6. Yes, that's why I said almost as well. But if the concern is precision, then the first thing to fix is to change float to double.

7. Either way, I am not spotting anything that would cause any sort of unusual returns. I would use a double for this. And even with a double I would still do: return 1.0 / power(a, -n); But that is just me.

8. When you're inputting n, being an integer, you should use

scanf ("&#37;d", &n);
scanf ("%f", &n);

[sound of "doh!!"]

9. ## The issue is when you run the program.

Originally Posted by C_ntua
What's seems to be the problem? What is the output?
The problem is that the program does not seem to have the correct output. I attached a screenshot to explain what I mean!

Thanks!

10. Originally Posted by nonoob
When you're inputting n, being an integer, you should use

scanf ("%d", &n);
scanf ("%f", &n);

[sound of "doh!!"]

Uh oh... Todd has some competition for the title of Ol' Eagle Eye. That looks like reason enough for your code to be broken by my count.

Being in a good mood today, here is a recap of all issues we have addressed.

Suggestions implemented:
Code:
```#include "stdafx.h"
#include "stdio.h"

float power(float a, int n); // function prototype//

int main (void)
{
float a;
int n;

printf("\n enter a value of a");
scanf ("%f", &a);
printf("\n enter value of n");
scanf ("%d", &n);
printf("\n %.4f to the power of % d is %.4f",a,n,power(a,n));

return 0;
}
float  power(float a, int n)
{
/* I put the numbers in float notation. Not a big thing. I am nit-picking. */
if (a==0.0f)
{
return 0.0f;

}
else if(n==0)
{
return 1.0f;
}
else if (n>0)
{
return( a* power(a,n-1));
}
else
{
return (1.0f / power(a,-n)); // 1 extra recursion. 1 division
}
}```

11. Thank you all for your help! Now, what can I do to turn this into a non-recursive function.

12. Write it in a non-recursive way. If only there was a way to repeat a certain group of code while some condition remains true....

13. Ooooh ooooh ooooh pick me pick me!!!

Gotos?

14. Originally Posted by nonoob
Ooooh ooooh ooooh pick me pick me!!!

Gotos?
Now, now, Johnny, you know what happens when you say ... that word.

/me throws tennis ball at head.

Anyone else?

15. Here is the code I am trying to compute for a non- recursive function now!
#include "stdafx.h"
#include "stdio.h"

double mypower(float a, int n); // function prototype//

int main (void)
{
float a;
int n;
int power;

printf("\n enter a value of a\n");
scanf ("&#37;d", &a);
printf("\n enter value of n\n");
scanf ("%d", &n);
power=mypower (a,n);
printf("The result is %d\n", power);

return 0;
}
double power(float a, int n)
{
if (a==0.0)
{
return 0.0;

}
else if(n==0)
{
return 1.0;
}
else if (n>0)
{
return( a* mypower(a,n-1));
}
else
{
return (1.0/ mypower(a,-n));
}
}

The error is error LNK2019: unresolved external symbol "double __cdecl mypower(float,int)" (?mypower@@YANMH@Z) referenced in function _main
C:\Documents and Settings\Owner\My Documents\Visual Studio 2008\Projects\lab 8.3.2\Debug\lab 8.3.2.exe : fatal error LNK1120: 1 unresolved externals