((PLEASE USE A string tokenizer)) This is a simple prototype program to predict
ID: 3795346 • Letter: #
Question
((PLEASE USE A string tokenizer))
This is a simple prototype program to predict the trajectory of a projectile under ideal conditions. You may assume that the projectile will have a constant velocity as it travels and that the only outside effect on the projectile will be due to the force of gravity. Thus the formula used to compute the time that it takes for the projectile to reach its target is: Distance to target Time = ----------------------------------- Velocity * cos (angle of elevation in radians) The distance the projectile is off the ground at the target's distance is calculated using the formula: Height = Velocity * Time * sin (angle of elevation in radians) Force of Gravity * Time * Time - ------------------------- 2 You will receive the following information from the user: projectile velocity (feet / sec) angle of elevation (degrees) distance from target (feet) target size (height of target in feet) elevation of target (to bottom of target in feet) All values for one shot will be entered on one line of input. The program will continue reading input values until the user enters zeros for all values. Use the value of 32.17 as the constant for Gravity. For each input set of values your program should indicates one of the following responses: The target was hit by the projectile The projectile was too low, height was: #.### feet The projectile was too high, height was: #.### feet The computed distance was too short to reach the target This last message should be printed if the height was less then zero. The height should be printed using three digits to the right of the radix point. Example: given input: The projectile's velocity is 2.000 feet per second The angle of elevation is 10.000 degrees The distance to the target is 55.600 feet The target's size is 4.500 feet The target is located 100.000 feet above the ground The computed distance was too short to reach the target
Error handling: If a calculation cannot be performed (ie: a divide by zero would occur) your program needs to alert the user using the message:
The computed distance cannot be calculated with the given data
This is a simple prototype program to predict the trajectory of a projectile under ideal conditions. You may assume that the projectile will have a constant velocity as it travels and that the only outside effect on the projectile will be due to the force of gravity. Thus the formula used to compute the time that it takes for the projectile to reach its target is Distance to target Time Velocitycos (angle of elevation in radians) The distance the projectile is off the ground at the target's distance is calculated using the formula Height-Velocity Timesin (angle of elevation in radians) Force of Gravity Time Time You will receive the following information from the user projectile velocity (feet sec) angle of elevation (degrees) distance from target (feet) target size (height of target in feet) elevation of target (to bottom of target in feet) All values for one shot will be entered on one line of input. The program will continue reading input values until the user enters zeros for all values. Use the value of 32.17 as the constant for Gravity For each input set of values your program should indicates one of the following responses: 1. The target was hit by the projectile 2. The projectile was too low, height was: ### feetExplanation / Answer
c++ code:
#include <bits/stdc++.h>
#include <math.h>
using namespace std;
int main()
{
float velocity, ang_ele,dist_from_target;
float target_size, elv_target;
cout << "Enter projectile velocity (feet/sec) ";
cin >> velocity;
cout << "Enter angle of elevation (degrees) ";
cin >> ang_ele;
cout << "Enter distance from target (feet) ";
cin >> dist_from_target;
cout << "Enter target size (feet) ";
cin >> target_size;
cout << "Enter elevation of target (feet) ";
cin >> elv_target;
if((velocity == 0) and (ang_ele == 0) and (dist_from_target == 0))
{
if(target_size == 0 and elv_target == 0)
{
exit(0);
}
}
else
{
float g = 32.17;
if((velocity*cos(ang_ele)) == 0)
{
cout << "The computed distance can not be calculated with given data ";
exit(0);
}
float Time = (dist_from_target)/(velocity*cos(ang_ele));
float Height = velocity*Time*(sin(ang_ele)) -
(g*Time*Time/2) ;
if( Height < 0 )
{
cout << "The computed distance was too short to reach the target ";
}
else if( (Height >= elv_target) and (Height <= (elv_target + target_size)))
{
cout << "The Target was hit by the projectile ";
}
else if(Height > (elv_target + target_size))
{
cout << "The projectile was too high, Height was: " << Height << "feet ";
}
else if(Height < elv_target )
{
cout << "The projectile was too low, Height was: " << Height << "feet ";
}
}
return 0;
}
Sample Output:
Enter projectile velocity (feet/sec)
2
Enter angle of elevation (degrees)
10
Enter distance from target (feet)
55.6
Enter target size (feet)
4.5
Enter elevation of target (feet)
100
The computed distance was too short to reach the target
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.