bouncing ball (due by 5pm today!)

Hey all. My first post here. Currently taking a programming course and having a little trouble getting the right code sorted out.

the problem is to create a code that will take a ball (with only vertical displacement, h) and will output the final height and velocity at a time tf. all depending on the coefficient of restitution, alpha.

now i figured i need a for loop and if statements for when h<=0, the formulas need to reset. here is what i have so far and am really clueless as to what i need to do to fix the issue.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#include <cmath>
#include <iostream>
using namespace std;

int main(void) {
	float h0,h,h1;
	float v0,v,v1;
	float tf,t,tinc,t0;
	float g,alpha;
	
	cout << "Enter values for h0, v0, and alpha : ";
	cin >> h0 >> v0 >> alpha;
	cout << "Enter desired final time : " ;
	cin >> tf;
	g=-32.2;
	t0=0;
	tinc=.01;
	for (t=t0;t<=tf;t+=tinc){
		v1=v0-g*(t-t0);
		h1=h0+v0*(t-t0)-(.5*g*pow((t-t0),2));

		if (h1<=0){
			v0=-1*alpha*v1;
			t0=t;
			h0=0;
			v=v0-g*(t+tinc-t0);
			h=h0+v0*(t+tinc-t0)-(.5*g*pow((t+tinc-t0),2));
		}
		else {
		v=v1;
		h=h1;
		}
			cout << "At t = " << tf << ", h = " << h << " and v = " << v; 
		}

return 0;
}


any help would be appreciated guys
Last edited on
Topic archived. No new replies allowed.