I have a coding assignment which I need to take points (x,y,z) from a text document, and print them out in the console, then compare two coordinates at a time and find the distance between the two, then move on to the next two points. All of the points need to be compared to the points before them and the points after them, so each point with the exception of the first and last need to be compared twice. I have tried many different thing and tried looking for similar questions online, but have found no help. I know how to get the points out of the text document and into the console but cant figure out how to compare the points. The points I have in the text doc are (1,2,3) the next lines in the doc is (0,0,0) and the third line is (1,2,3) so by the end I should have the points 5.6568 for both of them, the points I have are just for testing and the code needs to work with 300+ total different points. Thank you for any help in advance.
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <math.h>
#define MAX_STRING_LENGTH 100
typedef struct Points Points;
struct Points
{
float x;
float y;
float z;
};
void getNumber(char string[], Points* point)
{
int commaIndex = -1;
char* result = NULL;
result = strchr(string, ',');
char* stringStart = &string[0];
commaIndex = result - stringStart;
char* xString = malloc((commaIndex + 1) * sizeof(char));
strncpy(xString, string, commaIndex);
xString[commaIndex] = '\0';
point->x = atof(xString);
string = &string[0] + commaIndex + 1;
result = strchr(string, ',');
stringStart = &string[0];
commaIndex = result - stringStart;
char* yString = malloc((commaIndex + 1) * sizeof(char));
strncpy(yString, string, commaIndex);
yString[commaIndex] - '\0';
point->y = atof(yString);
string = &string[0] + commaIndex + 1;
point->z = atof(string);
}
int main(int argc, char** argv)
{
printf("This program will calculate distance between points and how long it will take.\n");
int numLines = 0, sum = 0, ysum = 0, zsum = 0;
float currentLine[MAX_STRING_LENGTH];
FILE *data = fopen("points.txt", "r");
if (data == NULL)
{
printf("Error");
return (EXIT_FAILURE);
}
while (!feof(data))
{
fgets(currentLine, MAX_STRING_LENGTH, data);
numLines++;
}
rewind(data);
Points* point = malloc(numLines * sizeof(Points));
for (int i = 0; i < numLines; i++)
{
fgets(currentLine, MAX_STRING_LENGTH, data);
getNumber(currentLine, &point[i]);
}
fclose(data);
for (int i = 0; i < numLines; i++)
{
printf("X:%f ", point[i].x);
printf("Y:%f ", point[i].y);
printf("Z:%f ", point[i].z);
printf("\n");
}
//float timeSum = sum + ysum + zsum;
//printf("%f", timeSum);
//distance = sqrt((x2 - x1)^2 + (y2 - y1)^2 + (z2 - z1)^2);
return 0;
}