C++ math.h functions

Hi guys. I'm new to the programming field (first C++ class in a CIS major). My instructor has given an assignment to calculate altitude and velocity of a weather balloon given a length of time from the user. The program is supposed to take an input of minutes from the user, convert it to hours, then use the result to calculate the velocity and altitude.I seem to be able to get most everything else working, besides these two equations. I'm sure its in the syntax but I can't for the life of me figure out what I'm doing wrong. Here's what I have so far.Oh yeah, and I'm using Microsoft Visual C++. Any help/advice would be welcome. Thanks!

#include <iostream>
#include <string>
#include <math.h>

using namespace std;

int main(int argc, char*argv[]){
float userTime;
cout<<"Enter a time interval in minutes:";
cin>>userTime;

float hours = (userTime / 60); //converts minutes to hours

float velocity = -0.48f * pow(userTime,3) - 36.0f * pow(userTime,2) - 760.0f * userTime + 4100.0f; //calculate velocity using input from user
float altitude = -0.12f * pow(userTime,4) + 12.0f * pow(userTime,3) - 380.0f * pow(userTime,2) + 4100.0f * userTime + 220.0f; //calculates altitude using input from user

cout<<"That's " << hours << " hours!"<< endl;
cout<<"The velocity is " << velocity<< endl;
cout<<"The altitude is " << altitude;


system("pause");

return 0;
}
Your coding is not very interpretable visually. Which makes it difficult for me to know what you want done...

Please reply to this thread, or edit it and post the exact worksheet information your teacher/ professor gave to you so we can re-create the code in more depth.

Your coding techniques are not very great either.

When and If you post the information to do the project I will recreate it with my techniques and explain them without big techy jargon crap.
Last edited on
Yeah, sorry for all that. As I stated, this is my first C++ class. Here are the directions he gave:

"Write a program that creates a tab delimited file, balloon.csv, that can be opened by a spreadsheet to graph the altitude and velocity of a balloon as a function of time from the time of release through 48 hours. the user, using the keyboard, will indicate the time interval in minutes. The following two formulas approximate the altitude of the balloon as A(t) meters... V(t) is velocity in meters/hr. "t" is measured in hours, so you'll need to calculate how many hours long the user's time interval is because they will use minutes."

A(t)=-0.12t^4+12t^3-380t^2+4100t+220
V(t)=-0.48t^3+36t^2-760t+4100
Well, for one assuming I'm understanding those equations correctly, you'll need to divide the whole thing by t to get A, since A(t) = *equation* you'll want A = *equation* / t. That is, of course, if I'm understanding how that's written correctly.

Also, the velocity in your code has a - where there is a + in the formula you posted.
Last edited on
#include <iostream>
#include <cmath>
#include "conio.h" // I use "conio.h" so that I can use _getch(); which is a more professional and desirable method of system("pause");
using namespace std;

int main()
{

double usertime, finaltime; // I like to categorize the storage.
double hour;
int at, vt;

cout << "Enter a Time Variable in Minutes:(1-2880)" << endl;
cin >> usertime;

hour = 60;
finaltime = usertime / hour;
vt = -0.12t^4+12t^3-380t^2+4100t+220; // plug in your code... finaltime = t. I would go to my professor to ask how to use the complicated equation. this is C++ class(for you) to learn programming, not Algebra.
at = -0.48t^3+36t^2-760t+4100; // plug in your code... finaltime = t.

cout << "The velocity is " << vt << endl;
cout << "The altitude is " << at << endl;

_getch();
return 0;
}


yeah your going to have to plug in the equations because I honestly do not understand them...

Just talk with your professor.
Last edited on
Aside from the first equation needing 'at =' instead of 'vt =' the above is a bit cleaner in terms of readability. Though I, personally, prefer to initialize my all variables when I declare them.
His code is fairly straight-forward and easy to read.
tresk21 also does a great job of naming his variables, which you two fail to do.

In mathematics, something that looks like A(t) = equation is a way of defining a function over t. In this case, the altitude is a function of the time, as is the velocity. Hence his two equations: A(t) and V(t). Given some time, t, give us an altitude and a velocity.

@tresk21
According to the information in your second post, your equation is off for velocity: the sign of the second term should be positive, not negative.

Also, since this is C++, use <cmath>:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <cmath>
#include <iostream>
#include <string>

using namespace std;

int main()
{
  float userTime;
  cout<<"Enter a time interval in minutes:";
  cin>>userTime;

  float hours = (userTime / 60); //converts minutes to hours

  float velocity = -0.48f * pow(userTime,3) + 36.0f * pow(userTime,2) - 760.0f * userTime + 4100.0f; //calculate velocity using input from user
  float altitude = -0.12f * pow(userTime,4) + 12.0f * pow(userTime,3) - 380.0f * pow(userTime,2) + 4100.0f * userTime + 220.0f; //calculates altitude using input from user

  cout<<"That's " << hours << " hours!"<< endl;
  cout<<"The velocity is " << velocity<< endl;
  cout<<"The altitude is " << altitude;

  return 0;
}

I don't understand the equations your professor gave you. They seem to be some pretty convoluted altitude and time functions... (I get negative numbers out of them).

Hope this helps.
Thank you all so much for your help. I really appreciate you guys taking your time to read and reply to my post.
Topic archived. No new replies allowed.