Simulation of a falling ball I made a simple simulation of a falling ball. Is it possible to somehow improve or optimize this code? What tips can you give for development?
#include &lt;iostream&gt;
#include &lt;cstdlib&gt;
#include &lt;cmath&gt;
using namespace std;

int timeFallingBall() {
    static int time(0);
    return ++time;
}
void fallingBall() {
    cout &lt;&lt; &quot;From what height do we drop the ball? (In meters): &quot;;
    double h;                                           
    cin &gt;&gt; h;
    double v = 0, high = 0, s = 0, maxv = 0, g = 9.8;   
    int t = 0;
    double hi = h;
    while (h) {
        t = timeFallingBall();
        v = static_cast&lt;double&gt;(t) * g;
        high = (v * static_cast&lt;double&gt;(t)) / 2;
        h = hi - high;
        if (h &lt;= 0) {
            switch (t) {
            case 1:
                cout &lt;&lt; &quot;After &quot; &lt;&lt; t &lt;&lt; &quot; second the ball has reached the ground! Maximum speed: &quot; &lt;&lt; maxv &lt;&lt; &quot; m/s&quot;;
                break;      
            default:
                cout &lt;&lt; &quot;After &quot; &lt;&lt; t &lt;&lt; &quot; seconds the ball has reached the ground! Maximum speed: &quot; &lt;&lt; maxv &lt;&lt; &quot; m/s&quot;;
            }
            break;
        }
        switch (t) {
        case 1:
            cout &lt;&lt; &quot;After &quot; &lt;&lt; t &lt;&lt; &quot; second, the ball is at a distance of &quot; &lt;&lt; h &lt;&lt; &quot; m from the ground at a speed: &quot; &lt;&lt; v &lt;&lt; &quot; m/s&quot; &lt;&lt; endl;
            break;
        default:
            cout &lt;&lt; &quot;After &quot; &lt;&lt; t &lt;&lt; &quot; seconds, the ball is at a distance of &quot; &lt;&lt; h &lt;&lt; &quot; m from the ground at a speed: &quot; &lt;&lt; v &lt;&lt; &quot; m/s&quot; &lt;&lt; endl;
            break;
        }
        if (v &gt; maxv) {
            maxv = v;
        }
    }
}
int main() {
    system(&quot;chcp 1251&gt;nul&quot;);
    fallingBall();
    return 0;
}
using namespace std; is a bad habit to get into, as it can lead to name-collisions and other issues. It's best to explicitly qualify the names we need where necessary, e.g. std::cout.

A few more line-breaks would be useful in the code. Think of code with a specific purpose as a paragraph when writing text. For example, this could be considered one &quot;paragraph&quot;, so should have an empty line after it.
 std::cout &lt;&lt; &quot;From what height do we drop the ball? (In meters): &quot;;
 double h;                                           
 std::cin &gt;&gt; h;


We have several similarly named variables h, high and hi. This makes the code harder to understand.

Variables that don't change should be marked const. (e.g. g).

Variables should generally be declared as close to the point of use as possible, set to immediately useful values, and not reused (unless they consume significant resources). For example, t, v, high and h can all be declared where they are set inside the loop.

s does not appear to be used.

We have no way to reset the static time variable in timeFallingBall(). This means we can't call fallingBall a second time, to drop another ball. We can easily just increment t in the fallingBall function instead.

while (h) translates to while (h != 0). Since h is a double with a calculated value, it's a bad idea to compare it with an exact number. We should use while (h &gt; 0.0) instead, or while (true) since we handle the exit condition inside the loop.

In C++, number literals have specific types: 0 is an int, 0.f is a float, 0.0 is a double. When comparing or assigning to variables, it's best to use the literal style of the correct variable type, rather than relying on conversions.

std::endl outputs a newline, but also flushes the output stream unnecessarily. We can output &quot;\n&quot; to get a newline without the flush.

Note that given the constant downwards acceleration, the maximum velocity is always going to be the velocity when the ball hits the ground. So we don't really need to keep track of it.

(Obviously the 1 second resolution makes the max speed output rather incorrect).



Applying the above, I'd probably go with something like:
void fallingBall()
{
    std::cout &lt;&lt; &quot;From what height do we drop the ball? (In meters): &quot;;
    double h_start;
    std::cin &gt;&gt; h_start;

    if (h_start &lt;= 0.0)
    {
        std::cout &lt;&lt; &quot;The ball is already on the ground!\n&quot;;
        return;
    }

    const double g = 9.81;

    double t = 0.0;

    while (true)
    {
        t += 1.0;

        const double h_current = h_start - 0.5 * g * t * t;
        const double v_current = g * t;

        if (h_current &lt;= 0.0)
        {
            std::cout &lt;&lt; &quot;After second &quot; &lt;&lt; t &lt;&lt; &quot;, the ball has reached the ground! Maximum speed: &quot; &lt;&lt; v_current &lt;&lt; &quot; m/s\n&quot;;
            break;
        }

        std::cout &lt;&lt; &quot;After second &quot; &lt;&lt; t &lt;&lt; &quot;, the ball is at a distance of &quot; &lt;&lt; h_current &lt;&lt; &quot; m from the ground at a speed: &quot; &lt;&lt; v_current &lt;&lt; &quot; m/s\n&quot;;
    }
}