Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

C program error search, read and evaluate data in C: I have solved the following task: A. csv file is given which contains control commands

C program error search, read and evaluate data in C:

I have solved the following task:

A. csv file is given which contains control commands for a spaceship and temperatures measured by the spaceship. The spaceship can only move in 2 dimensions, starts at position (0, 0) and points to the right. The .csv file contains three values separated by commas (,) for each of 100 time steps, each of which is one second long. The three values are:

  1. The acceleration of the spacecraft in m/s^2 in the direction in which it is current shows
  2. The executed rotation of the spacecraft starting from the current direction in radians
  3. The measured temperature at the end of the time step

The spacecraft performs the following steps for each time step in that order:

  1. The spaceship rotates
  2. The spacecraft accelerates in the direction of flight (positive values) or against the direction of flight (negative values).
  3. The spaceship changes its position
  4. The spacecraft measures the temperature

-Determine the maximum distance of the spacecraft from its launch position during the flight -Calculate the total distance traveled by the spacecraft

However, there is an error in my code. Because I have a small deviation in the total distance and also in the maximum distance of the spacecraft from its launch position during the flight.

Can someone look at the code, unfortunately I can't upload the csv file here but maybe someone can help me and find the error.

#include #include #include #include

char* read_file(char* filename) { FILE *fptr;

if ((fptr = fopen(filename,"r")) == NULL){ printf("Error! opening file"); exit(1); }

fseek(fptr, 0, SEEK_END); long size = ftell(fptr); fseek(fptr, 0, SEEK_SET);

char* fcontent = (char*)calloc(size, sizeof(char));

fread(fcontent, 1, size, fptr);

fclose(fptr);

return fcontent; }

int main() { char* input_data = read_file("spaceship_data-1-1.csv"); char* token; char* save = input_data;

FILE *file;

if ((file = fopen("result.csv","w")) == NULL){ printf("Error! opening file"); exit(1); }

double direction = 0.0f; double v0x = 0.0f; double v0y = 0.0f; double x0 = 0.0f; double y0 = 0.0f;

double max_distance = 0.0f; double total_distance = 0.0f;

double max_speed = 0.0f;

while ((token = strtok_r(save, " ", &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 = vx + (ax/2) + x0; double y = vy + (ay/2) + y0;

fprintf(file, "%f,%f,%f ", 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; } fclose(file);

input_data = read_file("spaceship_data-1-1.csv"); save = input_data; double variance = 0.0f;

{ char* token1; char* save1 = token; double acceleration = atof(strtok_r(save1, ",", &save1)); double rotation = atof(strtok_r(save1, ",", &save1));

}

printf("The maximum distance of the spacecraft from its launch : %.14Lf ", max_distance); printf("The total distance traveled by the spacecraft : %.14Lf ", total_distance); printf("The maximum speed of the spacecraft from its launch : %.15Lf ", max_speed);

return 0; }

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

More Books

Students also viewed these Databases questions

Question

Define organisational structure

Answered: 1 week ago

Question

Define line and staff authority

Answered: 1 week ago

Question

Define the process of communication

Answered: 1 week ago

Question

Explain the importance of effective communication

Answered: 1 week ago

Question

* What is the importance of soil testing in civil engineering?

Answered: 1 week ago