C language, using recursive rewriting of the power function so that it can calculate negative expone

Updated on technology 2024-04-01
13 answers
  1. Anonymous users2024-02-07

    include recursively overrides the power() function so that it can output negative exponents.

    double power(double n,int p)double pow,s;

    if(n==0&&p ||n==1)

    return n;

    else if(p>0) {

    pow=n*power(n,p-1);

    return pow;

    else if(p<0) {

    s=power(n,-p);There is an error in this place!!

    return 1/s;

    else return 1;

    int main (void)

    double x;

    int n;

    scanf("%lf%d",&x,&n);

    power(x,n);

    printf("%.3lf to the power %d is %.5lf",x,n,power(x,n));

    return 0;

    The landlord should also note that the power of 0 is 1

  2. Anonymous users2024-02-06

    else y=1/x*power(x,n+1);The order of operation is (1 x)*power(x,n+1); x is a negative number, 1 x is meaningless, and it will naturally collapse In addition, the content in double quotation marks of scanf does not need to be commas.

  3. Anonymous users2024-02-05

    int ex(int a)

    This paragraph only sets the condition that a is not equal to 1, that is, the function is nested, but does not take into account the case when a=1, that is, when a=1, it should return 1.

    Replace it with something like this: int ex(int a).

    elsereturn 1;}

  4. Anonymous users2024-02-04

    include Take a look at your environment and what header files are needed.

    int ex(int a)

    int main()

  5. Anonymous users2024-02-03

    b is not assigned an initial value, and the recursive constraints are not perfect. Therefore: 1. When a>1, b in the function ex() is assigned as ex(a-1)*a, and the result is correct; 2. When a=1, b is not assigned, and b outputs a random value; 3. When a<1, since b=ex(a-1)*a, and the end condition is a==1, it will be infinitely recursive and become an endless loop.

    In addition, the function declaration should be outside the function body.

  6. Anonymous users2024-02-02

    fourth line: int ex(int a); Why?

    Function declarations? It should be written outside of the main function.

  7. Anonymous users2024-02-01

    This can also be calculated to the power.

    #include

    main()

    As for your program, you put the penultimate line **.

    pow=n*power_negative(n,q-1);

    change to pow=n*power positive(n,q-1); Try it.

  8. Anonymous users2024-01-31

    Someone has already corrected it for you, so I'll point out your mistake in **.

    if(q>0)

    pow=n*power_negative(n,q-1);

    re=1/pow;

    On the face of it, it's true, however, according to re=1 pow. Then the previous ones should all be positive to the power. For example, a (-3) first loop p1=a*p(a,-2), second loop, p2=a*(a,-1), and a third cycle, while recursion has at least one specific value when a certain value is specific and cannot be called all the time.

    Regardless of the specific value for the time being, the last time we re=1 pow, what is the previous pow??? The logic is confused... You should think about it.

    This program is really redundant to implement with recursion, of course, you are training recursion, it is recommended that you use recursion to practice.

  9. Anonymous users2024-01-30

    Brother: This is because the final return value is not the number you want to return at all, for example, if you change return a to return, the result is still the original result.

    In recursive terms, only the function that runs for the first time can return the value returned for the final result, and the return value of other functions cannot be the most effective function, such as your function.

    Probably the process is like this.

    f( ) I'll change yours a little bit and yours will work.

    #include

    #include

    float f(int i, float a)else }

    int main()

  10. Anonymous users2024-01-29

    You didn't declare the function name once.

  11. Anonymous users2024-01-28

    As a reminder, there is no jump out of the delivery statement, so the cycle continues.

  12. Anonymous users2024-01-27

    In the case of n as an integer, when n is greater than 1, the value of (1 (n-1))+1 n) must be 0, and when 0 is passed into the function, an error like 1 0 will occur.

  13. Anonymous users2024-01-26

    The recursion function, if it is wrong if you don't add return in it, recursion is calculated to the lowest level, and then return with return, layer by layer to return the result!!

Related questions
6 answers2024-04-01

linklist reserve_fei(linklist l,int n)

What does n do. >>>More

2 answers2024-04-01

Recursion, in a nutshell, is when the application calls itself to query and access hierarchical data structures. The use of recursion can make ** more concise and clear and readable (not necessarily for beginners), but because recursion requires a system stack, the space consumption is much larger than that of non-recursive, and if the recursion depth is too large, the system resources may not be enough. >>>More

8 answers2024-04-01

The semicolon is the sign of the end of the statement, but the semicolon is not used after the loop, if, and subfunctions such as long long a (int b), and everything else is used, but two semicolons cannot be added, although it will not cause an error, but it may affect the result.

7 answers2024-04-01

Modify network parameters? Can you elaborate on what you want to change?

8 answers2024-04-01

Method steps.

Open VS2012 - File - New - Project. >>>More