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 ...
(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.
#include <iostream>
#include <cmath>
usingnamespace 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:
doubleconst a = 60.;
// Edit: precedence issues fixed (see below)
doubleconst b = 2.13 * pow(time, 2);
doubleconst c = 0.0013 * pow(time, 4);
doubleconst d = 0.000034 * pow(time, 4.451);
doubleconst height = a + b - c + d;
cout << height << '\n';
}
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: