Question: Programming in C: I need to read in a data series in C and evaluate it. The numbers have a length of 18 decimal places

Programming in C:

I need to read in a data series in C and evaluate it. The numbers have a length of 18 decimal places (Like this number: 0.19552015426254446). However, my program outputs only 6 decimal places as the result. Can you fix my code so that long double numbers are read in and they are read out correctly. Here is my code:

// I included stdio.h to print to the console #include // I included stdlib.h to dynamically alocate memory #include // I included string.h to handle strings #include // I included math.h to use mathematical functions #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("sdata.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_temperature = 0.0f; double min_temperature = 0.0f; double total_temperature = 0.0f; double max_speed = 0.0f;

// This loop splits the input_data in the save char* variable into tokens delimited by the separator " " (basically splits the data into line) // The strtok_r function just reads characters untill it reaches and then return those to token and the rest is saved in the save variable // When save is empty strtok_r returns NULL so then the loop is done (basically we reached the last line) 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)); double temperature = 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(temperature < min_temperature) min_temperature = temperature; if(temperature > max_temperature) max_temperature = temperature; total_temperature += temperature;

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);

double median = total_temperature/100.0f;

input_data = read_file("sdata"); save = input_data; double variance = 0.0f; // This loop splits the input_data in the save char* variable into tokens delimited by the separator " " (basically splits the data into line) // The strtok_r function just reads characters untill it reaches and then return those to token and the rest is saved in the save variable // When save is empty strtok_r returns NULL so then the loop is done (basically we reached the last line) 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)); double temperature = atof(strtok_r(save1, ",", &save1));

variance += (temperature - median)*(temperature - median); } variance /= 100;

printf("The maximum distance of the spacecraft from its launch : %f ", max_distance); printf("The total distance traveled by the spacecraft : %f ", total_distance); printf("The maximum speed of the spacecraft from its launch : %f ", max_speed); printf("the maximum measured temperature : %f The minimum measured temperature %f The average measured temperature : %f ", max_temperature, min_temperature, median); printf("The variance of the measured temperatures : %f ", variance);

return 0; }

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!