#include <iostream>
#include <cmath>
#include <math.h>
usingnamespace std;
int main ()
{
constfloat g = 9.81;
float height_sphere1, height_sphere2, height_sphere3;
cout << "Enter height for the first sphere: ";
cin >> height_sphere1;
cout << "Enter height for the second sphere: ";
cin >> height_sphere2;
cout << "Enter height for the third sphere: ";
cin >> height_sphere3;
cout << endl;
cout << "The time for the first sphere to fall is " << ( 2 * height_sphere1 / g ) pow (1/2);
system ("pause");
return 0;
}
What's more, division of two integers will result in another integer - that means 1/2 will result in 0. So you should either write 0.5 or 1.0/2 or use sqrt instead, which is faster.
Also, it's better to avoid float unless you have special requirements in terms of execution speed and memory usage.
double provides much higher precision than float at little additional cost.
I'm pretty new to this but wouldn't it be far neater to declare a class with each successive sphere being a member of that class? How about something like this (Constructive criticism please - I just started learning):
#include <iostream>
#include <cmath>
#include <math.h>
using namespace std;
cout << "Enter height for the first sphere: ";
cin >> height;
Sphere first (height);
cout << "The time for the first sphere to fall is " << first.falltime() << endl;
cout << "Enter height for the second sphere: ";
cin >> height;
Sphere second (height);
cout << "The time for the first sphere to fall is " << second.falltime() << endl;
cout << "Enter height for the third sphere: ";
cin >> height;
Sphere third (height);
cout << "The time for the first sphere to fall is " << third.falltime() << endl;