In this lab, you will write a program that computes the following physics motion
ID: 3817007 • Letter: I
Question
In this lab, you will write a program that computes the following physics motion equations vo at vi 2- zf zo vo t 3. vf (vs vo) t where zo is the initial position. zf is the final position. vo is the initial velocity vf is the final velocity a is acceleration. and t is time. See the Example Execution section below for detailed examples. Objectives Learn about function prototypes Use pointers to pass by reference Translate mathematical equations to C expressions More practice with the switch statement More practice with a user menu More practice with the math library Function Prototypes In C. all identifiers need to be declared before they are first used. We have seen this for variables. but it is also true for functions. In standard C, all functions must be declared before the first call to the function. A function declaration. also known as a prototype contains the return type. the function name. an a list of function parameter types. In past labs, we have avoided this issue by placing the main function as the last function in the program. In this lab, you will place your main finction function as the first function in the program. Therefore you will need to include function declarations for all your functions in the program. Notice that the parameter v names are absent in the declaration. You will need to give these variable name in your s implementation of the function, but you omit them in the function prototype. These are the function declarations you will need in this lab. Place these lines above your main function and below your #include statements. You will write the function definition e. implementation) for each of these functions.Explanation / Answer
#include <stdio.h>
#include <stdlib.h>
#include<math.h>
int user_menu();
void equation1();
void equation2( );
void equation3( );
void equation4( );
float get_position_initial();
float get_position_final();
float get_velocity_initial();
float get_velocity_final();
float get_acceleration();
float get_time();
int main()
{
int choice;
choice=user_menu();
switch(choice)
{
case 1: printf(" Calculate motion equation 1 ");
equation1();
break;
case 2: printf(" Calculate motion equation 2 ");
equation2();
break;
case 3: printf(" Calculate motion equation 3 ");
equation3();
break;
case 4: printf(" Calculate motion equation 4 ");
equation4();
break;
default: printf(" Wrong Entry !!");
}
//float xo=get_position_initial();
// float xf=get_position_final();
//float v0=get_velocity_initial();
//float vf=get_velocity_final();
//float a=get_acceleration();
//float t=get_time();
return 0;
}
float get_position_initial()
{
float ip;
printf("Enter Initial Position : ");
scanf("%f",&ip);
return ip;
}
float get_position_final()
{
float fp;
printf("Enter Final Position : ");
scanf("%f",&fp);
return fp;
}
float get_velocity_initial()
{
float vo;
printf("Enter Initial velocity : ");
scanf("%f",&vo);
return vo;
}
float get_velocity_final()
{
float vf;
printf("Enter final velocity : ");
scanf("%f",&vf);
return vf;
}
float get_acceleration()
{
float a;
printf("Enter acceleration : ");
scanf("%f",&a);
return a;
}
float get_time()
{
float t;
printf("Enter time : ");
scanf("%f",&t);
return t;
}
int user_menu()
{
int choice;
printf(" Menu bar");
printf(" Enter 1, for Calculate motion equation 1");
printf(" Enter 2, for Calculate motion equation 2");
printf(" Enter 3, for Calculate motion equation 3");
printf(" Enter 4, for Calculate motion equation 4");
printf(" Enter you Choice : ");
scanf("%d",&choice);
return choice;
}
void equation1()
{
float vf;
float vo=get_velocity_initial();
float a=get_acceleration();
float t=get_time();
vf=vo+a*t;
printf(" final Velocity(vf) is %.2f",vf);
}
void equation2()
{
float xo=get_position_initial();
float vo=get_velocity_initial();
float a=get_acceleration();
float t=get_time();
float xf;
xf = xo + (vo*t) + (float)((a*t*t)/2);
printf(" final position(xf) is %.2f",xf);
}
void equation3()
{
float xo=get_position_initial();
float xf=get_position_final();
float vo=get_velocity_initial();
float a=get_acceleration();
float vf;
vf=sqrt((vo*vo+2*a*(xf-xo)));
printf(" final velocity(vf) is %.2f",vf);
}
void equation4()
{
float xo=get_position_initial();
float vo=get_velocity_initial();
float vf=get_velocity_final();
float a=get_acceleration();
float t=get_time();
float xf;
xf= xo+((vf+vo)*t)/2.0;
printf(" final position(xf) is %.2f",xf);
}
-------------------------------
Output sample 1:-
Menu bar
Enter 1, for Calculate motion equation 1
Enter 2, for Calculate motion equation 2
Enter 3, for Calculate motion equation 3
Enter 4, for Calculate motion equation 4
Enter you Choice : 1
Calculate motion equation 1
Enter Initial velocity : 2
Enter acceleration : 3
Enter time : 4
final Velocity(vf) is 14.00
----------------------------------
Output sample 2:-
Menu bar
Enter 1, for Calculate motion equation 1
Enter 2, for Calculate motion equation 2
Enter 3, for Calculate motion equation 3
Enter 4, for Calculate motion equation 4
Enter you Choice : 2
Calculate motion equation 2
Enter Initial Position : 2
Enter Initial velocity : 4
Enter acceleration : 1
Enter time : 5
final position(xf) is 34.50
------------------------------------------------
Output sample 3:-
Menu bar
Enter 1, for Calculate motion equation 1
Enter 2, for Calculate motion equation 2
Enter 3, for Calculate motion equation 3
Enter 4, for Calculate motion equation 4
Enter you Choice : 3
Calculate motion equation 3
Enter Initial Position : 3
Enter Final Position : 5
Enter Initial velocity : 7
Enter acceleration : 4
final velocity(vf) is 8.06
-------------------------------------------------------
Output sample 4:-
Menu bar
Enter 1, for Calculate motion equation 1
Enter 2, for Calculate motion equation 2
Enter 3, for Calculate motion equation 3
Enter 4, for Calculate motion equation 4
Enter you Choice : 4
Calculate motion equation 4
Enter Initial Position : 1
Enter Initial velocity : 3
Enter final velocity : 5
Enter acceleration : 6
Enter time : 3
final position(xf) is 13.00
---------------------------------------------------------------------------------------------
If you have any query, please feel free to ask.
Thanks a lot.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.