Need Dev C++ Help

closed account (Doj23TCk)
How would you write this code using a recursive function?

#include <stdio.h>
double power(double a, int b);
int main()
{
double x, powr;
int n;
printf("Enter a number and the integer base and then the power\n");
while (scanf("%lf%d", &x, &n) == 2)
{
powr = power(x,n);
printf("%.3g to the power %d is %.5g\n", x, n, powr);
printf("Enter the next pair of numbers\n");

}
return 0;
}
double power(double a, int b)
{
double powr = 1;
int i;

if (b == 0)
{
if (a == 0)
printf("0 to the 0 undefined; using 1 as the value\n");
powr = 1.0;
}
else if (a == 0)
powr = 0.0;
else if (b > 0)
for(i = 1; i <= b; i++)
powr *= a;
else
powr = 1.0 / power(a, - b);
return powr;
}
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
double power( double a, unsigned int b ) // logarithmic complexity
{
    if( b == 0 )
    {
        // https://en.wikipedia.org/wiki/NaN
        if( a == 0 ) return 0 / 0.0  ; // NaN (not a number)
        else return 1 ;
    }

    else if( b == 1 ) return a ;

    else return power( a, b/2 ) * power( a, (b+1)/2 ) ; // O(log N)
}

double power_bf( double a, unsigned int b ) // linear complexity
{
    if( b == 0 )
    {
        if( a == 0 ) return 0 / 0.0  ; // NaN (not a number)
        else return 1 ;
    }

    else return a * power_bf( a, b-1 ) ; // O(N)
}
closed account (Doj23TCk)
I tired it out but I kept on getting some errors. So I played around with it and still didn't get anything.
You may want to post your code, accompanied by the text of the error messages that you received.
closed account (Doj23TCk)
wait so the recursive thing that you showed is that something you would replace with the original code so maybe look something like...
#include <stdio.h>
double power(double a, int b);
int main()
{
double x, powr;
int n;
printf("Enter a number and the integer base and then the power\n");
while (scanf("%lf%d", &x, &n) == 2)
{
powr = power(x,n);
printf("%.3g to the power %d is %.5g\n", x, n, powr);
printf("Enter the next pair of numbers\n");

}
return 0;
}
double power( double a, unsigned int b )
{
if( b == 0 )
{

if( a == 0 ) return 0 / 0.0 ;
else return 1 ;
}

else if( b == 1 ) return a ;

else return power( a, b/2 ) * power( a, (b+1)/2 );
}

double power_bf( double a, unsigned int b )
{
if( b == 0 )
{
if( a == 0 ) return 0 / 0.0 ;
else return 1 ;
}

else return a * power_bf( a, b-1 );
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
#include <stdio.h>

double power(double a, unsigned int b); // *** unsigned int

int main()
{
    double x ; // , powr;
    unsigned int n; // *** unsigned int

    puts( "Enter a number (base) and the non-negative integer exponent");
    while( scanf("%lf%u", &x, &n) == 2 ) // %u for unsigned int
    {
        const double powr = power(x,n);
        printf("%.3g to the power %u is %.5g\n", x, n, powr); // %u
        printf("Enter the next pair of numbers\n");
    }
    return 0;
}

double power( double a, unsigned int b )
{
    if( b == 0 )
    {
        if( a == 0 ) return 0 / 0.0 ;
        else return 1 ;
    }

    else if( b == 1 ) return a ;

    else return power( a, b/2 ) * power( a, (b+1)/2 );
}

/*
double power_bf( double a, unsigned int b )
{
    if( b == 0 )
    {
        if( a == 0 ) return 0 / 0.0 ;
        else return 1 ;
    }

    else return a * power_bf( a, b-1 );
}
*/
Topic archived. No new replies allowed.