Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
const double PI = 3.14159;
// Global Declarations
typedef struct Coordinates
{
char* city;
char* code;
double latitude;
double longitude;
}Coordinates;
// Function Declarations
int ReadFile (char* filename, char*** strings);
void GetCoordinates (struct Coordinates** coordinateslist, char** data, int count);
double Haversine (double dlatitude1, double dlongitude1, double dlatitude2, double dlongitude2, double radius);
void main()
{
// Local Declarations
char** matrix = NULL;
struct Coordinates* coordinates = NULL;
int count = ReadFile("C:\\Coordinates.csv", &matrix);
int index = 0;
double radius = 3959;
double dlatitude1;
double dlongitude1;
double dlatitude2;
double dlongitude2;
double res;
// Statements
if (count > 0)
{
GetCoordinates (&coordinates,matrix,count);
for (index = 0; index < count; index++)
printf("City: %s\nCode: %s\nLatitude: %.2f\nLongitude: %.2f\n\n", (coordinates+index)->city, (coordinates+index)->code,
(coordinates+index)->latitude, (coordinates+index)->longitude);
}
return;
}
int ReadFile (char* filename, char*** strings)
{
FILE* fp = fopen(filename,"r");
char temp[128];
int count = 0;
while (!feof(fp))
{
if (fgets(temp,128,fp) != NULL)
count++;
}
fseek(fp,0,SEEK_SET);
*strings = (char**)malloc(sizeof(char*)*count);
count = 0;
while (!feof(fp))
{
if (fgets(temp,128,fp) != NULL)
{
*((*strings)+count) = (char*)malloc(strlen(temp)+1);
strcpy(*((*strings)+count),temp);
count++;
}
}
fclose(fp);
return count;
}
void GetCoordinates(struct Coordinates** coordinateslist, char** data, int count)
{
// Local Declarations
char temp[32];
char* ptr = NULL;
int index = 0;
// Statements
*coordinateslist = (struct Coordinates*)malloc(sizeof(struct Coordinates)*count);
for (index=0; index<count; index++)
{
strcpy(temp,data[index]);
ptr = strtok(temp,",");
((*coordinateslist)+index)->city = (char*)malloc(strlen(ptr)+1);
strcpy(((*coordinateslist)+index)->city,ptr);
ptr = strtok(NULL,",");
*(ptr+strlen(ptr)) = NULL;
((*coordinateslist)+index)->code = (char*)malloc(strlen(ptr));
strcpy(((*coordinateslist)+index)->code,ptr);
ptr = strtok(NULL,",");
*(ptr+strlen(ptr)) = NULL;
((*coordinateslist)+index)->latitude = atof(ptr);
ptr = strtok(NULL,",");
*(ptr+strlen(ptr)) = NULL;
((*coordinateslist)+index)->longitude = atof(ptr);
}
}
double Radians(double value)
{
double radians = value * PI / 180.0;
return radians;
}
double Haversine (double dlatitude1, double dlongitude1, double dlatitude2, double dlongitude2, double radius)
{
double dLatitude = Radians(dlatitude2-dlatitude1);
double dLongitude = Radians(dlongitude2-dlongitude1);
double nA = pow ( sin(dLatitude/2.0), 2.0 ) + cos(Radians(dlatitude1)) * cos(Radians(dlatitude2)) * pow ( sin(dLongitude/2.0), 2.0 );
double nC = 2.0 * atan2( sqrt(nA), sqrt( 1.0 - nA ));
double distance = radius * nC;
return distance;
}
I'm really not too sure where to start on calculating the distance but my assumption is that I need to point values from latitude and longitude to dlatitude1-2 and dlongitude1-2.