Whats wrong with this function?

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Oct 2009
Posts: 43
Reputation: UKmason is an unknown quantity at this point 
Solved Threads: 1
UKmason UKmason is offline Offline
Light Poster

Whats wrong with this function?

 
0
  #1
29 Days Ago
  1. double distance (double angle, int mps, double earth_gravity)
  2. {
  3. double earth_distance;
  4. double speed_sqr = mps * mps;
  5.  
  6. earth_distance = speed_sqr * sin(2*angle) / earth_gravity;
  7. return earth_distance;
  8. }

its supposed to come out at = 3831.57
intead it ends up being =3155.52

anyone know what im doing wrong?
Reply With Quote Quick reply to this message  
Join Date: Sep 2009
Posts: 13
Reputation: Cosmin871 is an unknown quantity at this point 
Solved Threads: 0
Cosmin871 Cosmin871 is offline Offline
Newbie Poster
 
0
  #2
29 Days Ago
for what input parameters?
Reply With Quote Quick reply to this message  
Join Date: Oct 2009
Posts: 43
Reputation: UKmason is an unknown quantity at this point 
Solved Threads: 1
UKmason UKmason is offline Offline
Light Poster
 
0
  #3
29 Days Ago
Ill just show you my whole program

  1. #include <iostream>
  2. #include <cmath>
  3. using namespace std;
  4.  
  5. double angtorad (double angle, double pi);
  6. double distance_earth (double angle, int mps, double earth_gravity);
  7. double distance_moon (double angle, int mps, double moon_gravity);
  8. double distance_mars (double angle, int mps, double mars_gravity);
  9.  
  10. int main ()
  11. {
  12.  
  13. int mps;
  14. double angle;
  15. double pi = 3.1415927;
  16. double earth_gravity = 9.81;
  17. double moon_gravity = 1.62;
  18. double mars_gravity = 3.69;
  19.  
  20.  
  21. cout << " *** Program 3 Planetary Trajectory Simulator *** " << endl;
  22. cout << endl;
  23. cout << " Enter the speed of launch (mps) ";
  24. cin >> mps;
  25. cout << " Enter the angle of launch (degrees) " ;
  26. cin >> angle;
  27. cout << endl;
  28. cout << " You have entered " << mps << " mps for the speed and " << angle << " degrees for the angle." << endl;
  29. cout << " " << angle << " degrees is equal to " << angtorad(angle, pi) << " radian(s)." << endl << endl << endl;
  30. cout << " The distance traveled on Earth = " << distance_earth (angle,mps,earth_gravity) << " meters " << endl;
  31. cout << " The distance traveled on Mars = " << distance_mars (angle,mps,mars_gravity) << " meters " << endl;
  32. cout << " The distance traveled on The Moon = " << distance_moon (angle,mps,moon_gravity)<< " meters " << endl;
  33.  
  34. return 0;
  35. }
  36.  
  37.  
  38. double angtorad (double angle, double pi)
  39. {
  40. double radian;
  41. radian = pi * (angle / 180);
  42. return radian;
  43. }
  44.  
  45. double distance_earth (double angle, int mps, double earth_gravity)
  46. {
  47. double earth_distance;
  48. earth_distance = sin(2*angle)*(mps*mps) / earth_gravity;
  49. return earth_distance;
  50. }
  51. double distance_moon (double angle, int mps, double moon_gravity)
  52. {
  53. double moon_distance;
  54. moon_distance = sin(2*angle)*(mps*mps) / moon_gravity;
  55. return moon_distance;
  56. }
  57. double distance_mars (double angle, int mps, double mars_gravity)
  58. {
  59. double mars_distance;
  60. mars_distance = sin(2*angle)*(mps*mps) / mars_gravity;
  61. return mars_distance;
  62. }
Reply With Quote Quick reply to this message  
Join Date: Sep 2009
Posts: 359
Reputation: jonsca is an unknown quantity at this point 
Solved Threads: 43
jonsca jonsca is offline Offline
Posting Whiz
 
0
  #4
29 Days Ago
Return angtorad into a variable, so say double angrad = angtorad(angle,pi); You never actually change angle when you run your function. Use your new value in place of angle in your other functions.
Reply With Quote Quick reply to this message  
Join Date: Oct 2009
Posts: 43
Reputation: UKmason is an unknown quantity at this point 
Solved Threads: 1
UKmason UKmason is offline Offline
Light Poster
 
0
  #5
29 Days Ago
is that going to fix the math problem? of why i get about 800 meters short?
Reply With Quote Quick reply to this message  
Join Date: Oct 2009
Posts: 43
Reputation: UKmason is an unknown quantity at this point 
Solved Threads: 1
UKmason UKmason is offline Offline
Light Poster
 
0
  #6
29 Days Ago
I dont really understand what you're suggesting?
Reply With Quote Quick reply to this message  
Join Date: Sep 2009
Posts: 359
Reputation: jonsca is an unknown quantity at this point 
Solved Threads: 43
jonsca jonsca is offline Offline
Posting Whiz
 
0
  #7
29 Days Ago
I didn't check your math....give it a try, as it stands you are not converting your degrees to radians (except to display it on that one line, but the next line you put the value in degrees into your function).
Reply With Quote Quick reply to this message  
Join Date: Sep 2009
Posts: 359
Reputation: jonsca is an unknown quantity at this point 
Solved Threads: 43
jonsca jonsca is offline Offline
Posting Whiz
 
0
  #8
29 Days Ago
you call angtorad, but you do nothing with the return value except send it to the output stream. just calling your function does not change the value of angle which remains at the value your user had entered, even when you call the other functions with angle in the arguments.
Last edited by jonsca; 29 Days Ago at 10:47 pm.
Reply With Quote Quick reply to this message  
Join Date: Oct 2009
Posts: 45
Reputation: Fbody is an unknown quantity at this point 
Solved Threads: 5
Fbody Fbody is offline Offline
Light Poster
 
0
  #9
29 Days Ago
On line 29 you're converting your input angle to radians TEMPORARILY to display it, but you are not storing the result value any where. As a result, when you call distance_earth(), distance_moon(), and distance_mars(), the angle is still in degrees. You must find a way to do a permanent conversion from Degrees to Radians, then send the Radians version of your angle to the other functions.

Another option is to include the conversion function call directly as an argument to the other functions.
In other words:
  1. cout << " The distance traveled on Earth = " << distance_earth (angle,mps,earth_gravity) << " meters " << endl;
  2. cout << " The distance traveled on Mars = " << distance_mars (angle,mps,mars_gravity) << " meters " << endl;
  3. cout << " The distance traveled on The Moon = " << distance_moon (angle,mps,moon_gravity)<< " meters " << endl;

Would become:
  1. cout << " The distance traveled on Earth = " << distance_earth (angtorad(angle,pi),mps,earth_gravity) << " meters " << endl;
  2. cout << " The distance traveled on Mars = " << distance_mars (angtorad(angle,pi),mps,mars_gravity) << " meters " << endl;
  3. cout << " The distance traveled on The Moon = " << distance_moon (angtorad(angle,pi),mps,moon_gravity)<< " meters " << endl;

Hope that helps.
Last edited by Fbody; 29 Days Ago at 10:52 pm.
Reply With Quote Quick reply to this message  
Join Date: Oct 2009
Posts: 43
Reputation: UKmason is an unknown quantity at this point 
Solved Threads: 1
UKmason UKmason is offline Offline
Light Poster
 
0
  #10
29 Days Ago
Yeah, I know what youre talking about now but thats a problem I have to fix a little further down the road...

Right now I dont understand why "(200^2)*sin(2*35) / 9.81" dosnt equal 3831.57... I did it on the calc and it does

but when I do that same equation in my function I get 3155.52
Reply With Quote Quick reply to this message  
Reply

Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC