C++ numerical program with loops

Hello. I am just learning C++ and I am having troubles solving this problem statement:

Your program should start by reading in the values of the initial pointing altitude and muzzle velocity of the launcher: h0, v0, and a launch angle theta (in deg), then set the initial conditions for the model as h(0) = h0, w(0) = v0*sin(theta), x(0) = 0, and v(0) = v0*cos(theta). Have your program also read in a value for the drag constant c so that you can explore the effect of different projectile mass/shape properties. Let's assume dt = 0.1 in the simulation, and let's simulate forward for 1800 time steps (equivalent to 3 minutes simulated fight time). Thus h(180), w(180), x(180), and v(180) would be the last data calculated, corresponding to h[1800], w[1800], x[1800], and v[1800] in your C++ arrays.

After filling the arrays with the data generated from the time step propagation, your program should request a time range, then display on the screen the altitude h(t) and down range distance x(t) for any values of t in the indicated range computed by your iterative calculation. Lastly, you program should display the time, down range position, and impact speed ( sqrt(w(th)^2 + v(th)^2 ) which occur when the projectile hits the ground. Here, th is the time just before the projectile hits the ground.

We can write the time step propagation equations in C++ as:

h[k+1] = h[k] + dt*w[k];
w[k+1] = w[k] + dt*((-c * fabs(w[k]*w[k])) - 9.81);
x[k+1] = x[k] + dt*v[k];
v[k+1] = v[k] + dt*((-c * fabs(v[k]*v[k])) - 9.81);



In my code, I am having trouble printing out the altitude and down range positions for the specified range and also the impact values. My code is listed below.

Your help is greatly appreciated.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
#include <iostream>
#include <cmath>
using namespace std;

int main(void) {

	float h0, v0, theta, theta2, c, tmin, tmax, dt, k;
	float h[1801], w[1801], x[1801], v[1801];

	cout << "Enter h0, v0, and theta: ";
	cin >> h0 >> v0 >> theta;
	cout << "Enter drag constant c: ";
	cin >> c;
	cout << "Enter time range for display: ";
	cin >> tmin >> tmax;
        
        // Convert to radians
	theta2=theta*(acos(-1)/180);
	
	// Initial conditions
	h[0]=h0, w[0]=v0*sin(theta2), x[0]=0, v[0]=v0*cos(theta2), dt=0.1;

	for (int k=0; k<1800; k++) {
		h[k+1] = h[k] + dt*w[k];
		w[k+1] = w[k] + dt*((-c*fabs(w[k])*w[k])-9.81);
		x[k+1] = x[k] + dt*v[k];
		v[k+1] = v[k] + dt*((-c*fabs(v[k])*v[k])-9.81);
	}

	// Altitude and downrange position for specified range
	for (int k=tmin; k>tmax; k++) {
		cout << "At t=" << k << endl;
		cout << "  h=" << h[k] << endl;
		cout << "  x=" << x[k] << endl;
	}

	//Impact
	if (h[k]<=0) {
		cout << "Impact occured at t=" << k;
		cout << "with distance x=" << x[k];
		cout << "and velocity v=" << v[k] << endl;
	}
	
	return 0;
}
Last edited on
Line 31. What should be the end condition for this loop?

Line 38. You have to find the k. Would it be the first element in array h, where value <= 0? You can find that element by looking at one element in time until the condition is met (or end of array).
I want the loop to end once it reaches the end of the interval that the user enters, is this not how I should input it? Am I doing it correctly so that it will use the value obtained in the loop at line 23?

So do I need to add another loop in order to find the value that makes h=0 and then plug that into the if statement?

Thank you for your help.

The if-clause executes as long as the condition is true.

On first iteration k==tmin, so the condition tests tmin>tmax.
If that is true, then the element is shown.
However, if that is true, then (tmin+n)>tmax is true for all positive n, and the loop continues infinitely.

for ( size_t k = tmin; k <= tmax; ++k )
Note: you have a float k; on line 7.

Note: you don't verify that 0 <= tmin && tmin < 1801 && 0 <= tmax && tmax < 1801
You should do that before the loop.

The size_t is an unsigned integer type. Array indices cannot be negative, so lets be explicit with our indices.

The search loop is better done with while. One can break out from any loop, but ...
1
2
3
4
5
6
7
8
9
10
11
12
13
size_t j = 0;
while ( j < 1801 && 0 < h[j] )
{
  ++j;
}
if ( 1801 == j )
{
  cout << "Love is (still) in the air\n";
}
else
{
  cout << "Impact\n";
}

The loop has to test for both possibilities and in that order. It would be an error to dereference h[1801]. The AND operator is "lazy"; if the first test is false, then the second is not evaluated. We need the if after the while, because we do not know which reason did stop the loop. Again, the condition does the safe test.

However, you will want more than one loop, if your cannon is submerged/dug into a hole. First find the step, when shell reaches above ground, and then find the impact. Its just more of the same.
Topic archived. No new replies allowed.