Click here to Skip to main content
15,867,488 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
#include<stdio.h>
void main()
{
int a,u,t,t1,t2,i;
float s;
printf("ENTER THE VALUES OF a,u,t,t1,t2:");
scanf("%d%d%d%d%d",&a,&u,&t,&t1,&t2);
for(i=t1;i<=t2;i=i+t)
{
s=(u*i)+(0.5*a*i*i); // calculate the total distance
printf("\n\nthe distance travelled in %d seconds is %f ",i,s);
getch();
}
}

What I have tried:

I am unable to get the desired output
Posted
Updated 23-Feb-22 20:02pm
Comments
k5054 23-Feb-22 18:11pm    
Two things:
1) You probably want to use float or double for your variables rather than int. Gravity near the earth is about 9.8 m/s^2, so a should be a floating point, at least.
2) The formula given gives you distance over time, To find the time the ball takes to drop from one floor to the next, you need to solve for t, which involves some algebra - I wish you luck.
Patrice T 23-Feb-22 20:04pm    
What is your input ?
What is actual result ?
What is expected result ?

1 solution

Look at your code: What happens to s each time round the loop?

What happens to the previous versions? Shouldn't they be included in the final total?

Change this line:
s=(u*i)+(0.5*a*i*i);

To this:
s+=(u*i)+(0.5*a*i*i);

Then initialise s to zero before the loop, and move the printing and pausing code out of the loop!

To be honest, two minutes with the debugger would have shown you what you were doing wrong and enabled you to fix this 12 hours ago. Get used to the debugger, learn to use it on trivial code like this, and you will have a tremendous advantage when your code becomes more complex! How you use it depends on your compiler system, but a quick Google for the name of your IDE and "debugger" should give you the info you need.

Put a breakpoint on the first line in the function, and run your code through the debugger. Then look at your code, and at your data and work out what should happen manually. Then single step each line checking that what you expected to happen is exactly what did. When it isn't, that's when you have a problem, and you can back-track (or run it again and look more closely) to find out why.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900