Even the most basic of debug actions would be to put some printf statements in the code.
With your original code, it's plainly obvious that it recurses indefinitely until it all blows up.
Code:
//power.c--raises numbers to integer powers
#include<stdio.h>
double recursion(double n, int p);
int main(void)
{
double x, xpow;
int exp;
printf("Enter a number and the positive integer power");
printf("to which\n the number will be raised.Enter q");
printf("to quit.\n");
while (scanf("%lf%d", &x, &exp) == 2) {
xpow = recursion(x, exp);
printf("%.3g to the power %d is %.5g\n", x, exp, xpow);
printf("Enter next pair of numbers or q to quit.\n");
}
printf("Hope you enjoyed this power trip--bye!\n");
return 0;
}
double recursion(double n, int p)
{
double result = 0;
printf("DEBUG: n=%f, p=%d, result=%f\n", n, p, result);
if (p == 0)
result = 1;
else if (n == 0)
result = 0;
else if (p > 0) { //!! was n
result = (n * recursion(n, p - 1));
} else {
result = (1 / n) * (recursion(n, p + 1));
}
printf("DEBUG: n=%f, p=%d, result=%f\n", n, p, result);
return result;
}
I guess the next lesson would be to use meaningful variable names as well.
double recursion(double value, int power)
It would have been rather more obvious that this just didn't read correctly.
Code:
else if ( number > 0 )
result = (number * recursion(number, power - 1));