I\'m trying to write a program that calculates the time and height of a projecti
ID: 3539905 • Letter: I
Question
I'm trying to write a program that calculates the time and height of a projectile by taking the user input theta, distance, and velocity. I have to use a void function and I'm unsure exactly how to incorporate the calculations and return the values. My output must look like this from a previous homework we did (which was the same problem without the void function).
cout << "The projectile's flight time is " << time << "seconds";
cout << "The projectile's height above ground upon impact is " << height << "ft";
The formulas are
time = (distance)/(velocity * cos(theta));
height = velocity * sin(theta) * time - (G * time) / 2;
Here is the code and I need help at the bottom as you will see:
#include <iostream> //Required for cout
#include <cmath> //Required for trigonometric functions
using namespace std;
// Function prototype
void displayTimeAndHeight( double theta,
double distance,
double velocity );
int main()
{
// Declare and initialize objects.
double theta // input -- angle (radians) of elevation
double distance // input -- distance (feet) to target
double velocity // input -- projectile velocity (feet/second)
const double G = 32.17 //gravitational constant
cout << "Enter the angle of elevation in radians";
cin >> theta;
cout << "Enter the distance to target in feet";
cin >> distance;
cout << "Enter the projectile velocity in ft/sec";
cin >> velocity;
// Exit program.
system( "pause" ); //Pauses execution window
return 0;
}
/*-------------------------------------------------------------*/
// Function definition
/* This function takes the given angle theta, distance, and
* velocity and calculates the time of flight and height at
* impact of the projectile and displays them to standard
* output
*/
void displayTimeAndHeight( double theta,
double distance,
double velocity ) {
//local variables
double time // output %u2013 time (in seconds) of flight
double height // output %u2013 height at impact (in feet)
// implementation of the function goes here
....
.....
.....
....
.....
......
.....
....
.....
Explanation / Answer
#include <iostream> //Required for cout
#include <cmath> //Required for trigonometric functions
using namespace std;
const double G = 32.17; //gravitational constant
// Function prototype
void displayTimeAndHeight( double theta,
double distance,
double velocity,
double& time,
double& height);
int main()
{
// Declare and initialize objects.
double theta; // input -- angle (radians) of elevation
double distance; // input -- distance (feet) to target
double velocity; // input -- projectile velocity (feet/second)
double time;
double height;
cout << "Enter the angle of elevation in radians";
cin >> theta;
cout << endl;
cout << "Enter the distance to target in feet";
cin >> distance;
cout << endl;
cout << "Enter the projectile velocity in ft/sec";
cin >> velocity;
cout << endl;
displayTimeAndHeight(theta,distance,velocity,time,height);
cout << " The projectile's flight time is " << time << " seconds";
cout << " The projectile's height above ground upon impact is " << height << " ft";
// Exit program.
system( "pause" ); //Pauses execution window
return 0;
}
/*-------------------------------------------------------------*/
// Function definition
/* This function takes the given angle theta, distance, and
* velocity and calculates the time of flight and height at
* impact of the projectile and displays them to standard
* output and returns back to funciton.
*/
void displayTimeAndHeight( double theta,
double distance,
double velocity,
double& time,
double& height)
{
time = (distance)/(velocity * cos(theta));
height = velocity * sin(theta) * time - (G * time) / 2;
} //end of displayTimeAndHeight