Looping calculations and outputting them?

I'm having trouble writing the correct loops for an assignment. I have all the Math and Functions written; I'm just having a hard time understanding how to write the loops in order to calculate and show the correct output. Essentially, this is what I need to do:

A small test rocket is being designed for use in testing a retrorocket that is intended to permit “soft” landings. The designers have derived the following equations that they believe will predict the performance of the test rocket.

Note: The distance equation gives the height above ground level at time t and the first term (90) is the height in feet above ground level of the launch platform that will be used.

In order to check the predicted performance, the rocket will be “flown” on a computer, using the derived equations.

Write a program to cover a maximum flight of 100 seconds. The output should be of the following form:

TIME ACCELERATION VELOCITY DISTANCE
(sec) (ft/sec2) (ft/sec) (ft)

XXX.XX XXX.XX XXX.XX XXXX.XX

(starting with 0.0 sec)

Increments of time are to be 2.0 seconds, from launch through the ascending and descending portions of the trajectory until the rocket descends to within 60 feet of ground level. Below 60 feet the time increments are to be 0.05 seconds. If the rocket impacts prior to 100 seconds, the program is to be stopped immediately after impact.


This is the code I've written so far:

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
46
47
48
49
50
51
52
53
54
55
56
#include <iostream>
#include <iomanip>
#include <cmath>

using namespace std;

double AccelerationCalc(int t)
{
    double Acceleration = 0;
    Acceleration = 4.25 - 0.15*pow(t,2) + 6.07*pow(t,2.751)/(9995);

    return Acceleration;
}

double VelocityCalc(int t)
{
    double Velocity = 0;
    Velocity = 4.25*t - 0.15*pow(t,3)/(3) + 6.07*pow(t,3.751)/(3.751 * 9995);

    return Velocity;
}

double DistanceCalc(int t)
{
    double Distance = 0;
    Distance = 90 + 4.25*pow(t,2)/(2) - 0.15*pow(t,4)/(12) + 6.07*pow(t,4.751)/(4.751 * 37491);

    return Distance;
}

int main()
{


    // t = time in seconds

    double t = 2.0;
    double time = 0.0;
    double Acceleration = 0;
    double Velocity = 0;
    double Distance = 0;

    cout << left << setw(25) << "TIME" << setw(25) << "ACCELERATION" << setw(25) << "VELOCITY" << setw(25) << "DISTANCE" << endl;
    cout << setw(25) << "(sec)" << setw(25) << "(ft/sec^2)" << setw(25) << "(ft/sec)" << setw(25) << "(ft)\n" << endl;

    for(time = 0.0; time <=100.0; time+= 2.0)
    {
        Acceleration = AccelerationCalc(time);
        Velocity = VelocityCalc(time);
        Distance = DistanceCalc(time);

        cout << setw(25) << Acceleration << setw(25) << Velocity << setw(25) << Distance << endl;
    }

    return 0;
}



The output isn't correct and I'm not sure how I should code the loops. The output should look like this:

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
46
Time  Acceleration  Velocity  Distance
(sec)     (ft/sec2)  (ft/sec)      (ft)

0.00          4.25      0.00     90.00
2.00          4.19      8.46     98.48
4.00          4.04     16.71    123.70
6.00          3.79     24.55    165.05
8.00          3.48     31.84    221.55
10.00          3.09     38.41    291.92
12.00          2.66     44.17    374.65
14.00          2.17     49.00    467.98
16.00          1.66     52.84    570.00
18.00          1.11     55.62    678.63
20.00          0.55     57.29    791.72
22.00         -0.01     57.83    907.02
24.00         -0.58     57.23   1022.27
26.00         -1.15     55.49   1135.17
28.00         -1.70     52.65   1243.49
30.00         -2.22     48.73   1345.04
32.00         -2.71     43.79   1437.72
34.00         -3.17     37.90   1519.56
36.00         -3.58     31.14   1588.73
38.00         -3.94     23.61   1643.59
40.00         -4.24     15.42   1682.72
42.00         -4.47      6.70   1704.92
44.00         -4.63     -2.41   1709.26
46.00         -4.70    -11.76   1695.13
48.00         -4.69    -21.17   1662.20
50.00         -4.59    -30.47   1610.53
52.00         -4.38    -39.46   1540.53
54.00         -4.07    -47.94   1453.03
56.00         -3.65    -55.68   1349.27
58.00         -3.10    -62.44   1230.98
60.00         -2.42    -67.99   1100.34
62.00         -1.62    -72.05    960.04
64.00         -0.67    -74.36    813.33
66.00          0.42    -74.63    664.00
68.00          1.67    -72.56    516.40
70.00          3.07    -67.85    375.54
72.00          4.64    -60.16    247.02
74.00          6.38    -49.18    137.12
76.00          8.29    -34.54     52.80
78.00         10.39    -15.88      1.70
78.05         10.44    -15.36      0.92
78.10         10.50    -14.84      0.17
78.15         10.55    -14.31     -0.56

Last edited on
Can you let us know your broken output?
This is what my output looks like when I run my code.

https://i.sli.mg/26el7v.png

Note: I know the time portion is missing. I'm not exactly worried about that right now since the output of the calculations aren't right.
Last edited on
You are calling your functions with parameter time (a double), but the functions expect to receive an int.
That I can fix. The trouble I'm having is with how to write the loops so that they calculate at an increment of 2.0 seconds from launch through the ascending and descending portions. When the Rocket gets to 60 feet the increment goes from 2.0 down to 0.05 seconds. If the rocket impacts prior to 100 seconds then it should stop.

I don't know how to write the loops for the calculations.
Your code (as supplied) increments by 2.0 always. You can, however, adjust your increment to depend on the current value of Distance (and, presumably, the sign of the velocity, or it will do this on the ascent phase as well as the descent).
How would I write that in the loop that I have already?
Topic archived. No new replies allowed.