Question: Implement c++ program distance.cpp The distance program prints the distance between two airports. The airport codes are read from the command line arguments. The distance
Implement c++ program distance.cpp
The distance program prints the distance between two airports. The airport codes are read from the command line arguments. The distance is printed in nautical miles, rounded to an integer. See the example output files for the output format.
Notes:
The airport codes are entered as command line arguments and may consist of 3 or 4
characters. The program should be able to process both cases correctly.
If any of the airports entered is not found (e.g. ABCD), the program should print ABCD not found and exit.
If any of the airport codes given is longer than 4 characters, the program should print Airport code must be at most 4 characters and exit.
Example of input--
#!/bin/bash
./distance 24MD 21NJ
Example of output--
24MD - 21NJ 153 NM
just in case its helpful the folowing files are provided.
//
// gcdistance.h
//
#ifndef GCDISTANCE_H
#define GCDISTANCE_H
double gcdistance(double lat1, double lon1, double lat2, double lon2);
#endif
//
// gcdistance.cpp
//
// great circle distance between points (lat1,lon1) and (lat2,lon2)
// input in degrees decimal
// output in nautical miles (NM)
#include "gcdistance.h"
#include
double gcdistance(double lat1, double lon1, double lat2, double lon2)
{
// convert latitudes and longitudes from degrees to radians
const double lat1r = lat1 * (M_PI/180.0);
const double lon1r = lon1 * (M_PI/180.0);
const double lat2r = lat2 * (M_PI/180.0);
const double lon2r = lon2 * (M_PI/180.0);
// psi = central angle between the points (lat1,lon1) and
// (lat2,lon2) on a sphere
double c = cos(lat1r)*cos(lat2r)*cos(lon1r-lon2r) + sin(lat1r)*sin(lat2r);
// truncate possible numerical errors in cos to [-1,1] interval
c = fmin(c,1.0);
c = fmax(c,-1.0);
const double psi = acos(c);
// R_Earth = 6371 km
// 1 NM = 1.852 km
// 1 degree = 60.0405 NM on a great circle
return 60.0405 * psi * (180.0/M_PI);
}
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
