Thread: problem with function help

  1. #1
    Registered User
    Join Date
    Apr 2007

    Question problem with function help

    I have another problem I've spent a long time trying to solve. I know it's probably really simple but I cannot get it to work.

    Here's the problem:
    Write the definition of a function powerTo , which receives two parameters. The first is a double and the second is an int . The function returns a double .

    If the second parameter is negative, the function returns 0. Otherwise, it returns the value of the first parameter raised to the power of the second.

    Here's my attempt:

    double powerTo(double base, int power)
    int n;
    for (int k = 1; k < n; k++) {
    if (power < 0)
    return base *= power;
    return 0;
    This is the error message I get from codelab:

    Sometimes your code doesn't return a value.
    I really would appreciate someone's help on this, I think it's something pretty easy but I'm just lost on this one.

  2. #2
    Captain - Lover of the C
    Join Date
    May 2005
    I think you need to work out the logic on this one. Let's go through the steps of raising a number to a power. We know that raising to a power means multiplying by itself a number of times
    2^3 = 2 * 2 * 2
    Let's keep in mind that a number to the zeroth power is one
    2^0 = 1
    2^1 = 1 * 2
    2^4 = 1 * 2 *2 * 2 * 2
    So now for the logic of the function you're writing. First thing, check to make sure power is not < 0. If it is < 0 then return 0. Ok. Now we want a double that we are going to return. You need to initialize it to a certain number(you need to figure out which number for yourself). Then run a loop. The for loop you have set up is fine. For each cycle, you will want to multiply your return value not by the power but by the _______. After the loop return the return value. Good luck!
    Don't quote me on that... ...seriously

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 8
    Last Post: 10-29-2008, 06:33 AM
  2. wxWidgets link problem
    By cboard_member in forum C++ Programming
    Replies: 2
    Last Post: 02-11-2006, 02:36 PM
  3. Problem with Visual C++ Object-Oriented Programming Book.
    By GameGenie in forum C++ Programming
    Replies: 9
    Last Post: 08-29-2005, 11:21 PM
  4. Please Help - Problem with Compilers
    By toonlover in forum C++ Programming
    Replies: 5
    Last Post: 07-23-2005, 10:03 AM
  5. Problem with function pointers
    By vNvNation in forum C++ Programming
    Replies: 4
    Last Post: 06-13-2004, 06:49 AM