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.
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.
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.
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
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Ž
it was pseudocode, intended only to give a nudge in the right direction. you don't need to be condescending.
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) {}
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.
From which the output is: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; }
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.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
@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.