Thread: Recursive function help

  1. #1
    Registered User
    Join Date
    Feb 2017
    Posts
    18

    Recursive function help

    What am i missing for negative power?

    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){
    
    if(p==0)
    return 1;
    
    elseif(n==0)
    return 0;
    
    elseif (n>0)
    {
    return(n*recursion(n,p-1));
    }
    else
    {
    return(1/n)*(recursion(n,p+1));
    }
    }
    Last edited by Salem; 02-27-2017 at 04:51 AM. Reason: Please use paste-as-text when posting code in future

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    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));
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Feb 2017
    Posts
    18
    I need to do excersize next time slowly not to hurry up I didn see that i put n instead of p. Thank you very much for your help.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 3
    Last Post: 02-18-2013, 11:01 PM
  2. Converting recursive function to tail recursive
    By ajacobs365 in forum C Programming
    Replies: 1
    Last Post: 10-30-2011, 08:15 AM
  3. Replies: 1
    Last Post: 12-03-2010, 01:54 AM
  4. Make Recursive function 'Tail-Recursive'
    By dp2452 in forum C Programming
    Replies: 7
    Last Post: 12-04-2009, 10:13 AM
  5. recursive function
    By tonderai76 in forum C++ Programming
    Replies: 11
    Last Post: 04-21-2004, 12:49 PM

Tags for this Thread