CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 1 of 2 12 LastLast
Results 1 to 15 of 27
  1. #1
    Join Date
    Jan 2007
    Posts
    28

    [RESOLVED] help creating a power program..

    hi
    i need help in writing a function
    integerPower(base,exponent) that returns the value base^exponent with assuming that the exponent is a positive, nonzero integer and that the base is an integer. the function integerPower should use for or while loop to control the calculations, without using any math library functions.

  2. #2
    Join Date
    Oct 2002
    Location
    Timisoara, Romania
    Posts
    14,360

    Re: help creating a power program..

    Please see this FAQ.
    Marius Bancila
    Home Page
    My CodeGuru articles

    I do not offer technical support via PM or e-mail. Please use vbBulletin codes.

  3. #3
    Join Date
    Apr 2005
    Location
    Norway
    Posts
    3,934

    Re: help creating a power program..

    I assume this is homework so I wont give you any codes. But, basically you can just make some kind of loop that multiplies base with itself, exponent times.

    What have you got so far?

    - petter

  4. #4
    Join Date
    Jan 2007
    Posts
    28

    Re: help creating a power program..

    i thought of it like this..

    Code:
    int integerPower(int power,int exponent)
    {
    for(i=1;i<=exponent;i++)
    return x^exponent;
    }
    but it is not true ofcourse
    Last edited by elsa; January 19th, 2007 at 06:15 AM.

  5. #5
    Join Date
    Nov 2006
    Location
    Essen, Germany
    Posts
    1,344

    Re: help creating a power program..

    Hi,

    Quote Originally Posted by elsa
    i thought of it like this..

    int integerPower(int power,int exponent)
    {
    for(i=1;i<=exponent;i++)
    return x^exponent;
    }
    but it is not true ofcourse
    How would you calculate 4^5 using pen and paper?

    Guido
    - Guido

  6. #6
    Join Date
    Nov 2006
    Location
    Barcelona - Catalonia
    Posts
    364

    Re: help creating a power program..

    Quote Originally Posted by elsa
    i thought of it like this..

    int integerPower(int power,int exponent)
    {
    for(i=1;i<=exponent;i++)
    return x^exponent;
    }
    but it is not true ofcourse
    ^ is the XOR operation in C++, not for exponentiation!!! Answer the Guido post:
    Quote Originally Posted by GNiewerth
    How would you calculate 4^5 using pen and paper?
    and you'll have the solution.

    Albert.
    Please, correct me. I'm just learning.... and sorry for my english :-)

  7. #7
    Join Date
    Jan 2007
    Posts
    28

    Re: help creating a power program..

    i know the idea..4^5=4*4*4*4*4
    but i dont know how to transfer this into a c++ program..
    how can i stop the loop at the number of the exponent?

  8. #8
    Join Date
    Nov 2006
    Location
    Barcelona - Catalonia
    Posts
    364

    Re: help creating a power program..

    So, with this idea take the code you left here (changing the ^ operation for the correct one), and that's all. Right?

    Well, the return is not in the right place, either :-)

    Albert
    Please, correct me. I'm just learning.... and sorry for my english :-)

  9. #9
    Join Date
    Jan 2007
    Posts
    28

    Re: help creating a power program..

    that is what i thought..

    Code:
    int integerPower(int power,int exponent)
    {
    for(i=1;i<=exponent;i++)
    return x*i;
    }
    is that right?
    and what do u mean by the return is not in the right place? plz explain
    Last edited by elsa; January 19th, 2007 at 06:27 AM.

  10. #10
    Join Date
    Nov 2006
    Location
    Essen, Germany
    Posts
    1,344

    Re: help creating a power program..

    Hi Elsa,

    when you look at your code, what do you think does it do?

    Code:
    int integerPower(int power,int exponent)
    {
    for(i=1;i<=exponent;i++)
    return x*i;
    }
    What is the math done by that function? What results do you get for sample function calls? Maybe you should spend some hours of reading to get familiar with programming before asking.

    Regards,
    Guido
    - Guido

  11. #11
    Join Date
    Nov 2006
    Location
    Barcelona - Catalonia
    Posts
    364

    Re: help creating a power program..

    No, try to know what your function makes?
    Code:
    int integerPower(int power,int exponent)
    {
    	for(i=1;i<=exponent;i++)
    		return x*i;
    }
    Imagine you want to calculate 4^5.
    When your programs goes into integerPower function.

    power = 4 and exponent = 5. Right?

    Inside for:
    First step:
    i = 1;
    x*i = ? What is x? Is undefined, and i variable is also undefined!!! I guess you wanted power.
    Anyway, power*i = power = 4; Rigth?
    And then, there's a return. So, your funtion will return 4, without doing any other step. Is that what you want? Try to think a little bit more. We'll help you.
    And use code tags to put your code. It's easy to read :-)

    Albert.
    Please, correct me. I'm just learning.... and sorry for my english :-)

  12. #12
    Join Date
    Oct 2000
    Location
    London, England
    Posts
    4,773

    Re: help creating a power program..

    if you call return it will exit from the function immediately.

    You need to keep a temporary store of the number.

    Loop counter i must also be declared, although you can actually manage without a loop counter because you can decrement your local copy of the "exponent" parameter until it goes down to 0. I note you don't need to allow for the case where exponent is 0 but it wouldn't do any harm.
    Code:
    int integerPower( int base, int exponent )
    {
       int result = 1;
      // do the loop here
    
       return result;
    }

  13. #13
    Join Date
    Jan 2007
    Posts
    28

    Re: help creating a power program..

    ok..so will that work?
    Code:
    int integerPower(int power,int exponent)
    {
    	for(i=1;i<=exponent;i++)
    integerPower=power*exponent;
    return integerPower;
    }
    Last edited by elsa; January 19th, 2007 at 06:18 AM.

  14. #14
    Join Date
    Oct 2000
    Location
    London, England
    Posts
    4,773

    Re: help creating a power program..

    That is the Pascal way of doing it but in C++ the name of the function is not automatically a variable of the return type.

    I showed you how to create the result variable.

    You should also put any code you post here into [code] [/code] tags

    Your first parameter name should be base. It is a very important part of programming to use meaningful descriptors.

  15. #15
    Join Date
    Nov 2006
    Location
    Barcelona - Catalonia
    Posts
    364

    Re: help creating a power program..

    Can you answer yourself? What do you think?

    First, intergerPower variable is undefined. Error!!
    Second, what this makes?
    Code:
     integerPower=power*exponent;
    integerPower = 4 * 5 = 20
    and then return.
    Is 4^5 = 20? I don't think so.

    Read NMTop40 post!! So, return must be out of for!!!

    Albert.
    Please, correct me. I'm just learning.... and sorry for my english :-)

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
  •  





Click Here to Expand Forum to Full Width

Featured