logo CodeStepByStep logo

Projectile

Language/Type: C++ interactive programs parameters
Related Links:

Write a console program that calculates the trajectory that a projectile will follow. You will prompt the user for the projectile's initial velocity, its initial angle relative to the horizontal, and the number of time increments to display. Here is an example output log from your program:

This program computes the trajectory of a projectile given
its initial velocity and angle relative to the horizontal.

Velocity  (m/s)? 30
Angle (degrees)? 50
Number of steps? 10

step    x       y       time
----------------------------
0       0.00    0.00    0.00
1       9.03    9.69    0.47
2       18.07   17.23   0.94
3       27.10   22.61   1.41
4       36.14   25.84   1.87
5       45.17   26.92   2.34
6       54.21   25.84   2.81
7       63.24   22.61   3.28
8       72.28   17.23   3.75
9       81.31   9.69    4.22
10      90.35   0.00    4.69

You can compute the x and y components of velocity using the cos and sin of the initial angle, respectively. The projectile will be pulled downward by gravity with a force of 9.81 m/s2. Recall that you can compute the displacement of a body in motion using the following formula:

displacement = vt + ½at2

The following is a pseudocode description of the process:

x, y, t = 0.
for (the given number of steps):
    add time increment to t.
    add x increment to x.
    reset y to y-velocity * t + 0.5 * -9.81 * t * t.
    report step #, x, y, and t.

You should break down your program into several functions, each of which helps solve the overall problem.

The output columns should align into 8-space-wide columns, left-aligned.

Complete program: Write an entire program that you could put into a file and run outside of CodeStepByStep.

You must log in before you can solve this problem.

Log In

Need help?

Stuck on an exercise? Contact your TA or instructor.

If something seems wrong with our site, please

Is there a problem? Contact us.