Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write a C program to simulate a falling object. The program should ask for the i

ID: 3816436 • Letter: W

Question

Write a C program to simulate a falling object. The program should ask for the initial height of the object, in feet. The output of the program should be the time for the object to fall to the ground, and the impact velocity, in ft/s and miles/hour.
Your program should use Euler’s method to numerically solve the differential equations describing the motion of a falling object. In Euler’s method, the state of the object at some small future time is calculated using the values at the present time. This small future timestep is typically called delta time, or dt. Thus the position (p) and speed (v) of the object in the next timestep t + dt is written as a simple function of the position and speed at the current timestep t (g is the acceleration due to gravity):

v(t+dt) = v(t) + g * dt

p(t+dt) = p(t) + v(t+dt) * dt


You should actually start out with the velocity as zero, and the position at the initial height of the object. Then your position (above the ground) would be:


p(t+dt) = p(t) - v(t+dt) * dt


And you would integrate until the position becomes less than or equal to zero.


The input/output formats for your program should look like this:
Program to calculate fall time and impact speed of a falling object dropped from a specific height.

Enter initial height in feet: 100
Falling time = 2.492224
seconds Impact speed = 80.249613 feet/sec
Impact speed = 54.715645 mph

CODE: I need to use a while or for loop, either one is okay...

#include <stdio.h>
#include <stdlib.h>

//function main begins program excution

int main(void)
{
float T;// falling time
float dt = 0.49845; // the future timestep in seconds
float G = 32.2; // universal gravitational ocnstant in feet per second per second
float v0 = 0.0; //initial velocity
float v1, v2, v3, v4, v5, v6; // velocity of the falling object after dt


printf("program to calculate fall time and impact speed of a falling object dropped from a specific height. ");// define this program

float p0; //initial height to be entered by user
printf("Enter initial height in feet: "); //prompt
scanf("%f", &p0); // read initial height

float p1, p2, p3, p4, p5, p6, p7; // position of the falling object after time dt

v1 = G*dt+v0; // velocity of the falling object at time dt
v2 = G*dt+v1; // next velocity of the falling object after time dt
v3 = G*dt+v2; // next velocity of the falling object after time dt
v4 = G*dt+v3; // next velocity of the falling object after time dt
v5 = G*dt+v4; // next velocity of the falling object after time dt
v6 = G*dt+v5; // next velocity of the falling object after time dt

p1 =v0*dt+p0; // position of the falling object at time dt
p2 =v0*dt+p1; // next position of the falling object at time dt
p3 =v0*dt+p2; // next position of the falling object at time dt
p4 =v0*dt+p3; // next position of the falling object at time dt
p5 =v0*dt+p4; // next position of the falling object at time dt
p6 =v0*dt+p5; // next position of the falling object at time dt
p7 =v0*dt+p6; // next position of the falling object at time dt
  

T = 5*dt;// falling time
printf("falling time =%.6f seconds ", T); // display falling time

printf("impact speed = %.6f feet/sec ", v5); // display impact speed in feet/sec


//Convert velocity from feet/sec to mile/hour multiply by 0.682

v5 = v5*0.682; // Impact speed in mile/hour

printf("Impact speed = %.6f mph ", v5); // display impact speed in mile/hour

}

Explanation / Answer

So, I read the description and saw your code also.

The output was not having bigger difference than that which is printed in the question. So I can say that this code is fine for this question.

So now you want this to be developed using loop. And so I am answering for the same.

The modified source code is:

Source Code:

#include <stdio.h>
#include <stdlib.h>
//function main begins program execution
int main(void)
{
float T;// falling time
float dt = 0.49845; // the future timestep in seconds
float G = 32.2; // universal gravitational constant in feet per second per second

float v[7];

v[0] = 0.0; //initial velocity

float p[8];

int i;

printf("Program to calculate fall time and impact speed of a falling object dropped from a specific height. "); //define this program

printf("Enter initial height in feet:"); //prompt
scanf("%f", &p[0]); // read initial height

for(i=1; i<7; i++)
{
v[i] = G*dt + v[i-1];
}

for(i=1; i<8; i++)
{
p[i] =v[0]*dt+p[i-1];
}

T = 5*dt;// falling time
printf(" Falling time =%.6f seconds ", T); // display falling time

printf("Impact speed = %.6f feet/sec ", v[5]); // display impact speed in feet/sec

//Convert velocity from feet/sec to mile/hour multiply by 0.682
v[5] = v[5]*0.682; // Impact speed in mile/hour
printf("Impact speed = %.6f mph ", v[5]); // display impact speed in mile/hour
return 0;
}

Description:

As you have number of iterations fixed here in your program, seems like you have already calculated or given, for loop is the better choice here to use.

And, the for loop use will be easier with the arrays, not the individual variables.

For example, here you are having 7 variables v0 to v6 for velocity. So, instead of v0, v1, .... individual, just define a array v[ ] of size 7 which will have elements v[0] to v[6].

So, v[0] = 0.0 is assigned.

Now, inside for loop, you want to set from v[1] to v[6], and inside every iteration..

as you were writing  

v1 = G*dt+v0, write it as  v[1] = G*dt+v[0]

v2 = G*dt+v1 write it as  v[2] = G*dt+v[1]

..

So, in general you can say that for setting the velocity at current index i, you are having velocity set at previous index i-1 right !

So, these velocity assignment statements can be generalized as:   v[i] = G*dt + v[i-1];

The same thing goes with p.

At first I declared its array. Then set p[0] as initial height. And then again used for loop here, from p[1] to p[7]; I set the values using the equation.

Now, after this calculation, you were printing v5 so it will be replaced with v[5], nothing much.

So this how this program is using 2 for loops also which are iterated using integer 'i'. And so this is as per your requirement.

This prints the same output as the code you have posted as I have not changed any logic, just the representation is changed from individual basic data variables to array.

So please comment if there is any query. Thank you. :)

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote