Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <float.h>
- #include <stdio.h>
- #include <stdlib.h>
- #include <tgmath.h>
- static void data_read(int *n, double complex **p) {
- FILE *in = fopen("data.txt", "r");
- if (fscanf(in, "%d", n) != 1) {
- exit(1);
- }
- *p = calloc(sizeof(**p), *n);
- for (int i = 0; i < *n; ++i) {
- double x, y;
- if (fscanf(in, "%lf%lf", &x, &y) != 2) {
- exit(1);
- }
- (*p)[i] = CMPLX(x, y);
- }
- fclose(in);
- }
- static void find_closest_points(int n, double complex *p, int *index,
- double *min) {
- *min = DBL_MAX;
- for (int i = 0; i < n; ++i) {
- for (int j = i + 1; j < n; ++j) {
- double dist = fabs(p[i] - p[j]);
- if (dist < *min) {
- *min = dist;
- index[0] = i;
- index[1] = j;
- }
- }
- }
- }
- static void report(int *index, double min) {
- printf("%d %d %f\n", index[0], index[1], min);
- }
- int main(void) {
- int n;
- double complex *p;
- data_read(&n, &p);
- {
- int index[2];
- double min;
- find_closest_points(n, p, index, &min);
- report(index, min);
- }
- free(p);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement