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;
}

Recommended Answers

All 6 Replies

The key part of the assignment was make a function. You have thrown all of your code into main(). Make it a separate function that you can call in main().

Double check your formula for A with a calculator. Also, you did not use the M_PI constant, you wrote your own (which is probably nearly as good, but it's not what the exercise asked for).

please, can you write the code yourself, to compare my solve with your solve :)

You know what a function is, right? (I think you've posted stuff before with them). There's no function (besides main()) in your program. Read up on them and try again with it. The calculation for A is incorrect, you shouldn't need any programming for that.

Your equations just a little off. First you should convert the angle from radian to degree, then apply the equation given. Good luck with the Physics, I loved that class.

radianAngle = degAngle * PI / 180;
A = g*sin(radianAngle);

ok, but i still can't able to get degree angle to convert it to radian, this is my modified code:

#include <math.h>
#include <iostream>
using namespace std;
    
int main()
   {
   double A=0;
   double a=0;
   double g=9.8;
   const float M_PI=3.14159265;
   radianAngle = degAngle * M_PI / 180;
   A = g*sin(radianAngle);
   cout<<"acceleration is: "<< A <<endl;
   return 0;
   }

please can you solve it, this is the original exercise:
http://www.eit.ihk-edu.dk/subjects/cpp/sleigh.html

Your equations just a little off. First you should convert the angle from radian to degree, then apply the equation given. Good luck with the Physics, I loved that class.

radianAngle = degAngle * PI / 180;
A = g*sin(radianAngle);

He did have the conversion in there on line 11 within the sine.

OP, that does correct the A value for you, but it's up to you to wrap it all into a function (according to the problem description). Did you look up any information on how to do that?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.