regarding Nth Power of a Number

This is a discussion on regarding Nth Power of a Number within the C++ Programming forums, part of the General Programming Boards category; Hello Frendz,i'm a C++ Student, my Question is: Write a program to Find the Nth Power of a Number using ...

  1. #1
    Registered User
    Join Date
    Sep 2008
    Posts
    1

    regarding Nth Power of a Number

    Hello Frendz,i'm a C++ Student, my Question is:
    Write a program to Find the Nth Power of a Number using a Function.The Function must have 2 as the Default value for power.

    Plz any1 solve this program.

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    So what have you done so far?

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    Registered User
    Join Date
    Sep 2006
    Posts
    230
    Think about it. How do you find the Nth power mathematically? Once you know that, you just need to write some code to implement it.

    If you give it a try and it doesn't work, post your code here and we'll help you out. But no one will help you without you showing some effort first.
    I might not be a pro, but I'm usually right

  4. #4
    3735928559
    Join Date
    Mar 2008
    Location
    RTP
    Posts
    839
    while i < power
    result*=arg

  5. #5
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,066
    Quote Originally Posted by m37h0d View Post
    while i < power
    result*=arg
    Nothing quite like the blind leading the blind.

    While I'm all for the idea of giving the guy who asked us to do his homework bad or incomplete information. Let us remember that there are a lot of other new programmers reading these message boards to try to learn. If you're going to give an answer, complete it... or at least fix the syntax errors in the code you do write.
    Last edited by SlyMaelstrom; 09-09-2008 at 05:36 PM.
    Sent from my iPadŽ

  6. #6
    3735928559
    Join Date
    Mar 2008
    Location
    RTP
    Posts
    839
    it was pseudocode, intended only to give a nudge in the right direction. you don't need to be condescending.

  7. #7
    Registered User valaris's Avatar
    Join Date
    Jun 2008
    Location
    RING 0
    Posts
    507
    Well your function should probably take two inputs, what you want raised, and what power you want it raised to (n). Make a loop like m37 said and multiply your number by itself. Do this n number of times until n is zero. Then you can return this result.

    Also for a default param you just put something like
    Code:
    foo(int r = 2) {}

  8. #8
    Registered User
    Join Date
    Jul 2010
    Location
    Oklahoma
    Posts
    107
    Shameer,

    There are a lot of ways to calculate the power of a number. It depends on the type of numbers involved. Are they members of Z or are they members of R, for example would definitely drive an implementation. With the information you've provided, I'll assume you mean for the base to be a member of Z (integers) and the exponent to be a member of N (natural numbers) and possibly zero.

    In the case of integers, it is customary to use something like the what m37h0d provided. If your default for the base is two then you specify this when you declare the function stub, if any.

    Code:
    #include <iostream>
    
    using namespace std;
    
    int mypow( int base = 2, int exponent = 1 );
    
    int main()
    {
       cout << "mypow() = " << mypow() << endl;
       cout << "mypow( 3, 2 ) = " << mypow( 3, 2 ) << endl;
       
       return 1;
    }
    
    int mypow( int base, int exponent )
    {
       int result = 1;
    
       while( 0 < exponent )
       {
          result *= base;
          exponent--;
       }
       
       return result;
    }
    From which the output is:

    Code:
    mypow() = 2
    mypow( 3, 2 ) = 9
    Notice that every parameter after the first parameter with a default must also be given a default...I thought that was nifty when I was reading about function declaration/definition.

    Also, I took advantage of the fact that the integers passed to the "mypow" function were completely within it's scope and do not cause side effects in the calling function, so I used them to control the loop...lexical scoping is rather nifty.

    If you notice anything else that "looks funny," hit us up. I am confident that some one has at least a giggle for you.

    If this does happen to relate to your graded work, if any, I never got into plagiarism because it is a question of honour. If that isn't enough to complete the premise, I must also point out that you can't take us to the test...or any other controlled environment like a safe room. It's nice to see an answer though...I can appreciate that myself. From what I read that's what these boards are for.... I like your signature a lot SlyMaelstrom. Those terms are sound advice indeed....

    Best Regards,

    New Ink -- Henry

  9. #9
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,451
    @new_ink2001 - please read the post dates (and the FAQs) before digging up dead threads.
    This was from 2008!
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Simple XOR Program
    By dolfaniss in forum C Programming
    Replies: 8
    Last Post: 05-24-2010, 01:27 PM
  2. Problem With My Box
    By HaVoX in forum Tech Board
    Replies: 9
    Last Post: 10-15-2005, 07:38 AM
  3. Is number power of 2?
    By Roaring_Tiger in forum C Programming
    Replies: 2
    Last Post: 08-25-2004, 11:38 AM
  4. Prime number program problem
    By Guti14 in forum C Programming
    Replies: 11
    Last Post: 08-06-2004, 04:25 AM
  5. Random Number problem in number guessing game...
    By -leech- in forum Windows Programming
    Replies: 8
    Last Post: 01-15-2002, 04:00 PM

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