Code using C Use a series of latitude and longitude data entered from the comman
ID: 3906937 • Letter: C
Question
Code using C
Use a series of latitude and longitude data entered from the command line to
calculate the TOTAL distance between 2 points. For example, assume the lat/long data is
(32.639987, -116.995167) and
(32.640870, -116.997001).
If the input from the command line is
as follows:
E.g. C:getDist 32.639987 -116.995167 32.640870 -116.997001
The output should be:
The distance between points (32.639987, -116.995167) and (32.640870, -116.997001)
is 216.57 yards.
Do not prompt the user for information. Take your input data from the command line. Since the data is entered in lat/long pairs, your program will need at least 4 numbers to calculate distance, where location 1 is the first pair, and location 2 is the second pair.
Latitude and longitude data are typically in degrees so you will need to convert each to radians. Since ? (pi) radians is equivalent to 180 degrees, to convert from degrees to radians, you can multiply the angle in degrees by ?/180. For example, 90 degrees when converted to radians is 90* ? /180 = ? /2= 1.57096.
Use of functions:
Your program should make liberal use of functions. At a minimum, I anticipate your program having the following functions:
double toRadians( double deg);
double distanceBetween(double lat1, double long1, double lat2, double long2);
Explanation / Answer
If you have any doubts, please give me comment...
#include<stdio.h>
#include<math.h>
#include<stdlib.h>
double toRadians( double deg);
double distanceBetween(double lat1, double long1, double lat2, double long2);
int main(int argc, char *argv[]){
if(argc!=5){
printf("Usage: ./getDist 32.639987 -116.995167 32.640870 -116.997001 ");
return 0;
}
double lat1 = atof(argv[1]);
double long1 = atof(argv[2]);
double lat2 = atof(argv[3]);
double long2 = atof(argv[4]);
double lat1_rad = toRadians(lat1);
double long1_rad = toRadians(long1);
double lat2_rad = toRadians(lat2);
double long2_rad = toRadians(long2);
double distance = distanceBetween(lat1_rad, long1_rad, lat2_rad, long2_rad);
printf("The distance between points(%lf, %lf) and (%lf, %lf) is %.2lf yards ", lat1, long1, lat2, long2, distance);
return 0;
}
double toRadians( double deg){
return (deg*3.1459)/180;
}
double distanceBetween(double lat1, double long1, double lat2, double long2){
return sqrt(((lat2-lat1)*(lat2-lat1)) + ((long2-long1)*(long2-long1)));
}