C Exercise Example20
# C Programming Tutorial: Free-Falling Ball Physics Simulation (Exercise Example 20)
In this tutorial, we will explore a classic physics-based programming problem: simulating a free-falling ball that bounces repeatedly. This exercise is excellent for practicing loop structures, floating-point arithmetic, and mathematical modeling in C.
---
## Problem Description
A ball is dropped from an initial height of **100 meters**. Each time it hits the ground, it bounces back up to **half of its previous height** before falling again.
Write a C program to calculate:
1. The **total distance** the ball has traveled when it hits the ground for the **10th time**.
2. The **rebound height** after the **10th bounce**.
---
## Mathematical Analysis
To solve this problem programmatically, we need to break down the movement of the ball into individual phases:
1. **The First Drop:**
* The ball falls from $100\text{ m}$ and hits the ground for the first time.
* **Distance traveled ($s$):** $100\text{ m}$.
* **Rebound height ($h$):** $100 / 2 = 50\text{ m}$.
2. **Subsequent Bounces (from the 2nd drop onwards):**
* For every subsequent bounce, the ball travels up to its peak height and then falls back down.
* This means for each bounce $i$ (where $i \ge 2$), the distance added to the total is **twice** the current rebound height ($2 \times h$).
* After traveling this distance, the rebound height is halved again ($h = h / 2$).
### Visualizing the Path
| Bounce Count | Action | Distance Added in this Step | Total Distance ($s$) | Next Rebound Height ($h$) |
| :--- | :--- | :--- | :--- | :--- |
| **1st Drop** | Falls from $100\text{ m}$ | $100$ | $100$ | $50$ |
| **2nd Drop** | Rises to $50\text{ m}$ & falls | $2 \times 50 = 100$ | $200$ | $25$ |
| **3rd Drop** | Rises to $25\text{ m}$ & falls | $2 \times 25 = 50$ | $250$ | $12.5$ |
---
## C Source Code Implementation
Here is the complete, optimized C program to solve this problem. We use `#define` preprocessor directives for configuration to make the code clean and maintainable.
```c
#include
#define INITIAL_HEIGHT 100.0
#define BOUNCE_COUNT 10
int main()
{
double h = INITIAL_HEIGHT;
double s = INITIAL_HEIGHT;
// Calculate the rebound height after the 1st bounce
h /= 2;
// Calculate subsequent bounces and total distance
for (int i = 2; i <= BOUNCE_COUNT; i++) {
s += 2 * h; // Add both upward and downward paths
h /= 2; // Calculate the next rebound height
}
// Output the results formatted to 5 decimal places
printf("At the %dth bounce, the total distance traveled is %.5f meters.\n", BOUNCE_COUNT, s);
printf("The rebound height after the %dth bounce is %.5f meters.\n", BOUNCE_COUNT, h);
return 0;
}
```
---
## Output
When you compile and run the program, it produces the following output:
```text
At the 10th bounce, the total distance traveled is 299.60938 meters.
The rebound height after the 10th bounce is 0.09766 meters.
```
---
## Key Considerations & Best Practices
### 1. Floating-Point Precision
We use the `double` data type instead of `float` to prevent precision loss. Since the height is halved repeatedly ($10$ times in this example, but potentially much more in other simulations), using double-precision floating-point variables ensures the accuracy of the fractional parts.
### 2. Loop Initialization
Notice that the loop starts at `i = 2`. This is because the first drop is unique (it only goes down, not up and down). By handling the first drop and its subsequent rebound height outside the loop, we simplify the loop logic to cleanly handle the symmetric up-and-down paths of the remaining bounces.
### 3. Formatting Output
In the `printf` function, we use the format specifier `%.5f` to limit the output to 5 decimal places. This keeps the output clean and readable while maintaining high precision.
YouTip