C++ Height Calculation

Hello, I am new to c++ and I am trying to figure this out. Kindly help I will really appreciate it.


A small rocket is being designed to calculate the effect of wind shear in the vicinity of a thunderstorm. The designers predicted the performance of the rocket within this wind shear effect using the following formula ...

Height = 60 + 2.13t*2 - 0.0013t*4 + 0.000034t *4.751

(number after * is the exponent)
... where t is the elapsed time in seconds.

Write a C++ program that prompts the user for the elapsed time, computes the height (in feet) of the rocket for that given time, and outputs the height to the screen.

So far this is my code. Kindly help

#include <iostream>
#include <cmath>

using namespace std;

int main() {

// declare variables
float height, time;

//Ask for user input
cout<<"Enter time elapsed in seconds: ";

cin << time;
int a = 60;
double b = pow(2.13* time, 2);
double c = pow(0.0013* time,4);
double d = pow(0.000034* time,4.451);

// Calculate height
cout << height = a + b- c +d;
}
closed account (48T7M4Gy)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <iostream>
#include <cmath>

using namespace std;

int main()
{
    // declare variables
    double t = 0, height = 0;
    //Ask for user input
    cout<<"Enter time elapsed in seconds: ";
    cin >> t;
    
    //calculate height
    height = 60.0 + 2.13 *pow(t,2) - 0.0013*pow(t,4) + 0.000034*pow(t,4.751);
    
    cout << "time: " << t << " seconds\n";
    cout << "Height: " << height << " feet\n";
}
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
#include <iostream>
#include <cmath>

using namespace std; 

int main() {
  // declare variables near the first point-of-use.
  // use double consistently.

  cout<<"Enter time elapsed in seconds: ";
  // note the direction of the arrow >>
  double time; cin >> time;
  
  // variables whose values don't make sense to change later
  // should be marked const:
  double const a = 60.; 

  // Edit: precedence issues fixed (see below)
  double const b = 2.13 * pow(time, 2);
  double const c = 0.0013 * pow(time, 4);
  double const d = 0.000034 * pow(time, 4.451);
  double const height = a + b - c + d;

  cout << height << '\n';
}

Last edited on
I think, you've gotten tripped up by precedence, in the formula
Height = 60 + 2.13t2 - 0.0013t4 + 0.000034t4.751,
2.13t2 means 2.13(t2), not (2.13t)2

I'm an old guy who instinctively tries to save time when doing math calculations. You can compute t2 faster by just multiplying it by itself. Ditto for t4. And t4.751 is t4*t0.751. So you can get away with just one call to pow:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <iostream>
#include <cmath>

using namespace std;

int
main()
{

    double height, time;

    cout << "Enter time elapsed in seconds: ";

    cin >> time;
    double t2 = time*time;      // t^2
    double t4 = t2*t2;          // t^4
    // height = 60 + 2.13t^2 - 0.0013t^4 + 0.00003t^4.751
    // calculate t^4.751 as t^4 * t^0.751
    height = 60 + 2.13*t2 - 0.0013*t4 + 0.00003*t4*pow(time,0.751);

    cout << height << '\n';
}

Enter time elapsed in seconds: 0
60

Hmm. That doesn't seem right. The rocket starts at a height of 60? Are you sure that formula is correct?
I think, you've gotten tripped up by precedence, in the formula


Whoops ;)
Thanks for the catch. I'll have to fix my response in a bit.

Edit:
fixed.
Last edited on
Thank you, everyone!
Topic archived. No new replies allowed.