Friday, July 31, 2009

C++ program?

We want to develop a C++ program that calculates the distance between two cities. We will use the longitude and latitude to determine the location of each city. This is not as straightforward as you might imagine because the distance is not in a straight line. Instead, one would travel along a great circle defined by the curve of the Earth’s surface and passing through the two locations. Therefore, the actual distance to travel is the distance along the segment of such a great circle.





If the coordinates for location 1 are (θ1, φ1) and the coordinates for location 2 are (θ2, φ2), then the distance from location 1 to location 2 is:





r × arccos( sin(θ1)sin(θ2) + cos(θ 1)cos(θ2)cos(φ2−φ1))





where:





r is the radius of the earth, in the units for which you wish to calculate the distance. Measured in kilometers, r = 6378.7


θ is the latitude measured in radians


φ is the longitude measured in radians








Your program must allow the user to calculate as many distances as he/she wishes. In other words at the end of your program you need to prompt the user if he/she wants to continue or not. If the user says "yes" (or 1) then the the user is prompted to enter other locations, if the user enters "no" (or 2) then the program terminates.

C++ program?
It's been years since I learned C++, but don't you have the formula already? All you gotta do is just declare the variables and do 3 of those cin things for the radius, latitude and longitude, and then just do cout%26lt;%26lt;(that equation) and then you're done.





For the continue or termination thing, you just have to do declare a string sAnswer for "yes" and "no"


Run the previous whole calculation in a loop, if sAnswer = "yes" then boolean is true and you repeat the loop, if its "no" then just terminate the program.
Reply:USE FORTRAN AND LEARN PROGRAMMING.


No comments:

Post a Comment