#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;
}
When I try to build solution I get these errors
1 2 3
1>d:\Nikolay\cs\lab3\lab3\p1.cpp(9): warning C4305: 'initializing' : truncation from 'double' to 'const float'
1>d:\Nikolay\cs\lab3\lab3\p1.cpp(18): error C2146: syntax error : missing ';' before identifier 'pow'
1>d:\Nikolay\cs\lab3\lab3\p1.cpp(18): error C2661: 'pow' : no overloaded function takes 1 arguments
I tried include <cmath> and even <math.h> but still does not work.
Please help to tell me what I am doing wrong all help much appreciated
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;