#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#define MAX_SIZE 100

#define SQR(x) ((x)*(x))

double obwod(int N, double x[], double y[]);

int main(int argc, char *argv[]) {
 
  if (argc != 2) {
    printf("USAGE : %s FILE\n", argv[0]);
    exit(EXIT_FAILURE);
  } 

//  printf("Czytam z pliku %s\n", argv[1]);
  int N; 
  double x[MAX_SIZE];
  double y[MAX_SIZE];

  FILE *fh = NULL;

  fh = fopen(argv[1], "r");
  if (NULL == fh) {
    printf("Nie moge czytac pliku %s\n", argv[1]);
    exit(EXIT_FAILURE);
  }
  fscanf(fh, "%d\n", &N);
  int i;
  for (i=0; i<N; i++) {
    int r;
    r = fscanf(fh, "%lf%lf", &(x[i]), &(y[i]));
   if (EOF == r) {
     printf("Za malo danych w pliku\n");
     exit(EXIT_FAILURE);
   } 
   if (2 != r) {
      printf("Blad przy czytaniu wierzcholka %d\n", i);
      exit(EXIT_FAILURE);
    }
  }
  fclose(fh);

  printf("Plik na %d wierzcholkow\n",N);
  printf("Obwod wielokata : %g \n", obwod(N, x, y));
  return 0;
}

double obwod(int N, double x[], double y[]) {
  double s = 0.0;
  int i;
  for (i = 0; i<N; i++) {
    s = s + sqrt(SQR(x[i]-x[(i+1)%N]) + SQR(y[i]-y[(i+1)%N]));
  }
  return s;
}



