cannon ball thrown straight into air with starting velocity v0. position of cannon ball after t seconds is given by equation s = vi t – (1/2)gt^2. You are required to confirm equation by simulation. In simulation, you consider how ball moves in very short time interval Δt. In short time interval the velocity v is constant, we can compute distance ball moves using Δs = vΔt. You assume Δt is constant with value of 0.01. You get updated position using s = s + v *Δt. In short time interval, Δv = –gΔt, you must keep velocity updated as v = v - g×Δt; In next iteration, new velocity used to update distance. You need to run simulation until cannon falls back to ground. Your program take initial velocity as input. Update position and velocity 100 times per second but print out position only every full second. printout values from exact formula s =vi t – (1/2) gt^2 for comparison.plot the path that cannon ball
#include <iostream>
#include<math.h>
using namespace std;
int main()
{
double t=0.01;
int v=0;
cout<<"\nEnter initial position: ";
double s;
cin>>s;
cout<<"\nEnter gravity: ";
int g;
cin>>g;
for(int i=0;i<100;i++){
s = v*t - (1/2)*g*pow(t,2);
s = s + v * t;
v = v - g * t;
}
cout<<"\nFinal position = "<<s;
return 0;
}
Comments
Leave a comment