/* program free_fall C++ version */ #include #include #include /* need iostream.h and iomanip.h for stream output using cin and cout */ void initial(double &y,double &v,double &a,double &g,double &t, double &dt); void Euler(double &y,double &v,double &a,double g,double &t, double dt); void print_table(double y,double v,double a,double t,int &nshow); main() /* no air resistance */ { double y,v,a,g,t,dt; int counter,nshow; /* initial conditions and parameters */ initial(y,v,a,g,t,dt); /* print initial conditions */ print_table(y,v,a,t,nshow); counter = 0; while (y >= 0) { Euler(y,v,a,g,t,dt); counter = counter + 1; if (counter % nshow == 0) print_table(y,v,a,t,nshow); } print_table(y,v,a,t,nshow); // print values at surface } void initial(double &y,double &v,double &a,double &g,double &t, double &dt) { t = 0; // initial time (sec) y = 10; // initial height (m) v = 0; // initial velocity g = 9.8; // (magnitude) of accel due to gravity a = -g; cout << "time step dt = "; cin >> dt; // read in value for dt } void Euler(double &y,double &v,double &a,double g,double &t,double dt) { /* use velocity at beginning of interval */ y = y + v*dt; a = -g; // y positive upward v = v + a*dt; t = t + dt; } void print_table(double y,double v,double a,double t,int &nshow) { if (t == 0) { cout << "number of time steps between output = "; cin >> nshow; cout << "\n time (s) y (m) velocity (m/s) accel (m/s^2) " << "\n\n"; } /* setw(n) allows n spaces for a number setprecision(m) provides m decimal places */ cout << setw(10) << setprecision(3) << t; cout << setw(10) << setprecision(3) << y; cout << setw(10) << setprecision(3) << v; cout << setw(12) << setprecision(3) << a << "\n"; }