I've been trying to figure this out, but I can't get this to work! Can someone explain to me what I'm doing wrong?
This is what I'm supposed to do:
Write a function that returns the distance a vehicle can travel in a given time, where the vehicle has an initial velocity of zero and a constant acceleration up to a specified maximum velocity. The function should return a float and should have parameters for time (in seconds), the vehicle's acceleration (in m/s2), and its maximum velocity (in m/s).
You should calculate the distance traveled as follows:
· calculate time to reach maximum velocity -- t_max = max_v/ a
· compare t to t_max, if t is greater
o distance = max_v/ 2 * t_max + (t - t_max) * max_v
· otherwise
o distance = (t * a) / 2 * t
max_v = maximum velocity, a = acceleration, d = distance traveled, t = travel time and t_max= time to reach maximum velocity
And so far, I have this:
Code:
float disTraveled (float t, float maxVelocity, float acceleration);
void race();
int main()
{
printf("\n\nQUESTION - Race\n");
race();
return 0;
}
void race()
{
float T_Max1, acceleration1, t, maxVelocity, acceleration;
printf("Enter the maximum velocity of vehicle 1: ");
scanf("%f", &T_Max1);
printf("Enter the acceleration of vehicle 1: ");
scanf("%f", &acceleration1);
printf("Enter the race time: ");
scanf("%f", &t);
printf("\n%s%11s%10s%11s\n", "Vehicle", "Max.", "Acc.", "Distance");
printf("%s%11s%10s%11s\n\n", "-------", "----", "----", "--------");
printf("%s%9.2f%10.2f%11.2f\n", "Vehicle 1", T_Max1, acceleration1, disTraveled(t, maxVelocity, acceleration));
float disTraveled (float t, float maxVelocity, float acceleration)
{
float distance, T_Max;
T_Max = (maxVelocity / acceleration); // Time to reach maximum velocity
if (t >= T_Max) {
distance = (((maxVelocity / 2.0) * T_Max) + ((t - T_Max) * maxVelocity));
}
else {
distance = (((t * acceleration) / 2.0) * t);
}
return distance;
}