1

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;
}
branj26
  • 11
  • 1
  • What exactly is the problem? Just iterate the array and for each `index` "compare" (whatever that means since you didn't explain it clearly) the point at `index-1` with `index` and `index+1` with `index`. – kaylum Nov 21 '20 at 06:55
  • `while (!feof(data))` gives you wrong results. See this https://stackoverflow.com/questions/5431941/why-is-while-feof-file-always-wrong – Krishna Kanth Yenumula Nov 21 '20 at 08:10
  • Ack, no indentation. Hurts my eyes. – William Pursell Nov 21 '20 at 11:34
  • Instead of finding the comma with strchr, use `strtol` and examine the 2nd argument. – William Pursell Nov 21 '20 at 11:36
  • There's no need to rewind, just read the data once. Pseudo code is: `while(data available) { if prev (compare current and previous); prev = current}` That catches the beginning and end edge cases as you want. – William Pursell Nov 21 '20 at 11:39

1 Answers1

0

It is quite easy to compare two elements of a list. Here is how you would make a function that would calculate the distance between the points.

float TravDistance(Points* points, int n){
    float distance=0;
    for(int i=0; i<n-1; i++){
        distance+=sqrt( (double)pow((double)(points[i+1].x-points[i].x),(double)2)+
                (double)pow((double)(points[i+1].y-points[i].y),(double)2)+
                (double)pow((double)(points[i+1].z-points[i].z),(double)2));
    }
}