Sie sind auf Seite 1von 6

Introduction to Programming in C++

Summer 2013

HOMEWORK NO. 2

Computing Flight Time and Impact Height of a Projectile

1. PROBLEM STATEMENT
Compute the duration of a projectiles flight and its above ground height when impacting
the target wall.

2. INPUT/OUTPUT DESCRIPTION
The user will supply: angle of elevation in radians, distance to target, and projectile
velocity. The variables used are as follows:
double
double
double
double
double

theta; // input -- angle (radians) of elevation


distance; // input -- distance (feet) to target
velocity; // input -- projectile velocity (ft/s)
time;
// output -- time of flight (seconds)
height; // output -- height at impact (feet)

In addition to user supplied input, we will need to define the gravitational constant in
ft/s2 . We will use
const double GRAV = 32.17; // Gravitational constant

3. HAND EXAMPLE
The relevant formulas needed for our algorithm are

time = distance / ( velocity cos(theta) )


height = velocity sin(theta)time (GRAVtime)/2
Inputs
Angle of Elevation (radians)
Velocity (ft/s)
Distance to Target (ft)
Outputs

Data Set 1
0.3
800
11000

Data Set 2
0.71
1600
78670

Time to Impact (s)


Height of Impact (ft)

14.39
3171.19

64.84
66576.2

4. ALGORITHM DEVELOPMENT
1. We will prompt the user to supply the necessary inputs.
2. These inputs will be stored in their respective variables. No testing will be done to
ensure the user has entered valid input.
3. We will compute flight time first.
4. We will then compute the height of impact.
5. Our results will then be displayed to the user. If the height of impact is less than zero,
we will inform the user that the projectile cannot hit the target above ground with the
supplied parameters.
Code follows:
/*------------------------------------------------------------*/
/* Homework No. 2 */
/* This program computes the duration of a projectile's */
/* flight and its height at impact. */
#include <iostream>
//Required for cout
#include <cmath>
//Required for trigonometric functions
using namespace std;
int main()
{
double theta; // input -- angle (radians) of elevation
double distance; // input -- distance (feet) to target
double velocity; // input -- projectile velocity (feet/second)
double time; // output -- time of flight (seconds)
double height; // output -- height at impact (feet)
const double GRAV = 32.17; // Gravitational constant(feet/second)
// Get angle, distance, and projectile velocity from user
cout << "Enter the angle of elevation (in radians): ";
cin >> theta;
cout << "Enter the distance to the target (in feet): ";
cin >> distance;
cout << "Enter the projectile velocity (in feet per second): ";
cin >> velocity;
cout << endl;

//Compute time to target


time = (distance / (velocity * cos(theta)));
//Compute height at impact
height = velocity * sin(theta) * time - (GRAV * time) / 2;
// Display results
cout << "Time to hit target: " << time << " seconds" << endl;
if (height < 0) {
cout << "The projectile will not hit the target!" << endl;
}
else {
cout << "Height at impact: " << height << endl;
}
// Exit program.
system( "pause" ); //Pauses execution window
return 0;
}
Compilation is successful

5. TESTING
Inputs
Angle of Elevation
(radians)
Velocity (ft/s)
Distance to Target (ft)
Outputs
Time to Impact (s)
Height of Impact (ft)

DATA SET 1

Data Set 1
0.3

Data Set 2
0.71

Data Set 3
0.1

800
11000

1600
78670

30
15000

14.39
3171.19

64.84
66576.2

502.51
-6577.86

Data Set 2

Data Set 3

6. CONCLUSION
In this exercise I have learned to use trigonometric functions from the cmath library.

Das könnte Ihnen auch gefallen