Question: Programming in C: Hi, I have a .csv file with Acceleration and rotation temperature data and I am reading it into C. This also works,
Programming in C:
Hi, I have a .csv file with Acceleration and rotation temperature data and I am reading it into C. This also works, but when I want to find out my min and max temperature with a loop it doesn't quite work. With my loop I get the max temperature but for the minimum temperature I always get 0, although the smallest temperature in the .csl file is 3,4. Can someone take a look and maybe fix it? [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("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_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("spaceship_data-1-1.csv"); 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 : %.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); printf("the maximum measured temperature : %f The minimum measured temperature %f The average measured temperature : %.14Lf ", max_temperature, min_temperature, median); printf("The variance of the measured temperatures : %f ", variance); return 0; } [/CODE]