fork download
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <math.h>
  4.  
  5. #define MAX_PTS 100
  6. #define MAX_POLYS 100
  7. #define END_INPUT 0
  8.  
  9. struct Point {
  10. double x, y;
  11. };
  12.  
  13. double getDistance(struct Point a, struct Point b) {
  14. double distance;
  15. distance = sqrt((a.x - b.x) * (a.x - b.x) + (a.y-b.y) *(a.y-b.y));
  16. return distance;
  17. }
  18.  
  19. int main(int argc, char *argv[]) {
  20. int npoints, poly_id;
  21. struct Point a, b;
  22.  
  23. if(scanf("%d %d", &npoints, &poly_id)) {
  24. int iteration = 0;
  25. scanf("%lf %lf", &a.x, &a.y);
  26. struct Point initialPoint = a;
  27. double parameter = 0;
  28. for (iteration = 1; iteration < npoints; ++iteration) {
  29. scanf("%lf %lf", &b.x, &b.y);
  30. parameter += getDistance(a, b);
  31. a = b; // for next iteration
  32. }
  33. parameter += getDistance(a, initialPoint);
  34.  
  35. printf("Polygon is %d\n", poly_id);
  36. printf("perimeter = %2.2lf m\n", parameter);
  37. } else { printf("\nUnable to read input.\n");
  38. exit(EXIT_FAILURE);
  39. }
  40.  
  41. return 0;
  42. }
Success #stdin #stdout 0s 2056KB
stdin
3 12867 1.0 2.0 1.0 5.0 4.0 5.0
stdout
Polygon is 12867
perimeter = 10.24 m