Advertisement
vencinachev

Points-Distance

Mar 16th, 2022
766
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.01 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <math.h>
  4. #include <time.h>
  5.  
  6. #define COUNT 100
  7.  
  8. typedef struct  {
  9.     double x;
  10.     double y;
  11. } point;
  12.  
  13. double distanceDecart(point,point);
  14.  
  15. double maxDist(point pts[], int size){
  16.     double max = 0;
  17.     for (int i = 0; i < size; i++){
  18.         for (int j = 0; j < size; j++){
  19.             double dist = distanceDecart(pts[i], pts[j]);
  20.             if (dist > max){
  21.                 max = dist;
  22.             }
  23.         }
  24.     }
  25.     return max;
  26. }
  27.  
  28. int main(){
  29.     srand(time(NULL));
  30.     point points[COUNT];
  31.     for (int i = 0; i < COUNT; i++){
  32.         points[i].x = rand() % 11;  // [0-10]
  33.         points[i].y = rand() % 11;
  34.     }
  35.  
  36.     for (int i = 0; i < COUNT; i++){
  37.         printf("Point[%d] (%.1lf, %.1lf)\n", i, points[i].x, points[i].y);
  38.     }
  39.  
  40.     double maxD = maxDist(points, COUNT);
  41.     printf("Max distance: %.2lf\n", maxD);
  42.  
  43. }
  44.  
  45. double distanceDecart(point p1, point p2){
  46.     return sqrt((p1.x-p2.x)*(p1.x-p2.x) + (p1.y-p2.y)*(p1.y-p2.y));
  47. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement