Получение ошибки сегментации. Я так много пробовал, но не мог найти ошибки. Пожалуйста, помогите мне.
Пользователь введет количество точек и координаты. Ответ должен быть минимальным расстоянием, которое будет расстоянием между ближайшей парой точек.Я также использовал функцию часов, чтобы получить общее затраченное время.. есть ли какие-то проблемы в его синтаксисе?
Что я уже пробовал:
#include<stdio.h> #include<stdlib.h> #include<time.h> #include<math.h> #include<float.h> // A structure to represent a Point in 2D plane struct Point { int x, y; }; typedef struct Point Point; // Needed to sort array of points according to X coordinate int compareX(const void* a, const void* b) { Point *p1 = (Point *)a, *p2 = (Point *)b; return (p1->x - p2->x); } // Needed to sort array of points according to Y coordinate int compareY(const void* a, const void* b) { Point *p1 = (Point *)a, *p2 = (Point *)b; return (p1->y - p2->y); } float dist(Point p1, Point p2) { return sqrt( (p1.x - p2.x)*(p1.x - p2.x) + (p1.y - p2.y)*(p1.y - p2.y) ); } float minm(float x, float y) { return (x < y)? x : y; } float stripClosest(Point strip[], int n, float d) { float minm = d; // Initialize the minimum distance as d int i,j; for ( i = 0; i < n; ++i) for (j = i+1; j < n && (strip[j].y - strip[i].y) < minm; ++j) if (dist(strip[i],strip[j]) < minm) minm = dist(strip[i], strip[j]); return minm; } float closestUtil(Point Px[], Point Py[], int n) { // Find the middle point int mid = n/2; Point midPoint = Px[mid]; Point Pyl[mid+1]; // y sorted points on left of vertical line Point Pyr[n-mid-1]; // y sorted points on right of vertical line int li = 0, ri = 0,i; // indexes of left and right subarrays for (i = 0; i < n; i++) { if (Py[i].x <= midPoint.x) Pyl[li++] = Py[i]; else Pyr[ri++] = Py[i]; } float dl = closestUtil(Px, Pyl, mid); float dr = closestUtil(Px + mid, Pyr, n-mid); float d = minm(dl, dr); Point strip[n]; int j = 0; for ( i = 0; i < n; i++) if (abs(Py[i].x - midPoint.x) < d) strip[j] = Py[i], j++; return minm(d, stripClosest(strip, j, d) ); } float closest(Point P[], int n) { Point Px[n]; Point Py[n]; int i; for ( i = 0; i < n; i++) { Px[i] = P[i]; Py[i] = P[i]; } qsort(Px, n, sizeof(Point), compareX); qsort(Py, n, sizeof(Point), compareY); return closestUtil(Px, Py, n); } int main() { clock_t start,last; double total; FILE *fp; int n,i; printf("Enter number of co ordinates:\n"); scanf("%d",&n); Point P[n]; printf("Enter the co ordinates:\n"); for(i=0;i<n;i++) scanf("%d %d",&P[i].x,&P[i].y); start=clock(); printf("Minimum distance is %f", closest(P, n)); last=clock(); total=(double)(last-start); printf("%f",total); return 0; }