Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <stdbool.h>
- #include <iomanip>
- using namespace std;
- double coeffOfRestitution[4] = {0.7,0.75,0.09,0.3} ;
- bool validate(int ball_id)
- {
- if (ball_id > 3 || ball_id < 0)
- return false ;
- return true ;
- }
- void compute(int ball_id , double initial_height )
- {
- cout<<"Coefficient of restitution: "<<setprecision(2) << fixed<<coeffOfRestitution[ball_id]<<endl ;
- double current_height = initial_height ;
- int nb_bounce = 0 ;
- double distance = 0 ;
- while (current_height > 0.1)
- {
- nb_bounce++ ;
- distance+=current_height ;
- current_height *= coeffOfRestitution[ball_id] ;
- if (current_height >= 0.1)
- distance+=current_height ;
- }
- //distance-=current_height ;
- cout<<"Number of bounces: "<<nb_bounce<<endl;
- cout<<"Meters traveled for ball: "<<setprecision(2) << fixed<<distance<<endl ;
- }
- int main()
- {
- int ball_id ;
- cout<<"Enter the type of ball (0 to 3) :" ;
- cin>>ball_id;
- bool verified = validate(ball_id) ;
- while (!verified)
- {
- cout<<"Please enter 0 to 3 .."<<endl ;
- cout<<"Enter the type of ball (0 to 3) :";
- cin>>ball_id;
- verified = validate(ball_id) ;
- }
- double initial_height ;
- cout<<"Enter initial height in meters:" ;
- cin>>initial_height ;
- while (initial_height < 0)
- {
- cout<<"Enter initial height in meters:" ;
- cin>>initial_height ;
- }
- compute(ball_id ,initial_height);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement