Please program in C Calculate the average velocity distance
Please program in C
Calculate the average velocity, distance travelled and the acceleration of a car when the inputs, initial velocity, final velocity and time are provided by the user Formulae: 1) Vavg (V0 Vf)/2 3) A (Vf-VO)/t Where, VO Initial Velocity in meters/second (m/s), Vf Final Velocity in m/s, t Time in seconds vavg Average velocity in m/s, s Distance travelled in meters, A Acceleration in mls2 Create four functions and a main function to write this program. Details of each function are given below: 1) main function The user should prompt for the inputs Initial velocity, Final velocity and Time The Initial velocity must be greater than 0 or equal to 0 e Final velocity must be greater than 0 and also greater than initial velocity The time should be greater than 0 Call the functions calculteAverageVelocity, calculateDistance, calculateAcceleration and store their values in different variables. There is no need to store the output of printResults function in any variable because it does not return anything and it is a void function Add a loop in main that allows the user to repeatedly execute the program until he/she chooses to exit 2) calculateAverageVelocity function The inputs to this function are nitial velocity, Final velocity. Calculate the average using the formula provided; Return its value to the main function a store it in a variable. 3) calculateDistance function The inputs to this function are Initial velocity, Final velocity, time Calculate the distance using the formula provided Return its value to the main function and store it in a variable. 4) calculateAcceleration function The inputs to this function are Initial velocity, Final velocity, time Calculate the acceleration using the formula provided; Return its value to the main function and store it in a variableSolution
// C code
#include <stdio.h>
#include <stdlib.h>
double calculateAverageVelocity(double Vo, double Vf)
{
return (Vo+Vf)/2;
}
double calculateDistance(double Vo, double Vf, double t)
{
return ((Vo+Vf)*t)/2;
}
double calculateAcceleration(double Vo, double Vf, double t)
{
return (Vf-Vo)/t;
}
int main()
{
double Vo,Vf, t;
while(1)
{
printf(\"Enter initial velocity: \");
scanf(\"%lf\",&Vo);
if(Vo < 0)
printf(\"initial velocity must be greater han or equal to 0\ \");
else
break;
}
while(1)
{
printf(\"Enter final velocity: \");
scanf(\"%lf\",&Vf);
if(Vf <= 0 || Vf < Vo )
printf(\"Final velocity must be greater than 0 and the initial velocity\ \");
else
break;
}
while(1)
{
printf(\"Enter time: \");
scanf(\"%lf\",&t);
if(t < 0)
printf(\"time should be greater than 0\ \");
else
break;
}
double Vavg = calculateAverageVelocity(Vo,Vf);
double distance = calculateDistance(Vo,Vf,t);
double A = calculateAcceleration(Vo,Vf,t);
printf(\"\ Average velocity: %lf m/s\ \",Vavg);
printf(\"Distance: %lf m\ \",distance);
printf(\"Acceleration: %lf m/s^2\ \",A);
return 0;
}
/*
output:
Enter initial velocity: -1
initial velocity must be greater han or equal to 0
Enter initial velocity: 4.5
Enter final velocity: 3
Final velocity must be greater than 0 and the initial velocity
Enter final velocity: 10
Enter time: 4
Average velocity: 7.250000 m/s
Distance: 29.000000 m
Acceleration: 1.375000 m/s^2
*/

