Question
Your goal is to print the cities in United States sorted in the increasing order of distance from Charlotte. Download from Canvas the text file
Your goal is to print the cities in United States sorted in the increasing order of distance from Charlotte. Download from Canvas the text file (cities.txt) containing the latitude and longitude information of nearly 30,000 cities in the United States. Calculate the distance from Charlotte to each of these cities. Sort the cities in ascending order of distance, and print the sorted list along with the distance from Charlotte, to an output file dist_clt.txt. Sample entries in your output would look like this Yorktown,IA 867.294 Here, 867.294 is the distance in miles from Charlotte. Do a sanity check to make sure your output is correct! Use the Haversine formula to calculate the great-circle distance between two points that is, the shortest distance over the earths surface giving an as-the-crow-flies distance between the points (ignoring any hills they fly over, of course!). Haversine formula: delta_phi = phi(city) - phi(Charlotte) delta_lambda = lambda(city) - lambda(Charlotte) a = sin(delta_phi/2) + cos(phi1)*cos(phi2)*sin(delta_lambda/2) c = 2*atan2(sqrt(a), sqrt(1-a)) d = R*c where phi is latitude, lambda is longitude, R is earths radius (mean radius = 3,959 miles); Note that angles need to be in radians to pass to trig functions! The latitude and longitude of Charlotte is 35.2060,-80.8290 Hint: Carefully think of the operations that you need to do - read line from file; split line into city name & state, latitude, longitude; store these in a data structure; calculate the distance; sort by distance to Charlotte; and print the output.
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started