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.

Printable View

- 09-09-2008Shameerregarding 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. - 09-09-2008matsp
So what have you done so far?

--

Mats - 09-09-2008Abda92
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. - 09-09-2008m37h0d
while i < power

result*=arg - 09-09-2008SlyMaelstrom
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. - 09-09-2008m37h0d
it was pseudocode, intended only to give a nudge in the right direction. you don't need to be condescending.

- 09-09-2008valaris
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 likeCode:`foo(int r = 2) {}`

- 08-11-2010new_ink2001
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;

}

Code:`mypow() = 2`

mypow( 3, 2 ) = 9

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 - 08-11-2010Salem
@new_ink2001 - please read the post dates (and the FAQs) before digging up dead threads.

This was from 2008!