/* program free_fall C version */ #include 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); /* next 2 lines usually written as if (++counter % nshow == 0) */ counter = counter + 1; if (counter % nshow == 0) /* % is modulus operator */ print_table(y,v,a,t,&nshow); } /* print values at surface */ print_table(y,v,a,t,&nshow); } 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); printf("time step dt = "); scanf("%lf",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; /* reminder that acceleration is constant */ /* y positive upward */ *a = -g; *v = *v + (*a)*dt; *t = *t + dt; } void print_table(double y,double v,double a,double t,int *nshow) { if (t == 0.0) { printf("number of time steps between output = "); scanf("%d",nshow); printf(" \n"); printf(" time (s) y (m) velocity (m/s) accel (m/s^2) \n"); printf(" \n"); } printf("%10.3f,%10.3f,%10.3f,%12.3f\n",t,y,v,a); }