Monday, March 20, 2023
HomeSoftware EngineeringThe way to Bounce Balls in C

# The way to Bounce Balls in C

## The problem#

A toddler is taking part in with a ball on the nth ground of a tall constructing. The peak of this ground, h, is thought.

He drops the ball out of the window. The ball bounces (for instance), to two-thirds of its top (a bounce of 0.66).

His mom seems to be out of a window 1.5 meters from the bottom.

What number of instances will the mom see the ball move in entrance of her window (together with when it’s falling and bouncing?

Three circumstances should be met for a sound experiment:

• Float parameter “h” in meters should be larger than 0
• Float parameter “bounce” should be larger than 0 and fewer than 1
• Float parameter “window” should be lower than h.

If all three circumstances above are fulfilled, return a constructive integer, in any other case return -1.

Observe:

The ball can solely be seen if the peak of the rebounding ball is strictly larger than the window parameter.

Examples:

``````- h = 3, bounce = 0.66, window = 1.5, result's 3

- h = 3, bounce = 1, window = 1.5, result's -1

(Situation 2) not fulfilled).
``````

Choice 1:

``````#embrace <stdio.h>
#embrace <stdlib.h>

int bouncingBall(double h, double bounce, double window) {
if ((h <= 0) || (window >= h) || (bounce <= 0) || (bounce >= 1))
return -1;
int seen = -1;
whereas (h > window) {
seen += 2;
h = h * bounce;
}
return seen;
}
``````

Choice 2:

``````#embrace <math.h>

int bouncingBall(double h, double bounce, double window) {
if (h <= 0 || bounce <= 0 || bounce >= 1 || window >= h) {
return -1;
}
return 2*ceil(log(window/h)/log(bounce)) - 1;
}
``````

Choice 3:

``````int bouncingBall(double h, double bounce, double window) {
if(!(h>0 && bounce>0 && bounce<1 && window<h)) return -1;
int instances=1; double okay;
for(okay=h*bounce; okay>window; okay*=bounce) instances+=2;
return instances;
}
``````

## Check circumstances to validate our answer#

``````#embrace <stdlib.h>
#embrace <time.h>
#embrace <criterion/criterion.h>

extern int bouncingBall (double h, double bounce, double window);

static void testequal(double h, double bounce, double window, int anticipated) {
int precise = bouncingBall(h, bounce, window);
cr_assert_eq(precise, anticipated,
"for h = %f, bounce = %f, window = %fn"
h, bounce, window,
anticipated, precise
);
}

static int answer (double h, double bounce, double window) {
if ((h <= 0) || (window >= h) || (bounce <= 0) || (bounce >= 1))
return -1;
int seen = -1;
whereas (h > window) {
seen += 2;
h = h * bounce;
}
return seen;
}

static int randomGen(int a, int b){
int r = rand();
return r % (b - a) + a;
}

Check(bouncingBall, randomTests) {
srand(time(NULL));

enum {N = 30};

const double someheights[N] = {
12, 10.5, 144, 233, 15.25, 61, 98, 15.9, 25.8, 41.8, 67, 109, 17, 28, 46,
7.5, 12.20, 19, 3, 5, 83, 13, 21, 35.5, 57, 92, 14, 24, 39, 6.5
};
const double someBounces[N] = {
0.6, 0.6, 0.6, 0.6, 0.6, 1.1, 9, 1, 0.6, 0.6, 0.6, 0.75, 0.75, 0.75, 0.75,
0.75, 12.20, 0.75, 0.75, 0.83, 0.13, 0.21, 0.35, 0.57, 0.9, 0.14, 0.24, 0.39, 0.65, 0.65
};
const double somewin[N] = {
1.5, 1.5, 1.44, 2.33, 1, 6.1, 9.8, 1.9, 2.8, 4.8, 3, 1.09, 1.7, 2.8, 46, 7.5,
12.20, 1.9, 3, 5, 0.83, 1.3, 2.1, 3.5, 0.57, 0.92, 1.4, 2.4, 3.9, 6.5
};

for (int okay = 0; okay < 100; okay++) {
int i = randomGen(0, N - 1);
double h = someheights[i];
double b = someBounces[i];
double w = somewin[i];
testequal(h, b, w, answer(h, b, w));
}
}

Check(bouncingBall, sampleTests) {
testequal(2, 0.5, 1.0, 1);
testequal(3, 0.66, 1.5, 3);
testequal(30, 0.66, 1.5, 15);
testequal(30, 0.75, 1.5, 21);
testequal(30, 0.4, 10, 3);
testequal(40, 0.4, 10, 3);
testequal(10, 0.6, 10, -1);
testequal(40, 1, 10, -1);
testequal(-5, 0.66, 1.5, -1);
testequal(5, -1, 1.5, -1);
testequal(4, 0.25, 1.0, 1);
}
``````
RELATED ARTICLES