help, how would i change this recursive function

This is a discussion on help, how would i change this recursive function within the C Programming forums, part of the General Programming Boards category; into an iterative one, what steps would you take.. Code: int power (int n){ int (n == 1){ return 2; ...

  1. #1
    Registered User
    Join Date
    Oct 2007
    Posts
    51

    help, how would i change this recursive function

    into an iterative one, what steps would you take..

    Code:
    int power (int n){
      int (n == 1){
         return 2;
      }else{
         return 2*power(n-1);
    }
    }

  2. #2
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,354
    Quote Originally Posted by webznz View Post
    into an iterative one, what steps would you take..

    Code:
    int power (int n){
      int (n == 1){
         return 2;
      }else{
         return 2*power(n-1);
    }
    }
    First, you probably meant "if (n == 1)" not "int (n == 1)".

    Well, figure out what it computes.

    If you pass it 1, it returns 2.
    If you pass it 2, it returns 2 * power(1) = 2 * 2 = 4.
    If you pass it 3, it returns 2 * power(2) = 2 * 2 * power(1) = 2 * 2 * 2 = 8.

    Hopefully it becomes clear what to do.

  3. #3
    abyss - deep C
    Join Date
    Oct 2007
    Posts
    46
    Hi,

    One of the ways is mentioned below:

    Code:
    int power(int n) {
      int value = 1;
    
      while(n > 0)
        value *= 2;
    
      return value;
    }
    Regards
    maverix

  4. #4
    Chinese pâté foxman's Avatar
    Join Date
    Jul 2007
    Location
    Canada
    Posts
    404
    You forgot a little something...
    Code:
    int power(int n) {
      int value = 1;
    
      while(n-- > 0)
        value *= 2;
    
      return value;
    }

  5. #5
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,354
    Quote Originally Posted by foxman View Post
    You forgot a little something...
    Code:
    int power(int n) {
      int value = 1;
    
      while(n-- > 0)
        value *= 2;
    
      return value;
    }
    Congrats, I think you guys just did a homework assignment.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  2. Bisection Method function value at root incorrect
    By mr_glass in forum C Programming
    Replies: 3
    Last Post: 11-10-2005, 08:10 AM
  3. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  4. I need help with passing pointers in function calls
    By vien_mti in forum C Programming
    Replies: 3
    Last Post: 04-24-2002, 10:00 AM
  5. qt help
    By Unregistered in forum Linux Programming
    Replies: 1
    Last Post: 04-20-2002, 09:51 AM

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