This seems to do the trick:
#include <stdio.h>
#include <math.h>
typedef struct Point {
float x;
float y;
float z;
} Point;
double findDistanceFromOrigin(Point p) {
double x = p.x;
double y = p.y;
double z = p.z;
//square-root((x2−x1)2+(y2−y1)2+(z2−z1)2)
double distance = sqrt(pow(x, 2.0) + pow(y, 2.0) + pow(z, 2.0));
return distance;
}
int main()
{
int numberOfPoints;
printf("Enter number of points:");
scanf ("%d", &numberOfPoints);
Point points[numberOfPoints];
Point closest;
Point farthest;
for (int i = 0; i < numberOfPoints; i++) {
printf("Enter x-coordinate for point %d:", i);
scanf("%f",&points[i].x);
printf("Enter y-coordinate for point %d:", i);
scanf("%f", &points[i].y);
printf("Enter z-coordinate for point %d:", i);
scanf("%f", &points[i].z);
if (i == 0) {
closest = points[i];
farthest = points[i];
} else {
if (findDistanceFromOrigin(points[i]) < findDistanceFromOrigin(closest)) {
closest = points[i];
}
if (findDistanceFromOrigin(points[i]) > findDistanceFromOrigin(farthest)) {
farthest = points[i];
}
}
}
if (numberOfPoints % 2) {
printf("The closest point is located at: (%f, %f, %f)\n", closest.x, closest.y, closest.z);
} else {
printf("The farthest point is located at: (%f, %f, %f)\n", farthest.x, farthest.y, farthest.z);
}
return 0;
}