PPS Lab · Experiment 8
Time taken by a falling ball to reach each floor
CExpression evaluationJNTUH R22 · CS108ES
Aim
To write a C program that finds the time taken by a ball dropped from the top of a 10-floor building (3 m per floor) to fall past each floor, using s = ut + ½at² with u = 0 m/s and a = 9.8 m/s².
Algorithm
- Set the number of floors to 10, the floor height to 3 m and the acceleration a to 9.8 m/s². The initial velocity u is 0 because the ball is dropped.
- Since u = 0, the formula s = ut + ½at² becomes s = ½at², so t = √(2s / a).
- For each floor count k from 1 to 10, find the distance fallen s = k × 3.
- Find the time t = √(2s / a).
- Print k, s and t as one row of a table.
Program
#include <stdio.h>
#include <math.h>
#define FLOORS 10
#define FLOOR_HEIGHT 3.0 /* metres */
#define A 9.8 /* acceleration due to gravity in m/s^2 */
/* The ball is dropped, so the initial velocity u is 0 m/s */
int main(void) {
printf("Ball dropped from the top of a %d-floor building (%.0f m per floor)\n\n",
FLOORS, FLOOR_HEIGHT);
printf("Floors fallen Distance (m) Time (s)\n");
for (int k = 1; k <= FLOORS; k++) {
double s = k * FLOOR_HEIGHT;
/* s = ut + (1/2)at^2 with u = 0 gives t = sqrt(2s / a) */
double t = sqrt(2 * s / A);
printf("%13d %12.1f %8.2f\n", k, s, t);
}
printf("\nThe ball reaches the ground after %.2f s.\n",
sqrt(2 * FLOORS * FLOOR_HEIGHT / A));
return 0;
}Output
Ball dropped from the top of a 10-floor building (3 m per floor)
Floors fallen Distance (m) Time (s)
1 3.0 0.78
2 6.0 1.11
3 9.0 1.36
4 12.0 1.56
5 15.0 1.75
6 18.0 1.92
7 21.0 2.07
8 24.0 2.21
9 27.0 2.35
10 30.0 2.47
The ball reaches the ground after 2.47 s.Recorded by compiling and running this exact program.
How it works
The syllabus does not say how the floors are counted, so this program counts the floors the ball has fallen past from the top: after 1 floor the ball has fallen 3 m, and after 10 floors (30 m) it reaches the ground.
Because the initial velocity u is 0, the term ut is 0 and the formula can be solved for t directly: t = √(2s / a). Check of the last row: t = √(2 × 30 / 9.8) = √6.12 ≈ 2.47 s. If u were not 0, t would have to be found by solving the quadratic equation ½at² + ut − s = 0.
The times do not grow evenly: the ball takes 0.78 s to fall the first floor but only about 0.13 s to fall the last one, because it keeps speeding up.
Common mistakes
Writing
1/2 * a * t * tin C.1/2is integer division and gives 0, so the whole result becomes 0. Write0.5or1.0 / 2.Using
^for squares, as int^2. In C^is bitwise XOR; writet * tor usepow.Forgetting
math.hforsqrt, or-lmwhen compiling on Linux.Declaring the distance or time as
int. The times are fractions of a second, so they must bedouble.
Viva questions
Try to answer each one aloud before you open it.
How is t found from s = ut + ½at² when u = 0?
With u = 0 the formula is s = ½at². Multiplying by 2 and dividing by a gives t² = 2s / a, so t = √(2s / a).
What is the value of 1/2 in C, and why does it matter here?
1/2is 0, because both numbers are integers and integer division drops the fraction. Writing1/2 * a * t * twould therefore always give 0. Use0.5or1.0 / 2.Why does the ball take less time to fall each later floor?
The ball accelerates, so its speed keeps increasing. Each 3 m floor is covered faster than the one before.
What does
It gives a name to a constant value, such as
#define A 9.8. The preprocessor replaces each use of the name with its value before compiling, and the value can be changed in one place.How long does the ball take to reach the ground?
The total height is 10 × 3 = 30 m, so t = √(2 × 30 / 9.8) ≈ 2.47 s.