| | |
powers in c++
![]() |
•
•
Join Date: May 2007
Posts: 15
Reputation:
Solved Threads: 0
i need to know how to use powers in c++. so if i enter to and then 3 for the power i get 8.
this is what i have so far
the function power is the part im having problems with i dont know what to put in it
thank you in advance
this is what i have so far
C++ Syntax (Toggle Plain Text)
#include <iostream> using namespace std; double power(double,int); int main() { double n; int p; cout << "Enter a number "; cin >> n; cout << "Enter a power "; cin >>p; cout << power(n,p); return 0; } double power(double n, int p) { }
thank you in advance
Last edited by tralfas; Jul 9th, 2007 at 3:54 pm.
•
•
Join Date: May 2007
Posts: 15
Reputation:
Solved Threads: 0
C++ Syntax (Toggle Plain Text)
double power( double n, int p ) { double result = 1.0; for(int j=0; j<p; j++) result *= n; return result; }
Last edited by tralfas; Jul 9th, 2007 at 4:30 pm.
>ok i got the loop i just dont understand it
Translation: I ganked this code, but I need to know how it works to get credit for my homework.
>can some one explain it quick
What don't you understand? It's a simple loop that counts p times and continually updates the result by multiplying n into it.
Translation: I ganked this code, but I need to know how it works to get credit for my homework.

>can some one explain it quick
What don't you understand? It's a simple loop that counts p times and continually updates the result by multiplying n into it.
In case you were wondering, yes, I do hate you.
You might, for the sake of clarity, want to chage
result *= n; to result = result * n; *Voted best profile in the world*
•
•
•
•
C++ Syntax (Toggle Plain Text)
double power( double n, int p ) { double result = 1.0; for(int j=0; j<p; j++) result *= n; return result; }
Then the loop runs 3 times.
Each time it runs the result is multiplied by 2.
result = result * n;
This statement is executed p times multiplying the result with n.
This is how the power function works.
Ubuntu Linux User #25732
![]() |
Similar Threads
- calculataing large powers in C++ (C++)
- Machine Powers Down Before POSTing (Troubleshooting Dead Machines)
- powers of two, recursion. (C)
- have you ever abused ur powers? (Geeks' Lounge)
- Monitor powers down (Windows 95 / 98 / Me)
Other Threads in the C++ Forum
- Previous Thread: ComboBox
- Next Thread: Reducing floating point numbers.
Views: 5024 | Replies: 8
| Thread Tools | Search this Thread |
Tag cloud for C++
algorithm api array arrays assignment beginner binary browser c++ c/c++ calculator char class classes code compile compiler console constructor conversion convert count delete desktop dll dynamic encryption error file files filestream fstream function functions game givemetehcodez graph gui homework i/o iamthwee input int lazy library linked-list linker list loop loops math matrix member memory network newbie number object objects opengl operator output parameter pointer pointers problem program programming project random read recursion recursive reference server sort sorting spoonfeeding string strings struct student studio template templates text time tree undefined variable vc++ vector video visual visualstudio void win32 window windows winsock






