Here is a solution that also calculates the perimeter.
It implements the pseudo-code I gave you above.
I noticed that your data points are polar coordinates and the angles are given in DEGREES not RADIANS.
This means your formula to calculate the distance using the cos() function is incorrect because the cos() function needs angles in RADIANS... so you need to convert the (q1-q) to radians before you use the cos().
This is done by: (q1-q)*dtor
So now the formula becomes
d = sqrt((r*r) + (r1*r1) - (2 * r*r1) *(cos((q1 - q)*dtor)));
The variable dtor is a defined at the top of the program as pi/180.
Here is the program
#include "stdafx.h"
#include <stdio.h>
#include <math.h>
FILE *pg;
double input_status, d,r0,q0,r,q,r1,q1,total_perimeter=0;
int t=0;
double dtor = 3.14159265358979 / 180.0; int main()
{
errno_t err;
err = fopen_s(&pg, "input.txt", "r");
if (err == 0)
printf("The file 'input.txt' was opened\n\n");
else
{
printf("The file 'input.txt' was not opened\n");
return 0; }
input_status = fscanf_s(pg, "%lf %lf ", &r, &q);
r0 = r; q0 = q; if(input_status==EOF)
{
printf("There are no data points in the file");
return 0; }
printf("point %d: r=%lf , q=%lf\n", t, r, q);
do
{
input_status = fscanf_s(pg, "%lf %lf ", &r1, &q1);
if(input_status==EOF)
break; t++;
printf("point %d: r=%lf , q=%lf\n", t, r1, q1);
d = sqrt((r*r) + (r1*r1) - (2 * r*r1) *(cos((q1 - q)*dtor)));
printf("distance between %d and %d = %lf \n\n",(t-1),t,d);
r = r1; q = q1; total_perimeter += d; }while(1);
r1 = r0; q1 = q0; d = sqrt((r*r) + (r1*r1) - (2 * r*r1) *(cos((q1 - q)*dtor)));
printf("distance between %d and 0 = %lf \n\n\n",t,d);
total_perimeter += d; printf("Total Perimiter = %lf\n",total_perimeter);
return 1;
}