In: Computer Science
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[]. C++
Solution :
Following is the C++ code for the problem :
#include <iostream>
#include <cmath>
using namespace std;
const double PI = 3.141592654, g = -9.81 ;
const int N=20;
float time_of_flight(float v,float angle)
{
float time=fabs((2*v*sin((angle*PI)/180))/g);
return time;
}
int main()
{
float launch_angle, Vo,tof,tf[20],y[20];
Vo=40.0;
launch_angle=30;
tof=time_of_flight(Vo,launch_angle);
cout<<"The time of flight is : "<<tof<<endl;
float increment=tof/20;
cout<<"Following are the position of projectile at N(20) different time intervals"<<endl;
cout<<" Time\t\tHeight"<<endl;
for(int i=0;i<N-1;i++)
{
if(i==0)
tf[i]=increment;
else
{
tf[i]=tf[i-1]+increment;
}
y[i]=Vo*sin(launch_angle*PI/180)*tf[i]+0.5*g*tf[i]*tf[i];
cout<< tf[i]<<" "<<y[i]<<endl;
}
return 0;
}
Code demo :
Output :