Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

C++ projectile assignment

  1. May 3, 2007 #1
    hi
    I am having trouble doing this assignment. I have very little knowledge of c++ and the truth is i dont even know where to begin. Can someone please help me with it. the program im supposed to be writing is as follows:

    compute the height of a projectile that was launched.
    You will recall the tutorial problem in which you computed the height of a projectile
    that was launched “straight up”. In this case the height after t seconds is given by:
    2
    2
    s u t 1 gt o = −
    Where uo is the launch velocity in m/s and the gravitational constant g is 9.8 m/s2.
    In the simulation of the motion of the projectile the movement is monitored in very
    short time intervals Δt. In a short time interval the velocity v is almost constant and
    the distance the projectile moves can be computed as Δs = v Δt. The position can then
    be updated by s = s + v * Δt ;
    The velocity changes constantly, it is reduced by the gravitational force. In a short
    time interval, Δv = -g Δt, and the velocity must be updated as v = v – g * Δt ;
    In the new iteration the new velocity is used to update the distance.
    However, most projectiles are not shot upright but at an angle to the horizontal. If the
    starting velocity has magnitude v and the starting angle of α, then the velocity is a
    vector with components vx = v cos α, vy = v sin α.
    In the x-direction the velocity does not change. In the y-direction the gravitational
    force has an effect. These equations presume there is no air resistance.
    Write a program that will perform the simulation of a projectile that has been
    launched with an initial velocity and angle. The user is to enter the initial velocity between (between 0 & 100 m/s) and launch angle (between 50 and 850).
    The program will present the results in two formats. The user should be able to choose
    which format to view.
    1. Print the results in a table format on the screen, showing the height (y position)
    and range (x position) at each time interval. Update the position every 100
    times/second, but print out the position only every full second. Repeat until the
    projectile has reached the ground level.
    2. Plot the position of the projectile on the screen.
    - Use a character display, similar to the sine wave example given earlier.
    - The display should plot the x axis of the projectile on the horizontal
    plane of the screen and y axis on the vertical plane of the screen.
    - Hint: You could store the projectile position in a 2 dimensional array
    that can represent the projectile positions on the screen. The contents
    of the array can then be printed to the screen.
    Deleted: ,
    Deleted: and the time interval
    Deleted: Use the time interval to
    u

    the reason im posting the whole thing is beause i got no idea where to even start. thanx
     
  2. jcsd
  3. May 3, 2007 #2
    Start off with a basic outline.

    What do you want the user to tell you:

    Given the user information, what tools/formulas are at your disposal:

    How will you get to the final answer:

    For formating your table a useful function is called setw, and it takes an integer parameter.
    cout << set(15);
    would output 15 white spaces between

    You will have to include the library iomanip
    #include <iomanip>

    Here is a sample code that uses whitespace (it uses Euler's approximation to solve a population ODE):
    // file: island.cxx
    // author: Andrew Holmgren
    // This program uses quantitative techniques to find the populations of foxes
    // and geese living together in an environment. The program outputs the
    // population numbers every 10 years.

    //includes that provide 1) cin & cout 2)EXIT_SUCCESS = 0 and 3)setw()
    #include <iostream>
    #include <cstdlib>
    #include <iomanip>
    using namespace std;

    //constants for the program
    const double DEATH_RATE = 0.1;
    const double CONVERSION_RATE = 0.00001;
    const double GROWTH_RATE = 0.4;
    const double FOX_ABILITY = 0.0005;
    const double CAPACITY = 30000.0;


    int main ()
    {

    //variable declarations
    int fox_pop; //a variable for storing all the new fox populations
    int goose_pop; //a variable for storing all the new goose populations
    int temp_fox_pop; //the previous year's fox population
    int temp_goose_pop; //the previous year's fox population
    int year=0; //to keep track of the years, starting at the year zero

    //prompt the user to tell us how many foxes and geese are currently alive
    cout << "How large is the current fox population?" << endl;
    cin >> fox_pop;
    cout << "How large is the current goose population?" << endl;
    cin >> goose_pop;

    //make an initial build of the table showing the populations
    cout << "\n Please wait while the populations are calculated..." << endl;
    cout << "---------- Year ---------- Foxes ---------- Geese" << endl;

    //here is a while loop that will calculate the fox and goose populations
    while (year<=100)
    {

    //make sure we don't have any negative populations, and set to zero if so
    if (fox_pop < 0)
    {
    fox_pop = 0;
    }

    //this is the same negative population prevention with the goose population
    else if (goose_pop < 0)
    {
    goose_pop = 0;
    }

    //if the the year is a factor of ten, then output the current populations
    else if ((year%10) == 0)
    {
    cout << setw(15) << year; //setw(x) makes whitespace with x number of spaces
    cout << setw(17) << fox_pop; //have the fox_pop output take up 17 spaces
    cout << setw(17) << goose_pop << endl; //print out and make new line
    }

    //increment the year everytime we enter the loop and calculate the populations
    //for that year
    ++year;

    //make temperary populations so that the new population calculations will
    //not rewrite the old population values
    temp_fox_pop = fox_pop;
    temp_goose_pop = goose_pop;

    //population formulas
    //F_n = (1 - d + bG_(n-1))F_(n-1)
    //G_n = (1 + r - rG_(n-1)/k - aF_(n-1))G_(n-1)
    fox_pop = int( ((1 - DEATH_RATE) + (CONVERSION_RATE*temp_goose_pop))
    *temp_fox_pop );
    goose_pop = int( (1 + GROWTH_RATE - (GROWTH_RATE*temp_goose_pop/CAPACITY) -
    (FOX_ABILITY*temp_fox_pop))*temp_goose_pop );

    }

    return EXIT_SUCCESS;
    }
     
  4. May 3, 2007 #3
    I just realized I have else-if statements for goose_pop<0 and if modulus of 10 is 10, when they should actually be if statements.
     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook

Have something to add?



Similar Discussions: C++ projectile assignment
Loading...