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

  1. #1
    Registered User
    Join Date
    Feb 2012
    Posts
    4

    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. #2
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    Quote Originally Posted by toobee View Post
    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. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    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
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  4. #4
    Registered User
    Join Date
    Feb 2012
    Posts
    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. #5
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. distance formula help
    By Arex Bawrin in forum C Programming
    Replies: 15
    Last Post: 02-25-2011, 03:38 PM
  2. Hamming Distance?
    By rs07 in forum C Programming
    Replies: 3
    Last Post: 09-12-2008, 04:02 PM
  3. getting distance from angles
    By HermitCrab in forum C++ Programming
    Replies: 5
    Last Post: 06-08-2007, 07:26 PM
  4. Distance to City
    By DavidP in forum A Brief History of Cprogramming.com
    Replies: 8
    Last Post: 08-17-2003, 10:58 PM
  5. Hex distance
    By waterst in forum C Programming
    Replies: 7
    Last Post: 11-02-2002, 01:59 PM