I am trying to write a recursive function that compute a to the n.

here is as far as I have gotten as far as code

#include "stdafx.h"
#include "stdio.h"

int power (int n); // function prototype//


main()
{
    float a;

    int n;

    printf("\n enter values of a an n");

    printf("\n %.4f to the power of % d is %.4f",a,n,power(a,n)); 
}
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));
    }
}

I have 2 errors

cpp(12) : error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
c:\documents and settings\owner\my documents\visual studio 2008\projects\lab 8.3.1\lab 8.3.1\lab 8.3.1.cpp(19) : error C2660: 'power' : function does not take 2 arguments

Recommended Answers

All 16 Replies

If this is a C program then do not code it in a file with *.cpp extension because the compiler may give some wrong error messages. Rename the file to have *.c extension for better compile messages.

line 7: it should be int main() -- you can not leave off the function's return type

compare the differences between lines 4 and 17. They must both have the same parameters.

still having is error cpp(19) : error C2660: 'power' : function does not take 2 arguments

#include "stdafx.h"
#include "stdio.h"

int power (int n); // function prototype//


int main (void)
{
    float a;

    int n;

    printf("\n enter values of a an n");

    printf("\n %.4f to the power of % d is %.4f",a,n,power(a,n)); 
}
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));
    }
}

1. Better follow standard pow pattern: use double type for argument and returned value.
2. Some useful method: extract recursive part to reduce a number of operators in recursion:

static double powrec(double x, int n) {
    return n? x * powrec(x,n-1): 1.0;
}
double powint(double x, int n) {
    if (x == 0.0) /* if n < 0? */
        return 0.0;
    else if (n == 0)
        return 1.0;
    else if (n < 0)
        return powrec(1.0/x,-n);
    return x * powrec(x,n-1);
}

The only test in a recursion instead of 3...

I am still getting the follow error cpp(19) : error C2660: 'power' : function does not take 2 arguments

Could someone please what this means?

Thanks

On line 7 you have:

int power (int n); // function prototype

And later you call your function:

float power(float a, int n)

Hello,

I make the changes and am now getting an abort error when I run the program. I attached a screenshot.

Well, it says about 'n' not being initialised. Try to debug it yourself!

Thank you all for your help. After several tries of debugging I am still having issues initalizing n. Could someone please point me to the line and need to intialize i.

Thanks again

Post entire code please. Once more :)

From the original post

int main (void)
{
    float a;

    int n;

    printf("\n enter values of a an n");

    printf("\n %.4f to the power of % d is %.4f",a,n,power(a,n)); 
}

What's the value of n ? Answer: It just has some random value because it was never initialized to anything. You forgot to get user input using scanf() or something else.

Still getting this error. (19) : error C2660: 'power' : function does not take 2 arguments

#include "stdafx.h"
#include "stdio.h"
 
int power (int n); // function prototype//
 
 
int main (void)
{
    float a;
 
    int n;
 
    printf("\n enter values of a an n");
	scanf ("%f", &n);
    printf("\n %.4f to the power of % d is %.4f",a,n,power(a,n)); 
	scanf ("%f", &a);
}
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));
    }
}

10 hours ago Sci@phy told you about this error.
You wrote:

int power (int n); // function prototype//
int main(void)
{ ...
    printf("\n %.4f to the power of % d is %.4f",a,n,power(a,n)); 
...
} /* the main does not know about this fucntion: no its prototype */
float  power(float a, int n)
{...

At the 1st line you tell (to compiler) that power function has one parameter of int type. Of course, no such function in your code. You tell lies. In printf you call power function with two arguments. The compiler knows that power has one argument only, it sends obvious message to you:
'power' : function does not take 2 arguments.
If you want to call your power function, write correct prototype instead of senseless int power(int) .

It seems you don't understand all ours posts to you...

Now when I run the program it does not run correctly and does compute values. It's aborting the program. I really appreciate all the help and was wondering if anyone had an advice of why it is aborting when I run the program.

#include "stdafx.h"
#include "stdio.h"
 
float power(float a, int n); // function prototype//
 
 
int main (void)
{
    float a;
 
    int n;
 
    printf("\n enter values of a an n");
	scanf ("%f", &n);
    printf("\n %.4f to the power of % d is %.4f",a,n,power(a,n)); 
	scanf ("%f", &a);
}
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));
    }
}

Two problems that I see.
What about your input code? First you ask user to insert "a and n", but using scanf() you first get n, then a. That could be problem.

And on end of int main() add return 0;

You are still trying to print out the value of a before you have retrieved its value from the user via scanf()

Do, I need to put results= a^n. I am still having issues for someone reason. I know there is something I am missing but I cannot put my finger on it.

Thanks!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.