# Thread: Help! Why does it keep showing Distance = 0.00?

1. ## Help! Why does it keep showing Distance = 0.00?

I've been trying to figure this out, but I can't get this to work! Can someone explain to me what I'm doing wrong?

This is what I'm supposed to do:
Write a function that returns the distance a vehicle can travel in a given time, where the vehicle has an initial velocity of zero and a constant acceleration up to a specified maximum velocity. The function should return a float and should have parameters for time (in seconds), the vehicle's acceleration (in m/s2), and its maximum velocity (in m/s).
You should calculate the distance traveled as follows:

· calculate time to reach maximum velocity -- t_max = max_v/ a
· compare t to t_max, if t is greater
o distance = max_v/ 2 * t_max + (t - t_max) * max_v
· otherwise
o distance = (t * a) / 2 * t

max_v = maximum velocity, a = acceleration, d = distance traveled, t = travel time and t_max= time to reach maximum velocity
And so far, I have this:

Code:
```float disTraveled (float t, float maxVelocity, float acceleration);

void race();

int main()
{
printf("\n\nQUESTION - Race\n");
race();

return 0;
}

void race()
{
float T_Max1, acceleration1, t, maxVelocity, acceleration;

printf("Enter the maximum velocity of vehicle 1: ");
scanf("%f", &T_Max1);

printf("Enter the acceleration of vehicle 1: ");
scanf("%f", &acceleration1);

printf("Enter the race time: ");
scanf("%f", &t);

printf("\n%s%11s%10s%11s\n", "Vehicle", "Max.", "Acc.", "Distance");
printf("%s%11s%10s%11s\n\n", "-------", "----", "----", "--------");

printf("%s%9.2f%10.2f%11.2f\n", "Vehicle 1", T_Max1, acceleration1, disTraveled(t, maxVelocity, acceleration));

float disTraveled (float t, float maxVelocity, float acceleration)
{
float distance, T_Max;

T_Max = (maxVelocity / acceleration); // Time to reach maximum velocity

if (t >= T_Max) {
distance = (((maxVelocity / 2.0) * T_Max) + ((t - T_Max) * maxVelocity));
}
else {
distance = (((t * acceleration) / 2.0) * t);
}

return distance;
}```

2. Originally Posted by toobee
I've been trying to figure this out, but I can't get this to work! Can someone explain to me what I'm doing wrong?
Hi, what you pasted didn't compile good until I added a closing brace for the end of the race function. After that, I got these warnings...

Code:
```mingw32-gcc.exe -Wall  -g     -c "C:\Documents and Settings\User\My Documents\sandbox\sandbox.c" -o obj\Debug\sandbox.o
C:\Documents and Settings\User\My Documents\sandbox\sandbox.c: In function 'race':
C:\Documents and Settings\User\My Documents\sandbox\sandbox.c:45:84: warning: 'maxVelocity' is used uninitialized in this function
C:\Documents and Settings\User\My Documents\sandbox\sandbox.c:45:84: warning: 'acceleration' is used uninitialized in this function
mingw32-g++.exe  -o bin\Debug\sandbox.exe obj\Debug\sandbox.o
Output size is 30.55 KB
Process terminated with status 0 (0 minutes, 1 seconds)
0 errors, 2 warnings```
Just like it says, you have uninitialized variables being used in your code, which makes the behavior of any operations on those variables undefined. The problem with undefined behavior is that if even a small part of the program is undefined, then the whole thing is, too. Even if everything else is completely correct, printf could print 0.00. So you need to address the warnings emitted by the code.

You should turn warnings on to the highest (strictest) level they can be. This is different for every compiler so if you need help, please provide more information.

3. Code:
```\$ gcc -W -Wall -Wextra bar.c
bar.c: In function ‘race’:
bar.c:44: warning: ‘maxVelocity’ is used uninitialized in this function
bar.c:44: warning: ‘acceleration’ is used uninitialized in this function```
Perhaps because you read into one set of variables, but pass the other set to the function.

Edit: beaten to it

4. Thanks for your guys' response. What I want for it to do is to use the 'formula' from "float disTraveled" so that it can be computed within:

 printf("%s%9.2f%10.2f%11.2f\n", "Vehicle 1", T_Max1, acceleration1, disTraveled(t, maxVelocity, acceleration));
that way it will show me the calculations of its distance.. how would I be able to use and 'initialize' maxVelocity and acceleration? -- or at least change it so that it doesn't print 0.00?

5. That is something you should know. Set maxVelocity to the value of the maximum velocity, and acceleration to the appropriate value, before you use either in any math.