Thread: C loop error search

  1. #1
    Registered User
    Join Date
    Feb 2021

    C loop error search

    Hello, I have data in a .csv describing a car journey. The car starts at the position (0,0)-> (x,y). It moves only in 2 dimensions.
    The .csv file contains two values separated by commas (,). The values include:
    -The acceleration of the car in the direction it is currently pointing.
    -The rotation of the car in which it is currently pointing.

    The car always rotates at the beginning, then it accelerates for 1 second, stops rotates again and accelerates again. and so on...

    Now I have to calculate the maximum distance from the start position and the total distance covered. However, I get in C when I run the script slightly different values than should finally come out.

    Can someone take a look at my loop or algorithm? Do I have a mistake somewhere how I calculate the covered distance?

            while ((token = strtok_r(save, "\n", &save)))
                char* token1;
                char* save1 = token;
                double acceleration = atof(strtok_r(save1, ",", &save1));
                double rotation = atof(strtok_r(save1, ",", &save1));
                direction += rotation;
                double ax = acceleration * cos(direction);
                double ay = acceleration * sin(direction);
                double vx = v0x + ax;
                double vy = v0y + ay;
                double x = v0x + (ax/2) + x0;
                double y = v0y + (ay/2) + y0;
                fprintf(file, "%f,%f\n", x, y, direction);
                double distance = sqrt(x*x + y*y);
                if(distance > max_distance) max_distance = distance;
                total_distance += sqrt((x-x0)*(x-x0) + (y-y0)*(y-y0));
                if(sqrt(vx*vx + vy*vy) > max_speed) max_speed = sqrt(vx*vx + vy*vy);
                v0x = vx;
                v0y = vy;
                x0 = x;
                y0 = y;

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    The edge of the known universe
    > direction += rotation;
    > double ax = acceleration * cos(direction);
    > double ay = acceleration * sin(direction);
    The first question to ask would be what units your angles are measured in.

    Because the usual expectation for something human readable would be degrees.

    But the sin/cos function expect the more mathematically natural units of radians.
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Recursive search error
    By surgeon in forum C++ Programming
    Replies: 1
    Last Post: 09-05-2015, 01:38 PM
  2. Binary Search Tree Assignemnt to struct error
    By Purdue in forum C Programming
    Replies: 0
    Last Post: 12-02-2014, 03:34 PM
  3. Binary Search Tree Adding Error
    By shukiren in forum C Programming
    Replies: 1
    Last Post: 11-12-2012, 08:37 PM
  4. Sequential search- error?
    By denisa in forum C Programming
    Replies: 3
    Last Post: 12-05-2011, 11:46 PM
  5. add,view=ok, search,edit got error... help!
    By private0430 in forum C Programming
    Replies: 4
    Last Post: 09-27-2009, 01:36 PM

Tags for this Thread