473,471 Members | 1,750 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

C loop error search

1 New Member
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?

Here is the code:

Expand|Select|Wrap|Line Numbers
  1.     while ((token = strtok_r(save, "\n", &save)))
  2.     {
  3.         char* token1;
  4.         char* save1 = token;
  5.         double acceleration = atof(strtok_r(save1, ",", &save1));
  6.         double rotation = atof(strtok_r(save1, ",", &save1));
  7.  
  8.  
  9.         direction += rotation;
  10.         double ax = acceleration * cos(direction);
  11.         double ay = acceleration * sin(direction);
  12.  
  13.         double vx = v0x + ax;
  14.         double vy = v0y + ay;
  15.  
  16.         double x = v0x + (ax/2) + x0;
  17.         double y = v0y + (ay/2) + y0;
  18.  
  19.  
  20.         fprintf(file, "%f,%f\n", x, y, direction);
  21.  
  22.         double distance = sqrt(x*x + y*y);
  23.         if(distance > max_distance) max_distance = distance;
  24.         total_distance += sqrt((x-x0)*(x-x0) + (y-y0)*(y-y0));
  25.  
  26.  
  27.         if(sqrt(vx*vx + vy*vy) > max_speed) max_speed = sqrt(vx*vx + vy*vy);
  28.  
  29.         v0x = vx;
  30.         v0y = vy;
  31.         x0 = x;
  32.         y0 = y;
  33.     }
Feb 23 '21 #1
0 1844

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: undercups | last post by:
When I run page I immediatley get "Object expected" message and the tag <body onload="setfocus()" is highlighted. The error occurs in this snippet from within the script file function...
2
by: JMCN | last post by:
help! i'm caught in the endless of "compile error message: do without loop." i thought that i closed all of my statments but it appears not. does anyone know why my structure is incorrect? what...
1
by: SJ | last post by:
I'm developing a WAP client which presently works fine on most mobile phone browsers, but gives me an Infinite loop error (error 1025) when i try to access it from a couple of phones(motorola for...
3
by: tehgreatmg | last post by:
Here is my code: Dim db As DAO.Database Dim rs1 As DAO.Recordset Set db = CurrentDb() Set rs1 = db.OpenRecordset("CartUse") rs1.MoveFirst Do Until rs1.EOF
11
by: orajit | last post by:
---When I run following code DECLARE v_evct NUMBER(20); v_evcd NUMBER(20); v_class VARCHAR2(100); cursor c1 is select * from input_info; BEGIN
5
by: Poly | last post by:
Here is a block of code that I am having trouble with. I rewrote it as a main function, so I could isolate the problem to fix it, but still haven't been able to figure out the problem. I am pretty...
2
by: Darkside12 | last post by:
Hi, I'm trying to create a do loop that renames files in a folder. My current code looks like this: Private Sub Rename_Files_Click() Dim VPNPath As String VPNPath = Dir("C:\Temp\*.txt")
0
by: if1467 | last post by:
Hi, I am working with a loop that has the .Find method in it. I need to figure out how to have the program continue when the find doesn't find anything. The VB Help for Find Method shows the...
2
by: rando1000 | last post by:
I keep getting a compile error, "Loop without Do". I've tried Do While and Do Until. What am I doing wrong here? I've got other pieces of code like this in my project, and they work fine. ...
0
by: Mertah | last post by:
Hello All, My question is to know FIRST how to retrieve (to get) a value of <INPUT type="text"...> using XSL into my xsl variable, this value contains text strings separated by semi-column ";" like...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.