In: Computer Science
Q.3. Projectile Motion (30) A projectile is launched at 40.0 m/s at an angle 30 degree from ground to ground. o Write a function that will return the time of flight based on V_{0} and theta. o Find N time split (suppose N=20) and put those time in Tf[]. o Calculate the height y of the projectile for those time and put them in Y[].
Language C++ code
In this program, we have to calculate time of flight for the given projectile, split this time of flight into twenty equally spaced points in time, find the height y of the projectile at these points.
The program will have three functions:
1.) time_of_flight() : this function calulcate the time of filglt of the projectile with this formula :
2.) time_split() : this function splits the time of flight into N equally spaced points and stores them in an array
3.) calc_Y() : this function calculate y at given time points and stores them into another array Y[]. The height formula is
program:
#include <iostream>
#include <math.h>
#include <iomanip>
using namespace std;
float time_of_flight(float v0, float theta){
return 2*v0*sin(theta)/9.81;
}
void time_split(float t_flight,int N,float tf[]){
float t_split = (float)t_flight/N;
for(int i = 1; i<=N; i++)
tf[i-1] = t_split*i;
}
void calc_Y(float v0, float theta, int N, float tf[], float
Y[]){
float v0_y = v0*sin(theta);
for(int i = 0; i<N; i++)
Y[i] = v0_y*tf[i] - 0.5*9.81*tf[i]*tf[i];
}
int main(){
float v0 = 40, theta = (float)30*M_PI/180;
float t_flight = time_of_flight(v0,theta);
float tf[20];
time_split(t_flight,20,tf);
float Y[20];
calc_Y(v0,theta,20,tf,Y);
cout<<"time\t\tY\n";
for(int i = 0; i<20; i++){
cout<<fixed<<setprecision(5)<<tf[i]<<"\t\t"<<Y[i]<<endl;
}
}
output: