How to Simulate Projectile Motion in C++?

In summary: FOX POP: " << fox_pop << endl; GOOSE POP: " << goose_pop << endl; else { cout << "FOX POP: " << fox_pop << endl
  • #1
riza
1
0
hi
I am having trouble doing this assignment. I have very little knowledge of c++ and the truth is i don't even know where to begin. Can someone please help me with it. the program I am 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 I am posting the whole thing is beause i got no idea where to even start. thanx
 
Technology news on Phys.org
  • #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;
}
 
  • #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.
 

1. What is a "C++ projectile assignment"?

A "C++ projectile assignment" is a coding task that involves creating a program in the C++ programming language to simulate the motion of a projectile. The goal of the assignment is to use mathematical equations and programming techniques to accurately model the trajectory of the projectile given certain initial conditions.

2. What are the main components of a C++ projectile program?

The main components of a C++ projectile program include variables to store initial conditions such as velocity and angle, mathematical equations to calculate the trajectory, and a loop to update the position of the projectile over time. Additional components may include input and output functions, error handling, and graphical user interface elements.

3. How do you calculate the trajectory of a projectile in C++?

The trajectory of a projectile can be calculated using the equations of projectile motion, which involve variables such as initial velocity, angle, and time. In C++, these equations can be implemented using basic arithmetic operations and math functions from the cmath library. It is also important to consider factors such as air resistance and gravity when calculating the trajectory.

4. What are some common challenges when working on a C++ projectile assignment?

Some common challenges when working on a C++ projectile assignment include accurately implementing the mathematical equations, handling errors and edge cases, and ensuring that the program runs efficiently. It is also important to properly validate user input and consider any external factors that may affect the motion of the projectile.

5. How can I improve my C++ projectile assignment?

To improve your C++ projectile assignment, you can try implementing additional features such as a graphical user interface, incorporating real-life data, or optimizing the code for better performance. It can also be helpful to seek feedback from others and make any necessary revisions to improve the accuracy and functionality of the program.

Similar threads

Replies
4
Views
1K
  • Introductory Physics Homework Help
Replies
3
Views
443
  • Programming and Computer Science
Replies
2
Views
16K
  • Introductory Physics Homework Help
Replies
11
Views
788
  • Programming and Computer Science
Replies
4
Views
1K
  • Introductory Physics Homework Help
Replies
7
Views
92
  • Engineering and Comp Sci Homework Help
Replies
1
Views
2K
Replies
2
Views
2K
  • Classical Physics
Replies
25
Views
1K
  • Introductory Physics Homework Help
Replies
15
Views
21K
Back
Top