```
#include <stdio.h>
#include <math.h>
#define PI 3.141593
// prototype of function to find the great circle distance between two points
double gc_dist(double, double, double, double);
int main(void)
{
double lat1, long1, lat2, long2;
printf("Enter latitude north and longitude west ");
printf("for location 1 : ");
scanf("%lf %lf", &lat1, &long1);
printf("Enter latitude north and longitude west ");
printf("for location 2 : ");
scanf("%lf %lf", &lat2, &long2);
printf("Great Circle Distance: %.0f km\n", gc_dist(lat1, long1, lat2, long2));
}
double gc_dist(double lat1, double long1, double lat2, double long2)
{
double rho = 6371, phi, theta, gamma, dot, dist1, dist2, x1,y1,z1,x2,y2,z2;
phi = (90 - lat1) * (PI/180.0); // convert to spherical coordinates
theta = (360 - long1) * (PI/180.0);
x1 = rho * sin(phi) * cos(theta); // find rectangular coordinates for p1
y1 = rho * sin(phi) * sin(theta);
z1 = rho * cos(phi);
phi = (90 - lat2) * (PI/180.0); // repeat for p2
theta = (360 - long2) * (PI/180.0);
x2 = rho * sin(phi) * cos(theta);
y2 = rho * sin(phi) * sin(theta);
z2 = rho * cos(phi);
dot = x1 * x2 + y1 * y2 + z1 * z2; // find the dot product of the two vectors
dist1 = sqrt(x1 * x1 + y1 * y1 + z1 * z1); // find length of each vector
dist2 = sqrt(x2 * x2 + y2 * y2 + z2 * z2);
gamma = acos(dot / (dist1 * dist2));
return gamma * rho;
}
```

This is what the above code does:

- Get coordinates of two points.

a) Print “Enter latitude + longitude of first point”

b) Read in coordinates; and repeat for second point. - Find their distance.

a) Find spherical coordinates and convert to

rectangular coordinates for both points.

b) Find the dot-product and length of both vectors.

c) central_angle =`acos(dot-product/(length of p1*length of p2))`

d) Answer is central_angle multiplied by radius of the earth. - Print the great circle distance

In the case whereby the 1st point's coordinates are 36.12 86.67 and teh 2nd point's coordinates are 33.94 118.40,this is what shows up on teh console box:

Enter latitude north and longitude west for location 1 : 36.12 86.67

Enter latitude north and longitude west for location 2 : 33.94 118.40

Great Circle Distance: 2886 km

However, when i modified the program using a 'more modular' version, I managed to get the same answer for a certain point as using the above code.But i was unable to get 2886 when i used the same coordiantes (36.12 86.67) and (33.94 118.40).can soemone help me and tell me why is this so?here is the modified program>>>

```
#include <stdio.h>
#include <math.h>
#define PI 3.141593
double gc_dist(double, double, double, double);
int main(void)
{
double lat1, long1, lat2, long2;
printf("Enter latitude north and longitude west ");
printf("for location 1 : ");
scanf("%lf %lf", &lat1, &long1);
printf("Enter latitude north and longitude west ");
printf("for location 2 : ");
scanf("%lf %lf", &lat2, &long2);
printf("Great Circle Distance: %.0f km\n", gc_dist(lat1, long1, lat2, long2));
}
#define deg_to_rad(deg) ((deg * (PI/180.0))
double angle(double x1, double y1, double z1, double x2, double y2, double z2);
{
double dot, dist1, dist2;
dot = x1 * x2 + y1 * y2 + z1 * z2; // the dot product of the two vectors
dist1 = sqrt(x1 * x1 + y1 * y1 + z1 * z1); // length of each vector
dist2 = sqrt(x2 * x2 + y2 * y2 + z2 * z2);
return acos(dot / (dist1 * dist2));
}
double gc_dist(double lat1, double long1, double lat2, double long2)
{
double rho = 6371, phi, theta, gamma, dot, dist1, dist2, x1,y1,z1,x2,y2,z2;
phi = deg_to_rad(90 - lat1)); // convert to spherical coordinates
theta = deg_to_rad(360 - long1));
x1 = rho * sin(phi) * cos(theta); // find rectangular coordinates for p1
y1 = rho * sin(phi) * sin(theta);
z1 = rho * cos(phi);
phi = deg_to_rad(90 - lat2)); // find rectangular coordinates for p2
theta = deg_to_rad(360 - long2));
x2 = rho * sin(phi) * cos(theta);
y2 = rho * sin(phi) * sin(theta);
z2 = rho * cos(phi);
return angle(x1, y1, z1, x2, y2, z2) * rho;
}
```

whats my mistake and how shoud i edit it??

*Edited 3 Years Ago by happygeek*: fixed formatting