Frequent Flyer miles
You must demonstrate this program in class no later than Tuesday, September 8, 2009. No credit after that.
- Question: When your airline awards you frequent flyer miles for travel, how do you know they are not cheating you?
- Answer: Write a C program that can calculate the distance between two cities given their latitude and longitude.
- Use the following in your program:
- Assume that a city P has latitude and longitude (a1,b1) and city Q has latitude and longitude (a2,b2), where a1,b1,a2,b2 are all in degrees. The shortest (great circle) distance between P and Q is given by the formula: acos[cos[a1] * cos[b1] * cos[a2] * cos[b2] + cos[a1] * sin[b1] * cos[a2] * sin[b2]
+ sin[a1] * sin[a2]] * radius of the earth. Here cos and sin refer to the cosine and sine of the corresponding angles, and acos refers to the inverse cosine. Also note that when using the formula, West longitudes and South latitudes have to be entered as negative numbers. To see how this formula is derived, go here.
- Assume that the radius of the earth is 3963 miles.
- Here are some additional hints:
- The C functions cos and sin return the cosine and sine of their respective radian arguments expressed as a double. Since the latitude and longitude are expressed in degrees, your program will have to convert them to radians before you can use the C functions. How do you convert from degrees to radians?
- The C function acos returns a double representing the inverse cosine of its argument.
- You can use the constant M_PI to refer to the constant pi.
- Use the following template for your C program. Download and save the file and add your C code to it.
- Compile the file using the command gcc-3 miles.c -o miles -lm
. The -lm option directs the compiler to include the Math library.
- Test your program using data for several cities.
- Use this site to find latitude and longitude information for cities in the US.
- Use this site to find latitude and longitude information for non-US cities.
- This site has a airline mileage calculator that tells you the frequent flyer miles you can expect for travel.