Page 1 of 2 12 LastLast
Results 1 to 10 of 15
  1. #1
    What's that...? mesman00's Avatar
    Join Date
    Nov 2001
    Location
    USA
    Posts
    1,425

    question for the c++ programmers- recursion

    not really emucoding... but nonetheless i need help...
    damn, this is a simple problem i'm havin in c++ with recursion. its wicked pissing me off, so i decided to ask here. i need to write a program that asks the user for a number and a power. Then, i need to write a recursive function that takes the number the user entered and raises it to the power the user entered. For example, if the user enters a number of 2 and a power of 4, the function will return 16. now, help me please! i've been goin at it for a while, and i just can't seem to come up with it. here's the code i have, it's really just the main function, because the only other one is the power function (where the recursion will occur), the one i'm havin a little trouble with.

    // Exercise 7
    #include <iostream.h>

    int power(int number, int raise);

    int main()
    {
    int number;
    int answer;
    int raise;



    cout << "Enter a number: ";
    cin >> number;
    cout << "Enter a power to raise the number to: ";
    cin >> raise;
    cout << endl;

    answer = power(number, raise);

    cout << number << " raised to the " << raise << " th power is equal to " << answer << endl;

    return 0;
    }

    int power(int number, int raise)
    {
    HELP NEEDED HERE
    }
    <font color="blue">System Specs</font>
    Dual Boot:
    1. Gentoo Linux
    2. Win2k
    Athlon XP Barton 2700
    512 MB PC 2700 DDR Ram
    GF4 Ti4200 AGP 8X 64 MB
    Onboard Sound


    • Advertising

      advertising
      EmuTalk.net
      has no influence
      on the ads that
      are displayed
        
       

  2. #2
    Superman Azimer's Avatar
    Join Date
    Nov 2001
    Location
    USA
    Posts
    837
    Should be something like:

    Code:
    int power(int number, int raise) 
    { 
       if (raise == 0)
          return 1;
       else
         return number * power (number, raise-1);
    }
    if you have power (2, 0) it should return 1...
    if you have power (2, 2) is should return power (2, 2) (which is 2 * power (2, 1) (which is 2 * 2 power(2, 0)) (which is (2 * 2 * 1) ))) I assume you don't need negative squares... :-/ it would just be 1/power(2, abs(raise)) anyway...
    -Azimer

    "I am not a teacher: only a fellow traveler of whom you asked the way.
    I pointed ahead -- ahead of myself as well as of you." (George Bernard Shaw)

  3. #3
    What's that...? mesman00's Avatar
    Join Date
    Nov 2001
    Location
    USA
    Posts
    1,425
    ok, now i got it, i had the first part of this if statement right, i went wrong at else.

    i was trying to break up the else into all different ways, but i did have the function call in the else statement right!
    power (number, raise - 1)
    anyway, thanks azimer, now i can go to bed happy, yet feeling a little stupid because i should have been able to figure that out. oh well, you live and learn.

    just one more thing, the return value on the first part of the if statement in the power function should be zero, no? because if you a raise a number to a zero power, the answer will be 0, not 1.
    Last edited by mesman00; February 26th, 2002 at 09:05.
    <font color="blue">System Specs</font>
    Dual Boot:
    1. Gentoo Linux
    2. Win2k
    Athlon XP Barton 2700
    512 MB PC 2700 DDR Ram
    GF4 Ti4200 AGP 8X 64 MB
    Onboard Sound

  4. #4
    EmuTalk Member
    Join Date
    Dec 2001
    Location
    NH, USA
    Posts
    94
    this should help


    int i, total=1;

    for (i=0;i<power;i++)
    {
    total=total*number;
    }

    return total;
    My Specs:
    Gigabyte Motherboard
    1400 Athlon T-Bird
    512 Mb SDram
    Xtasy 5564 GeForce 2 AGP 64 MB
    40 Gb Western Digital Hd
    Windows 98, XP Pro

  5. #5
    Moderator Hacktarux's Avatar
    Join Date
    Nov 2001
    Location
    France
    Posts
    1,174
    Pheo : you are making an iterative algorithm and not a recursive one.
    And Mesmann : When you raise a number to the zero power the result is 1 !!.

  6. #6
    What's that...? mesman00's Avatar
    Join Date
    Nov 2001
    Location
    USA
    Posts
    1,425
    hah, ur right, im so dumb, i guess i wasn't thinking last night, goes to show that i need to be gettin more sleep!
    <font color="blue">System Specs</font>
    Dual Boot:
    1. Gentoo Linux
    2. Win2k
    Athlon XP Barton 2700
    512 MB PC 2700 DDR Ram
    GF4 Ti4200 AGP 8X 64 MB
    Onboard Sound

  7. #7
    EmuTalk Member
    Join Date
    Dec 2001
    Location
    NH, USA
    Posts
    94
    sleep would be nice, 1g of caffeine is way too much to survive 8 hrs of work w\ no sleep.
    i cannot even remember much of my C++ from high school
    My Specs:
    Gigabyte Motherboard
    1400 Athlon T-Bird
    512 Mb SDram
    Xtasy 5564 GeForce 2 AGP 64 MB
    40 Gb Western Digital Hd
    Windows 98, XP Pro

  8. #8
    -= Clark Kent -X- =- fivefeet8's Avatar
    Join Date
    Mar 2002
    Location
    Somewhere between heaven and hell..
    Posts
    347

    Wink

    All this coding stuff makes me want to start coding again..
    "I hate the art of fighting, but I need to be the king of fighters."
    System Specs: [Intel Q6600@3.2 ghertz][2x2Gig DDR2 1066][EVGA 8800gtx][Asrock Intel SLi Dual SATA2]

    My Online Mall

  9. #9
    What's that...? mesman00's Avatar
    Join Date
    Nov 2001
    Location
    USA
    Posts
    1,425
    then do so!
    <font color="blue">System Specs</font>
    Dual Boot:
    1. Gentoo Linux
    2. Win2k
    Athlon XP Barton 2700
    512 MB PC 2700 DDR Ram
    GF4 Ti4200 AGP 8X 64 MB
    Onboard Sound

  10. #10
    EmuTalk Member
    Join Date
    Mar 2002
    Posts
    1
    int recursion(int num, int power)
    {
    if(power == 0)
    return 1;
    else if(power == 1)
    return num;
    else if(power > 1)
    return recursion(num, power - 1);
    }

Page 1 of 2 12 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •