help with a recursiv programm

This is a discussion on help with a recursiv programm within the C Programming forums, part of the General Programming Boards category; Okay guys this is a subject that is a bit confusing to me... so just take it easy ... Write ...

  1. #1
    Registered User datainjector's Avatar
    Join Date
    Mar 2002
    Posts
    356

    help with a recursiv programm

    Okay guys this is a subject that is a bit confusing to me... so just take it easy ...

    Write a recursive function power(base, exponent) that when invoked returns
    base ,exponent
    for example, power(3,4 )= 3*3*3*3.Assume that exponent is an integer greater than or equal to 1.And the termination condition occurs when exponent is equal to 1 because base 1 = base..

    Code:
    #include <stdio.h>
    #include <math.h>
    
    int exponent ( int ,int ); //fucntion protoype
    
    int main()
    {
        int cnt, result;
        result = exponent (cnt, result );
        for ( cnt =2; cnt <= 10; cnt++ ){
            printf ("%d! = %d\n", cnt , result );
                       }
    
    
          system("PAUSE");
          return 0;
    }
    
    int exponent ( int a, int b )
    {
    
     if ( a <= 1 )
     return 1;
     else
         return pow( (a*a),2);
       }

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,830
    Something like

    Code:
    int exponent ( int a, int b )
    {
      if ( b <= 1 )
        return 1;
      else
         return a * exponent(a,b-1);
    }

  3. #3
    still a n00b Jaguar's Avatar
    Join Date
    Jun 2002
    Posts
    187
    When b=0...exponent(a,b) is still 1
    so my codes

    int exponent(int a,int b)
    {
    return b?a*exponent(a,b-1):1;
    }

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    When b=0...exponent(a,b) is still 1
    so my codes

    int exponent(int a,int b)
    {
    return b?a*exponent(a,b-1):1;
    }
    Which requires one more function call than Salem's version. While this may not seem like a lot, if this is a function that were used many times, you would end up with unneeded overhead.

    Quzah.
    Hope is the first step on the road to disappointment.

  5. #5
    Registered User pinko_liberal's Avatar
    Join Date
    Oct 2001
    Posts
    284
    Originally posted by Salem
    Something like

    Code:
    int exponent ( int a, int b )
    {
      if ( b <= 1 )
        return 1;
      else
         return a * exponent(a,b-1);
    }
    shouldnt you return a when b==1 rather than 1

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,830
    > shouldnt you return a when b==1 rather than 1

    But I just copy/pasted the original author's code and made it recursive. Making it produce the right answer is an easy exercise for the reader

  7. #7
    still a n00b Jaguar's Avatar
    Join Date
    Jun 2002
    Posts
    187
    Originally posted by Salem
    int exponent ( int a, int b )
    {
    if ( b <= 1 )
    return 1;
    else
    return a * exponent(a,b-1);
    }
    Be careful this code should be changed to
    Code:
    {
        if (b==1)
          return a; // ****not return 1
        else
          return a*exponent(a,b-1);
    }
    You can return 1, if you check if (b==0).
    Is that right?
    But if I do it my own way, I will never use recursive programming for this case.
    Last edited by Jaguar; 07-20-2002 at 11:03 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. HexDump Programm....
    By Kdar in forum C Programming
    Replies: 4
    Last Post: 10-03-2006, 06:13 AM
  2. programm will not start
    By keeper in forum C++ Programming
    Replies: 11
    Last Post: 07-03-2006, 07:02 AM
  3. printing with a c programm
    By -11Reaper11- in forum C++ Programming
    Replies: 3
    Last Post: 04-30-2006, 11:31 AM
  4. windowsAPI programm
    By datainjector in forum Windows Programming
    Replies: 8
    Last Post: 03-11-2003, 11:08 PM
  5. I am looking for a programm!
    By Unregistered in forum C Programming
    Replies: 1
    Last Post: 04-10-2002, 12:51 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21