Skip to content

Commit 3c2b1d1

Browse files
committed
Code for bouncing ball problem
1 parent e5c8f06 commit 3c2b1d1

File tree

2 files changed

+46
-0
lines changed

2 files changed

+46
-0
lines changed

BouncingBall/CMakeLists.txt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
cmake_minimum_required(VERSION 3.15)
2+
project(BouncingBall)
3+
4+
set(CMAKE_CXX_STANDARD 14)
5+
6+
add_executable(BouncingBall main.cpp)

BouncingBall/main.cpp

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/**
2+
* @file main.cpp
3+
* @author Arun Kumar Devarajulu
4+
* @date February 2, 2020
5+
* @copyright Copyright 2020, Arun Kumar Devarajulu. All rights reserved
6+
*/
7+
#include <iostream>
8+
9+
/**
10+
* @brief Class for solving the problem of bouncing ball
11+
*/
12+
class Bouncingball {
13+
public:
14+
/**
15+
* @brief Static member function to calculate the number of bounces made
16+
* by a ball after deprecation of height by bounce % in every turn
17+
* @param h is the height of the launch location
18+
* @param bounce is the deprecation factor
19+
* @param window is the height of the observer
20+
* @return total number of bounces as an integer
21+
*/
22+
static int bouncingBall(double h, double bounce, double window);
23+
};
24+
25+
int Bouncingball::bouncingBall(double h, double bounce, double window) {
26+
if(h > 0 && 0 < bounce && bounce < 1 && window < h) {
27+
int total_bounces = 1;
28+
h *= bounce;
29+
while(h > window) {
30+
total_bounces += 2;
31+
h *= bounce;
32+
}
33+
return total_bounces;
34+
} else return -1;
35+
}
36+
37+
int main() {
38+
std::cout << Bouncingball::bouncingBall(77, 0.83, 13) << std::endl;
39+
return 0;
40+
}

0 commit comments

Comments
 (0)