Question

C++ please A typical problem encountered in the study of dynamics is the trajectory problem. In the situation illustrated below, a projectile is fired from the edge of a cliff with an initial velocity, Vo , and a firing angle, A. The cliff has a height, h. It is desired to:

          1.       determine the distance, xt, to the target

          2.       determine the highest elevation reached, ymax

          3.       determine the time to reach the target, tt

          4.       generate a table of (x,y) points to use in plotting the trajectory

Background:

If       Vo = initial velocity

and    A = firing angle (in degrees), then

          Vxo = initial horizontal velocity = Vocos(A)

          Vyo = initial vertical velocity = Vosin(A)

If g = ‑9.81 m/s2 = acceleration due to gravity, then solving for t in the quadratic equation (the positive root)

                                     ‑h = Vyot + 0.5gt2

will give the time to reach the target, tt (i.e., t = tt ). Using this value of tt , the distance to the target is

          xt = Vxott

and the maximum height reached is

  

                                                                            Page 2

In order to generate a table of N (x, y) values, the final distance, xt , can be divided into N-1 even increments as shown below:

x 0.0 00. N-2 N-1 N-1

For example, if N = 21: x = 0.00, 0.05xt , 0.10xt , 0.15xt , . . . 0.95xt , xt

Using the x values above, the corresponding values of t and y can be found using

  

Program Requirements:

1.      The user of the program should be prompted to input four values from the keyboard. Also apply the restrictions indicated and allow the user to re-enter bad inputs):

  • Cliff height, h, in m                      h > 0
  • Initial velocity,Vo, in m/s             Vo > 0
  • Angle, A, in degrees                    90° > A > 0
  • Number of point, N.                    100 > N > 5

2.      Use functions for at least the following:

         a) to convert an angle from degrees to radians

         b) to find the two real roots of the quadratic equation when given the coefficients. The function might be called as:      RealRoots(A,B,C,Root1,Root2);

3.      Use arrays to store the values of t, x, and y. The arrays should be dimensioned for a max value of 100 points. The actual number of points, N, will be specified by the user.

4.      Give the user the option of:

         A) Displaying a table of values on the computer screen

         B) Sending the values to a data file so that they can be graphed using Excel

         If option A is selected, the output to the screen should include a brief program description, the input values, and calculated values for xt, ymax , and tt , and a table of (t,x,y) values. All outputs should be formatted and include units. The table might look as follows (lines are not required, but could be added for extra credit):

              time, t (s)      | distance, x (m)    |    height, y (m)

          ----------------------------------------------------------------------

                  0.000       |                 0.0         |                0.0

                  1.134       |           283.6         |              91.7

                      .             |                   .           |                  .

                      .             |                   .           |                  .

                  22.673       |           5672.6         |           -150.0

         If option B is selected, only the values of t, x, and y should be sent to the data file. Put commas between each value so that the file can be opened in Excel as a “commas delimited file”. Also prompt the user to enter the name of the output data file. The data file might look as follows:

0,0,0

1.134,283.6,91.7

.

.

.

22.673,5672.7,-150.0

Example 1:

          You can test your program with the following data:

          Inputs:

                   Vo = 180 m/s

                   h = 150 m

                   A = 30o

                   N = 21 points

          Outputs:

                   tt = 19.9 s

                   xt = 3100.0 m

                   ymax = 562.8 m

                   (The values of t, x, y are not shown for this example).

My main issue is getting the values into array solving for time distance and height isnt the issue, C++ please

0 0
Add a comment Improve this question Transcribed image text
Answer #1

#include<iostream>
#include<string>
#include<cmath>
#include <iostream>
#include <fstream>
using namespace std;
#define pi 3.14159
double Xarr[100],Yarr[100],Tarr[100];
double DegreeRad(double temp)
{
   return temp*pi/180;
}
double RealRoots(double a,double b,double c)
{
   float x1, x2, discriminant;
discriminant = b*b - 4*a*c;
  
if (discriminant > 0) {
x1 = (-b + sqrt(discriminant)) / (2*a);
x2 = (-b - sqrt(discriminant)) / (2*a);
       if(x1>0)
           return x1;
       else if(x2>0)
           return x2;
       else
           cout<<"roots are negative. Please enter iputs again"<<endl;  
}
  
else if (discriminant == 0) {
cout << "Roots are real and same." << endl;
x1 = (-b + sqrt(discriminant)) / (2*a);
if(x1>0)
           return x1;
       else
           cout<<"roots are negative. Please enter iputs again"<<endl;
          
}

else {
cout<<"no real roots. Please enter iputs again"<<endl;
}
   return -1;
}
void TabularDisplay(int N)
{
cout<<"tablular display"<<endl;
cout<<"time,t(sec) disatnce,x(m) height,y(m)" <<endl;
for(int i=0;i<N;i++)
cout<<Tarr[i]<<" "<<Xarr[i]<<" "<<Yarr[i]<<endl;
}
void FileOutput(int N)
{
   cout<<"enter file name"<<endl;
   string str;
   cin>>str;
   ofstream myfile ("\""+str+"\"");
   if (myfile.is_open())
{
for(int i=0;i<N;i++)
{
myfile << Tarr[i] ;
myfile << ",";
myfile <<Xarr[i];
myfile <<",";
myfile <<Yarr[i];
myfile <<"\n";
}
myfile.close();
}
else cout << "Unable to open file";
  
}

int main()
{
   double v;
   double h;
   double A;
   double N;
   cout<<"enter velocity, height, Angle and Number of points"<<endl;
   cin>>v>>h>>A>>N;
   double Arad=DegreeRad(A);
   double vh=v*cos(Arad);
   double vv=v*sin(Arad);
   double g=-9.81;
   double tt=RealRoots(0.5*-9.81,vv,h);
   if(tt==-1)
       cout<<"couldn't find real time.enter input again";
   double xt=vh*tt;
   double T=-1*vv/g;
double ht=h+(vv*T)+(0.5*g*T*T);
cout<<"tt="<<tt<<endl<<"xt="<<xt<<endl<<"ht="<<ht<<endl;
   for(int i=0;i<N;i++)
   {
   Xarr[i]=(i/(N-1))*xt;
       Tarr[i]=Xarr[i]/vh;
       Yarr[i]=h+(vv*Tarr[i])+(0.5*g*Tarr[i]*Tarr[i]);
   }  
char choice;
cout<<"enter 'a' for tabular display and 'b' for file output"<<endl;
cin>>choice;
switch(choice)
   {
case 'a': TabularDisplay(N);
               break;
   case 'b': FileOutput(N);
   break;
   }
      
}

θ θ https://bdpsparki e computer Science 1 DuckDucko-rx https://bdpsparki Χ × x C++ Program to 2Dimensional Tu/X -Input/Outpu

Add a comment
Know the answer?
Add Answer to:
C++ please A typical problem encountered in the study of dynamics is the trajectory problem. In...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • 9. The figure below shows the trajectory of a ball undergoing projectile motion. The time t-0s...

    9. The figure below shows the trajectory of a ball undergoing projectile motion. The time t-0s corresponds . Its launch velocity, also called are also indicated in the figure which are X0m and y- 0 m. to the moment just as the ball is launched from position the initial velocity, is g. The other two points along the trajectory corresponding the velocity of the ball at the peak ) it strikes the ground (2) at t How do the speeds...

  • Plot the Trajectory of a Projectile (by hand) from launch to maximum height • Launch angle...

    Plot the Trajectory of a Projectile (by hand) from launch to maximum height • Launch angle is 35 degrees • Launch height (y0) = 3 m • Initial velocity (V0) = 25 m/s • Acceleration ... ax = 0 , ay = -9.8 m/s2 Part 2 • Make a data table for x(t) and y(t) and plot the trajectory of the projectile (y vs. x) on graph paper • stop the plot when the maximum height is achieved • use...

  • A projectile is fired at time t=0.0 s, from point ) at the edge of a cliff

    A projectile is fired at time t=0.0 s, from point ) at the edge of a cliff, with initial velocity components of V0 x=90 m / s and V0 y=500 m / s. The projectile rises, then falls into the sea at point P. The total flight time is 125 s.(a) What is the magnitude of the velocity at time t=15.0 seconds?(b) In the figure, what is the distance D?(c) What is the height of the cliff?

  • Course # Section # Name Pre-lab B-4 Projectile Motion Read the lab manual B-4 thoroughly The...

    Course # Section # Name Pre-lab B-4 Projectile Motion Read the lab manual B-4 thoroughly The origin of our coordinate system is at the launching point (see Figure 1). If a projectile is launched horizontally (launching angle e-0.0) at a height of y above the table top, and it falls on the table top, how long does it take the projectile to fall onto the table top? Express the time in terms of g and y Let the y-direction be...

  • A ball is thrown from the ground onto a roof of height yroof 16 m from...

    A ball is thrown from the ground onto a roof of height yroof 16 m from a distance of x 7 meters away as shown in the diagram to the left. The maximum height of the ball's trajectory is ymax 3.2 meters above the top of the roof. Standard Exercise Roof Toss Standard Exercse Cliff Toss 1) Find the required initial vertical component of the velocity, V vy 0 m/s Submit Help 2) Find the time for the ball to...

  • 1. (10 points A Monte Carlo analysis can be used for optimization. For example, the trajectory...

    1. (10 points A Monte Carlo analysis can be used for optimization. For example, the trajectory of a ball can be computed with 2vo cos2 Go where y is the achieved height in meters (m), 6% is the initial angle (radians), is the horizontal distance (m), g is the gravitational acceleration (-9.81 m/s2), vo is the initial velocity (m/s), and yo is the initial height. Given yo 1 m, vo 25 m/s, and 500 (or T/180 x 50 rad), determine...

  • P. The horizontal distance of a projectile (in feet) is given by x (vo cos e)t,...

    P. The horizontal distance of a projectile (in feet) is given by x (vo cos e)t, and the height of the projectile is given by y = -16t2 + (vo sin 0)t + yo where vo is the initial velocity, e is the angle of inclination, and yo is the initial height. Suppose an object is propelled upward from the ground at an angle e to the horizontal with an initial velocity of vo ft/sec. a. Find a formula for...

  • 5.15 PROGRAM: Functions - Upset Fowls (C++) (1) Correct the first FIXME by moving the intro...

    5.15 PROGRAM: Functions - Upset Fowls (C++) (1) Correct the first FIXME by moving the intro text to the function stub named PrintIntro and then calling the PrintIntro function in main. Development suggestion: Verify the program has the same behavior before continuing. (2) Correct the second FIXME by completing the function stub GetUsrInpt and then calling this function in main. Notice that the function GetUsrInpt will need to return two values: fowlAngle and fowlVel. (3) Correct the third FIXME by...

  • A person on a cliff throws a ball with an initial velocity of 30.2 m/s at...

    A person on a cliff throws a ball with an initial velocity of 30.2 m/s at an angle of 46.50 above the horizontal. If the initial height of the ball, compared to it impact location is, Y = 18.5 m, what is the maximum height it obtains, and where does it land? (ind both as measured from the base of the cliff Max height-Number Units Distance, X NumberUnits

  • 2. (a) An object thrown vertically with a speed vo reaches a height h at time...

    2. (a) An object thrown vertically with a speed vo reaches a height h at time t, where: he vot tgt Write a MATLAB program with a user defined function that computes the time required to reach a specified height h, for a given value of vo. The function's input should be h, Vo, and g. Test your function for the case where h 100 m, Vo-50 m/s and g9,81 m/s2 by free hand. Write the MATLAB command how you...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT