Sign in to follow this  
iammfa

c++ calculate acceleration from slope

Recommended Posts

Hi, The following is an exercise in c++, I solved it but I doubt my solve is not right ..can someone tell me is my solve right or wrong especially the compiler didn't give me any errors The exercise: first look the attached image, The acceleration of a sleigh sliding down a hill is a = g sin a , where a is the slope of the hill, and the gravity acceleration g = 9.8 ms-2. Write a C++ function that calculates the acceleration as a function of the slope a in degrees. Hint: the header file MATH.H defines the common mathematical functions, including sin(x) (where x is in radians) and sqrt(x) (square root). The constant p is also defined in MATH.H with the name M_PI. To get access to these functions and constants, write: #include <MATH.H> My c++ code: #include <math.h> #include <iostream> using namespace std; int main() { double A=0; double a=0; double g=9.8; const float PI=3.14159265; double degAngle = sin(a*PI/180); A = degAngle/9.8; cout<<"acceleration is: "<< A <<endl; return 0; }

Share this post


Link to post
Share on other sites
You need to do some work.
First why didn't you use M_PI instead of writing PI out?
Second how can acceleration answer be an angle?
You don't even have the right units?
Of course I would doubt your answer too if you don't even output the right units.
As they say garbage in/garbage out so I don't see why you expect the compiler to help you out?

Share this post


Link to post
Share on other sites
Quote:
Original post by iammfa
This is the original excercise:
http://www.eit.ihk-edu.dk/subjects/cpp/sleigh.html

Read the first sentence from the link you gave us and you will see whats wrong with your code.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this