# help with using functions to calculate projectile height on impact

#### Killerbee65

Joined May 15, 2017
243
Hello all!

I recently got an assignment from my pressor that ask me to Write a program that computes the duration of a projectile’s flight and its height above the ground when it reaches the target.

He gave us the following equations to work with:
a. time = distance/ velocity x cos(0)
b. height = velocity x sin(0) x time - (g x time^2)/2

distance, velocity, and angle are all inputs from the user and g is a gravitational consistency of 32.17

/*

Name: Erik Diaz
Course : Computer science and programming
Section : 07
Date : 09 / 23 / 2020
ID:lab 5b
Purpose:
Write a program that computes the duration of a projectile’s flight and its height above the ground when it reaches the target.

Analysis/ Data:
1. inputs: angle, distance, velocity (float)
2. output: time in seconds, height (float)
3. equations
a. time = distance/ velocity x cos(0)
b. height = velocity x sin(0) x time - (g x time^2)/2

Typical-
input:
expected output:
Results:

Boundry-
input:
expected output:
results:

Extreme-
input:
expected output:
results:

*/

#include <iostream>
#include <cmath>
using namespace std;

float velocity, target_distance, angle;
float grav_const = 32.17;

float inPuts( ) {

cout << "please enter the velocity of the projectile: ";
cin >> velocity;
cout << "it's distance: ";
cin >> target_distance;
cout << "The angle: ";
cin >> angle;
return velocity, target_distance, angle;
}

float time ( ) {

inPuts( );
return target_distance/ velocity * cos(angle);//equation for time

}

float height() {

inPuts();
time();
float height1 = velocity * sin(angle) * time() - (grav_const * (time() * time())) / 2;//equation for height
return height1;

}

int main() {

cout << time() << endl << height();
}

I'm using a different ide so I'm not sure if I copied and pasted correctly.
I keep getting consistent numbers when I input numbers yet they are wrong (ex: 1, 1, 1 for the inputs ->.54 for output or time). Also, the code consistently repeats 5 times yet I have no loops. I was tight for time so I only tested to see if the computer ran through each function.

#### RBR1317

Joined Nov 13, 2010
593
I keep getting consistent numbers when I input numbers yet they are wrong
If the numbers are wrong it may be because the equation for time needs some additional parentheses. Also, I am unfamiliar with this style of programming (not being a programmer); what language is it?

#### SamR

Joined Mar 19, 2019
2,921
32.17? Flying pigs? What are the units? I prefer 9.8 m/s^2 for gravity... And even when going up, it is falling down. Just a couple of hints...

#### RBR1317

Joined Nov 13, 2010
593
What are the units?
The units are baked into the equations provided by the professor. Not an issue here.

#### Killerbee65

Joined May 15, 2017
243
32.17? Flying pigs? What are the units? I prefer 9.8 m/s^2 for gravity... And even when going up, it is falling down. Just a couple of hints...
I know what you mean with the 32.17, I prefer 9.8m/s^2 but it is what my professor wants. But could you elaborate on your hint? I have an idea but I'm not sure my professor wants it to be too complicated.

#### SamR

Joined Mar 19, 2019
2,921
Dealing with projectiles, as soon as they leave the fixed mounted position gravity acts upon them determining their flight path. Hence even though they may be increasing in altitude they are also falling @ gravitational acceleration. Or else what goes up would not come down. Therefore they do not travel in a straight line. The US Navy spent billions of dollars calculating trajectories. In classical physics such things as air density changes with elevation, barometric pressures resultant frictional forces on projectile trajectories, diameter of projectile, shape of projectile, subsonic and supersonic frontal waves, etc. are ignored but were computed in the Naval calculations. Hence their need for computers such as ENIAC et al. It is much more complicated than gravity, angle, and velocity.

#### Wolframore

Joined Jan 21, 2019
2,195
32.17 ft/sec^2 is a conversion factor for gravity in English units. Useful if your bullets flight is measured in yards and feet.

#### drc_567

Joined Dec 29, 2008
972
32.17 ft/sec^2 is a conversion factor for gravity in English units. Useful if your bullets flight is measured in yards and feet.
g is plain gravitational acceleration ... $$32 ft/sec^2$$.
$$g_c$$ is the conversion factor between pound mass ... $$lb_m$$, and pound force .. $$lb_f$$ and is equal to $$32 [lb_m *ft]/[lb_f *sec^2]$$
The conversion factor is necessary when checking units in various formulae involving $$lb_m$$.

Last edited:

#### Wolframore

Joined Jan 21, 2019
2,195
Yeah that’s what I meant