Sample – compute distance between cities
by Nideesh C on February 8, 2011 · 0 comments
in C++
// Simple program to compute the distance between two cities
// given their coordinates
// May not work for some values (two points on opposite sides of
// the equator). Need to do some thinking about that.
#include<iostream.h>
#include<math.h>
float toRadian (float angle) {
float const PI = atan(1.0)*4.0;
return(angle * PI / 180.0);
}
float computeDist (float lat1, float long1, float lat2, float long2) {
float const R = 3964.0;
float temp;
temp = sin(lat1) * sin(lat2) + cos(lat1) * cos(lat2) * cos(long1-long2);
return(2.0 * R * atan(sqrt((1.0-temp)/(1.0+temp))));
}
void main () {
float lat1, long1, lat2, long2, distance;
cout << “Enter lat1, long1: “;
cin >> lat1 >> long1;
cout << “Enter lat2, long2: “;
cin >> lat2 >> long2;
distance = computeDist(toRadian(lat1),
toRadian(long1),
toRadian(lat2),
toRadian(long2));
cout << “Distance = ” << distance << endl;
}
Not Satisfied ? Just search & get the result
Related posts:
- Sample – compute square roots using Newton’s method
- Sample – convert temperatures
- Sample – Fibonacci numbers
- Sample – factorial
- Sample program (multiplication using addition)
Tagged as:
c programming examples,
c programming reference,
c tutorial,
c++ program examples,
C++ Programming,
compute distance between cities,
learning c++,
programming languages,
programming languages c,
sample c++ programs
Me, freelance system administrator having the qualification of Diploma in Electronics & Tele-communication + MCSE + CCNA + CST + 5 years of experience in IT field.
If you like This post, you can follow TheOnlineTutorials on Twitter.
Contact me Via email: support@theonlinetutorials.com
Subscribe to feed via Feed or EMAIL to receive instant updates.
Legal Disclaimer:All information found on the site is without any implied warranty of fitness for any purpose or use whatsoever. Content author/site administrator is not responsible for any loss occurred due to mistakes in this tutorial. Use this tutorial website at your own risk.