May 12, 2011 at 8:34pm May 12, 2011 at 8:34pm UTC
Hey hey the answer iam am suppsoed to output is 4... but it seems no matter what i keep getting 0? any ideas?
#pragma intrinsic(sqrt, pow)
#include <iostream>
#include <math.h>
#include <cmath>
#include <iomanip>
using namespace std;
const float G = 6.674e-011;//0.00000000006674;
const float M = 5.974e+024;//5974000000000000000000000.0;
float g1 = 9.803;
float g2 = 9.792;
float height = 0.0;
float calculateHeight(const float G, const float M, float g1, float g2)
{
float height = 0.0;
height = sqrt(G*M)*((pow(1/g2,1/2))-(pow(1/g1,1/2)));
return height;
}
int main ()
{
height = calculateHeight(G, M, g1, g2);
cout << setprecision(30);
cout << "The Balloon is " << height << " meters High in the sky!" << endl;
system("pause");
return 0;
}
May 12, 2011 at 8:38pm May 12, 2011 at 8:38pm UTC
Try this code and see if you can work it out. This is the beginning of learning to debug, so that you can work these things out for yourself.
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
#include <iostream>
#include <math.h>
#include <cmath>
#include <iomanip>
using namespace std;
const float G = 6.674e-011;//0.00000000006674;
const float M = 5.974e+024;//5974000000000000000000000.0;
float g1 = 9.803;
float g2 = 9.792;
float height = 0.0;
float calculateHeight(const float G, const float M, float g1, float g2)
{
float height = 0.0;
cout << "sqrt(G*M) = " << sqrt(G*M) << endl;
cout << "pow(1/g2,1/2) = " << pow(1/g2,1/2) << endl;
cout << "pow(1/g1,1/2) = " << pow(1/g1,1/2) << endl;
height = sqrt(G*M)*((pow(1/g2,1/2))-(pow(1/g1,1/2)));
cout << "h1 = " << height << endl;
return height;
}
int main ()
{
height = calculateHeight(G, M, g1, g2);
cout << setprecision(30);
cout << "The Balloon is " << height << " meters High in the sky!" << endl;
system("pause" );
return 0;
}
Last edited on May 12, 2011 at 8:39pm May 12, 2011 at 8:39pm UTC
May 12, 2011 at 8:47pm May 12, 2011 at 8:47pm UTC
Ah yeah that really shows what values are popping out. they keep equaling 1 and subtracting to zero. Why is that? Or is that... the answer?
May 12, 2011 at 8:48pm May 12, 2011 at 8:48pm UTC
I tried putting that Setprecision in there. Cause i believe it rounds it to 1
May 12, 2011 at 8:57pm May 12, 2011 at 8:57pm UTC
Don't include both math.h and cmath .
cmath is the C++ version of math.h , improved to be proper C++. Including both is insanity.
Try the sqrt function instead of pow to the half. You'll get closer...
May 12, 2011 at 9:01pm May 12, 2011 at 9:01pm UTC
your variables are global, so there is no need to pass them in the function.
you should declare them in main.
May 12, 2011 at 9:05pm May 12, 2011 at 9:05pm UTC
Try 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
#include <iostream>
#include <cmath>
#include <iomanip>
using namespace std;
const float G = 6.674e-011;//0.00000000006674;
const float M = 5.974e+024;//5974000000000000000000000.0;
float g1 = 9.803;
float g2 = 9.792;
float calculateHeight(const float G, const float M, float g1, float g2)
{
float height = 0.0;
float a,b,c,d,e;
a = sqrt(G*M);
b = sqrt(1/g2);
c= sqrt(1/g1);
d = b - c;
e = a*d;
cout << "sqrt(G*M) = " << a << endl;
cout << "sqrt(1/g2) = " << b << endl;
cout << "sqrt(1/g1) " << c << endl;
height = a * d;
float h2 = sqrt(G*M)*((pow(1/g2,1/2))-(pow(1/g1,1/2)));
cout << "height = " << e << endl;
cout << "Calc all at once = " << h2 << endl;
return e;
}
int main ()
{
float height;
height = calculateHeight(G, M, g1, g2);
cout << setprecision(30);
cout << "The Balloon is " << height << " meters High in the sky!" << endl;
return 0;
}
Then go and read about how computers actually store numbers.
Last edited on May 12, 2011 at 9:06pm May 12, 2011 at 9:06pm UTC
May 13, 2011 at 8:21am May 13, 2011 at 8:21am UTC
Sorry I have not replied ealier. Thanks for the responds. Yes i am new to this as you can see. I will try these out.