| | |
Whats wrong with this function?
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Oct 2009
Posts: 43
Reputation:
Solved Threads: 1
C++ Syntax (Toggle Plain Text)
double distance (double angle, int mps, double earth_gravity) { double earth_distance; double speed_sqr = mps * mps; earth_distance = speed_sqr * sin(2*angle) / earth_gravity; return earth_distance; }
its supposed to come out at = 3831.57
intead it ends up being =3155.52
anyone know what im doing wrong?
•
•
Join Date: Oct 2009
Posts: 43
Reputation:
Solved Threads: 1
0
#3 29 Days Ago
Ill just show you my whole program
C++ Syntax (Toggle Plain Text)
#include <iostream> #include <cmath> using namespace std; double angtorad (double angle, double pi); double distance_earth (double angle, int mps, double earth_gravity); double distance_moon (double angle, int mps, double moon_gravity); double distance_mars (double angle, int mps, double mars_gravity); int main () { int mps; double angle; double pi = 3.1415927; double earth_gravity = 9.81; double moon_gravity = 1.62; double mars_gravity = 3.69; cout << " *** Program 3 Planetary Trajectory Simulator *** " << endl; cout << endl; cout << " Enter the speed of launch (mps) "; cin >> mps; cout << " Enter the angle of launch (degrees) " ; cin >> angle; cout << endl; cout << " You have entered " << mps << " mps for the speed and " << angle << " degrees for the angle." << endl; cout << " " << angle << " degrees is equal to " << angtorad(angle, pi) << " radian(s)." << endl << endl << endl; cout << " The distance traveled on Earth = " << distance_earth (angle,mps,earth_gravity) << " meters " << endl; cout << " The distance traveled on Mars = " << distance_mars (angle,mps,mars_gravity) << " meters " << endl; cout << " The distance traveled on The Moon = " << distance_moon (angle,mps,moon_gravity)<< " meters " << endl; return 0; } double angtorad (double angle, double pi) { double radian; radian = pi * (angle / 180); return radian; } double distance_earth (double angle, int mps, double earth_gravity) { double earth_distance; earth_distance = sin(2*angle)*(mps*mps) / earth_gravity; return earth_distance; } double distance_moon (double angle, int mps, double moon_gravity) { double moon_distance; moon_distance = sin(2*angle)*(mps*mps) / moon_gravity; return moon_distance; } double distance_mars (double angle, int mps, double mars_gravity) { double mars_distance; mars_distance = sin(2*angle)*(mps*mps) / mars_gravity; return mars_distance; }
•
•
Join Date: Sep 2009
Posts: 359
Reputation:
Solved Threads: 43
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.
•
•
Join Date: Oct 2009
Posts: 45
Reputation:
Solved Threads: 5
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:
Would become:
Hope that helps.
Another option is to include the conversion function call directly as an argument to the other functions.
In other words:
c++ Syntax (Toggle Plain Text)
cout << " The distance traveled on Earth = " << distance_earth (angle,mps,earth_gravity) << " meters " << endl; cout << " The distance traveled on Mars = " << distance_mars (angle,mps,mars_gravity) << " meters " << endl; cout << " The distance traveled on The Moon = " << distance_moon (angle,mps,moon_gravity)<< " meters " << endl;
Would become:
c++ Syntax (Toggle Plain Text)
cout << " The distance traveled on Earth = " << distance_earth (angtorad(angle,pi),mps,earth_gravity) << " meters " << endl; cout << " The distance traveled on Mars = " << distance_mars (angtorad(angle,pi),mps,mars_gravity) << " meters " << endl; 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.
![]() |
Similar Threads
- Date Function not returning variable in main (C++)
- Can anyone please find whats wrong in this code (C++)
- Anyone know whats wrong with this union? (C)
- whats wrong in this code? (PHP)
- whats wrong with the code......... (PHP)
- Please help me i dont understand whats wrong with my code (C++)
- Whats wrong with my computer??? (Viruses, Spyware and other Nasties)
- Whats wrong with this class??? (C++)
Other Threads in the C++ Forum
- Previous Thread: compiling my program
- Next Thread: Get Dialog hWnd
| Thread Tools | Search this Thread |
api array arrays based beginner binary c++ c/c++ calculator char char* class classes code compile compiler console conversion count delete deploy desktop directshow dll download dynamic dynamiccharacterarray encryption error file forms fstream function functions game givemetehcodez google graph gui homeworkhelp homeworkhelper iamthwee ifstream input int integer java lib linkedlist linker linux list loop looping loops map math matrix memory news numbertoword output parameter pointer problem program programming project proxy python random read recursion recursive reference return rpg sorting string strings struct temperature template templates test text text-file tree unix url variable vector video visual visualstudio win32 windows winsock wordfrequency wxwidgets





