Public Group

c++ calculate acceleration from slope

This topic is 3006 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

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 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 on other sites
This is the original excercise:
http://www.eit.ihk-edu.dk/subjects/cpp/sleigh.html

Share on other sites
Quote:
 Original post by iammfaThis 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.

1. 1
2. 2
Rutin
21
3. 3
JoeJ
18
4. 4
5. 5

• 14
• 39
• 23
• 13
• 13
• Forum Statistics

• Total Topics
631717
• Total Posts
3001878
×