powers in c++

Reply

Join Date: May 2007
Posts: 15
Reputation: tralfas is an unknown quantity at this point 
Solved Threads: 0
tralfas tralfas is offline Offline
Newbie Poster

powers in c++

 
0
  #1
Jul 9th, 2007
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

  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 3:54 pm.
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 8,313
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 824
Team Colleague
Narue's Avatar
Narue Narue is online now Online
Code Goddess

Re: powers in c++

 
0
  #2
Jul 9th, 2007
Have you tried multiplying p n times? Do you know about loops yet?
In case you were wondering, yes, I do hate you.
Reply With Quote Quick reply to this message  
Join Date: May 2007
Posts: 15
Reputation: tralfas is an unknown quantity at this point 
Solved Threads: 0
tralfas tralfas is offline Offline
Newbie Poster

Re: powers in c++

 
0
  #3
Jul 9th, 2007
  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 4:30 pm.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 5,321
Reputation: iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold 
Solved Threads: 384
Featured Poster
iamthwee's Avatar
iamthwee iamthwee is offline Offline
Posting Expert

Re: powers in c++

 
0
  #4
Jul 9th, 2007
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.
*Voted best profile in the world*
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 8,313
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 824
Team Colleague
Narue's Avatar
Narue Narue is online now Online
Code Goddess

Re: powers in c++

 
0
  #5
Jul 9th, 2007
>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.
In case you were wondering, yes, I do hate you.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 5,321
Reputation: iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold 
Solved Threads: 384
Featured Poster
iamthwee's Avatar
iamthwee iamthwee is offline Offline
Posting Expert

Re: powers in c++

 
0
  #6
Jul 9th, 2007
You might, for the sake of clarity, want to chage


result *= n;   to  result = result * n;
*Voted best profile in the world*
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 8,313
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 824
Team Colleague
Narue's Avatar
Narue Narue is online now Online
Code Goddess

Re: powers in c++

 
0
  #7
Jul 9th, 2007
>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.
In case you were wondering, yes, I do hate you.
Reply With Quote Quick reply to this message  
Join Date: May 2007
Posts: 15
Reputation: tralfas is an unknown quantity at this point 
Solved Threads: 0
tralfas tralfas is offline Offline
Newbie Poster

Re: powers in c++

 
0
  #8
Jul 9th, 2007
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
Reply With Quote Quick reply to this message  
Join Date: Jul 2007
Posts: 127
Reputation: krnekhelesh is an unknown quantity at this point 
Solved Threads: 3
krnekhelesh's Avatar
krnekhelesh krnekhelesh is offline Offline
Junior Poster

Re: powers in c++

 
0
  #9
Jul 10th, 2007
Originally Posted by tralfas View Post
  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.
Ubuntu Linux User #25732
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the C++ Forum


Views: 5024 | Replies: 8
Thread Tools Search this Thread



Tag cloud for C++
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2010 DaniWeb® LLC