943,871 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 7353
  • C++ RSS
Jul 9th, 2007
1

powers in c++

Expand Post »
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

C++ Syntax (Toggle Plain Text)
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. double power(double,int);
  5.  
  6. int main()
  7. {
  8. double n;
  9. int p;
  10. cout << "Enter a number ";
  11. cin >> n;
  12. cout << "Enter a power ";
  13. cin >>p;
  14. cout << power(n,p);
  15. return 0;
  16. }
  17.  
  18. double power(double n, int p)
  19. {
  20.  
  21. }
the function power is the part im having problems with i dont know what to put in it

thank you in advance
Last edited by tralfas; Jul 9th, 2007 at 4:54 pm.
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
tralfas is offline Offline
15 posts
since May 2007
Jul 9th, 2007
0

Re: powers in c++

Have you tried multiplying p n times? Do you know about loops yet?
Administrator
Reputation Points: 6442
Solved Threads: 1393
Bad Cop
Narue is offline Offline
11,807 posts
since Sep 2004
Jul 9th, 2007
0

Re: powers in c++

C++ Syntax (Toggle Plain Text)
  1. double power( double n, int p )
  2. {
  3. double result = 1.0;
  4. for(int j=0; j<p; j++)
  5. result *= n;
  6. return result;
  7. }
ok i got the loop i just dont understand it can some one explain it quick
Last edited by tralfas; Jul 9th, 2007 at 5:30 pm.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
tralfas is offline Offline
15 posts
since May 2007
Jul 9th, 2007
0

Re: powers in c++

It's called using a flow chart. Draw one for the code (presuming it works) and all should become clear.

Trace each step with your finger and write the results on paper.
Featured Poster
Reputation Points: 1536
Solved Threads: 431
Posting Expert
iamthwee is offline Offline
5,865 posts
since Aug 2005
Jul 9th, 2007
0

Re: powers in c++

>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.
Administrator
Reputation Points: 6442
Solved Threads: 1393
Bad Cop
Narue is offline Offline
11,807 posts
since Sep 2004
Jul 9th, 2007
0

Re: powers in c++

You might, for the sake of clarity, want to chage


result *= n;   to  result = result * n;
Featured Poster
Reputation Points: 1536
Solved Threads: 431
Posting Expert
iamthwee is offline Offline
5,865 posts
since Aug 2005
Jul 9th, 2007
0

Re: powers in c++

>You might, for the sake of clarity, want to chage
>result *= n; to result = result * n;
Why? It's not like this is a huge stumbling point for beginners.
Administrator
Reputation Points: 6442
Solved Threads: 1393
Bad Cop
Narue is offline Offline
11,807 posts
since Sep 2004
Jul 9th, 2007
0

Re: powers in c++

LOL not really ganked and its not for a class. but i did find the code in the back of the book.

well maybe ganked just a little =P
Reputation Points: 10
Solved Threads: 0
Newbie Poster
tralfas is offline Offline
15 posts
since May 2007
Jul 10th, 2007
0

Re: powers in c++

Click to Expand / Collapse  Quote originally posted by tralfas ...
C++ Syntax (Toggle Plain Text)
  1. double power( double n, int p )
  2. {
  3. double result = 1.0;
  4. for(int j=0; j<p; j++)
  5. result *= n;
  6. return result;
  7. }
Suppose you send the value of n = 2 and power as 3 ie. p=3

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.
Reputation Points: 31
Solved Threads: 3
Junior Poster
krnekhelesh is offline Offline
127 posts
since Jul 2007

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: ComboBox
Next Thread in C++ Forum Timeline: Reducing floating point numbers.





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC