@polishprogrammer,
This is only a skim through. General comments are that you are using a mix of exact solutions (to get tgr1, vgr1 etc) to get the segment end points and stepping to get the intermediate results. Your only inputs are a1 and a2, which is correct. a3 would follow from everything else.
You are using a lot of "magic numbers", which make it tricky to follow with the large numbers involved because of the metre-second units.
You will be wrong because of integer division at several places. Some of your intermediate values are also wrong.
1 2
|
tgr1=sqrt(20000/a1);
vgr1=vi+a1*tgr1;
|
This is correct: t1 = sqrt( 2s1/a1 ) and v = u + at. However, it would be easier to follow if you defined a variable s1 = 10000 (to represent your initial distance in metres).
for(i=1; i<=tgr1; i++)
Sorry, but that just isn't going to work. i is just a counter; it is the time t you should be comparing with tgr1.
- This occurs in multiple places; I won't repeat the comments again.
s=si+vi*t+(1/2)*a1*t*t;
Corresponds to exact solution s = ut + (1/2)at
2. However, it will be WRONG here in code because of integer division. Writing 1/2 in code has to give an int, so truncates to 0. Just write 0.5 instead.
1 2
|
tgr2=-vgr1*sqrt(v*v+20000*a2);
vgr2=vgr1+a2*tgr2;
|
No idea where the first formula comes from, but it is dimensionally wrong and impossible to see how tgr2 could be negative. Also, v hasn't been set. Better to find vgr2 first as
vgr2 = sqrt( vgr1*vgr1 + 2 * a2 * s2 );
with s2 = 5000. Then
tgr2 = ( vgr2 - vgr1 ) / a2;
s=10000+vgr1*t+(1/2)*a2*t*t;
would be correct, except that (1/2) -> 0 again, by integer division. Also, 10000 is a magic number.
1 2
|
if(t+tgr1==T)
cout<<s;
|
No idea what this is supposed to do. (Several similar like this).
1 2
|
tgr3=1/vgr2;
vgr3=vgr2;
|
In metres, "1" should actually be 1000, or, better still, s3.
s=15000+vgr2*t;
This is the exact solution and OK (but 15000 is another "magic number")
1 2
|
tgr4=8000/vgr3;
a3=-vgr3/tgr4;
|
OK, first corresponds to t = s / (0.5(u+v) ). But would be easier to see if you didn't have 8000 as a magic number. Second is correct.
s=16000+vgr3*t+(1/2)*(a3)*t*t;
Corresponds to "s=ut + (1/2)at
2". Suffers from integer division again, as 1/2 -> 0 in code.
You are only outputting anything at a small number of points. I don't think that is what your assignment asked.