write program in c Question 01 The straightline distance in

write program in c++

Question 01: The straight-line distance in kilometres between two locations on earth can be computed by the following two formulas: w = sin (tartitudes oriencerniadian) + cos(Latitude1_in_radians) cos (Latitude2_in_radians) * sin® (longtudco Lorencentration) 2 distance = earthRadiusInKilometers * 2 * atan2 (w, V1 - w) Where: earthRadiusInKilometers = 6371 Latitude DifferencelnRadians = (Latitude1 – Latitude2) radians LongitudeDifference erenceInRadians = (Longitudel – Longitude2) radians

Solution

//Program Begins //

#include <iostream>
#include <math.h>

using namespace std;
/* All function declarations */
int isValidLatitude(int degrees,int minutes,double seconds,char position); //Validation function for latitudes
int isValidLongitude(int degrees,int minutes,double seconds,char position); //Validation function for longitudes
double decimalDegrees(int degrees,int minutes,double seconds,char position); //conversion for decimal formate for latitudes,langitudes
double degreesToRadians(double degrees);                                       //conversion fro degrees to radians
double calculateDistance(double lat1,double lon1,double lat2,double lon2); //calculation for distance method

int main()
{
//varible for input
int deg_lat1,min_lat1,deg_lon1,min_lon1;
double sec_lat1,sec_lon1;
char pos_lat1,pos_lon1;
int deg_lat2,min_lat2,deg_lon2,min_lon2;
double sec_lat2,sec_lon2;
char pos_lat2,pos_lon2;
   int temp;
   double d2d_lat1,d2d_lon1,d2d_lat2,d2d_lon2;
   double r1_lat1,r1_lon1,r1_lat2,r1_lon2;
  
cout << \"Enter the latitude of location 1 \"<< endl;
cin >>deg_lat1>>min_lat1>>sec_lat1>>pos_lat1;
           temp=isValidLatitude(deg_lat1,min_lat1,sec_lat1,pos_lat1); // Validation function call for latitudes
       if(temp==0)
           {
           cout <<\"Error: Invalid latitude of location 1 \"<<endl;
           return 0;
           }
           d2d_lat1=decimalDegrees(deg_lat1,min_lat1,sec_lat1,pos_lat1); //convertion degrees to decimal form
       r1_lat1=degreesToRadians(d2d_lat1);                               //conversion to radius call

cout << \"Enter the Longitude of location 1 \"<< endl;
cin >>deg_lon1>>min_lon1>>sec_lon1>>pos_lon1;
   temp=isValidLongitude(deg_lon1,min_lon1,sec_lon1,pos_lon1); // Validation function call for longitude
       if(temp==0)
       {
       cout <<\"Error: Invalid Longitude of location 1 \"<<endl;
       return 0;
       }
       d2d_lon1=decimalDegrees(deg_lon1,min_lon1,sec_lon1,pos_lon1); //convertion degrees to decimal form
       r1_lon1=degreesToRadians(d2d_lon1);                               //conversion to radius call


   cout << \"Enter the latitude of location 2 \"<< endl;
cin >>deg_lat2>>min_lat2>>sec_lat2>>pos_lat2;
temp=isValidLatitude(deg_lat2,min_lat2,sec_lat2,pos_lat2);
if(temp==0)
   {

   cout <<\"Error: Invalid latitude of location 2 \"<<endl;
   return 0;
   }
   d2d_lat2=decimalDegrees(deg_lat2,min_lat2,sec_lat2,pos_lat2);
r1_lat2=degreesToRadians(d2d_lat2);

cout << \"Enter the Longitude of location 2 \"<< endl;
cin >>deg_lon2>>min_lon2>>sec_lon2>>pos_lon2;
temp=isValidLongitude(deg_lon2,min_lon2,sec_lon2,pos_lon2);
if(temp==0)
   {
   cout <<\"Error: Invalid Longitude of location 2 \"<<endl;
   return 0;
   }
   d2d_lon2=decimalDegrees(deg_lon2,min_lon2,sec_lon2,pos_lon2);
   r1_lon2=degreesToRadians(d2d_lon2);

double distance= calculateDistance(r1_lat1,r1_lon1,r1_lat2,r1_lon2);

cout <<\"Straight line distance from location 1 to 2 =\" << distance << \"Kilometers\";



return 0;
}

//User Defined Functions Definations //
int isValidLatitude(int degrees,int minutes,double seconds,char position)
{
if(degrees>90 && degrees<0)
   {
       return 0;
   }
   if(minutes>60 && minutes<0)
   {
       return 0;
   }
   if(seconds>60 && seconds<0)
   {
       return 0;
   }
   if(!(position==\'N\' || position==\'S\'))
   {
       return 0;
   }
   return 1;
}
int isValidLongitude(int degrees,int minutes,double seconds,char position)
{
   if(degrees>180 && degrees<0)
   {
       return 0;
   }
   if(minutes>60 && minutes<0)
   {
       return 0;
   }
   if(seconds>60 && seconds<0)
   {
       return 0;
   }
   if(!(position==\'W\' || position==\'E\'))
   {
       return 0;
   }
   return 1;
  
}
double decimalDegrees(int degrees,int minutes,double seconds,char position)
{
       double x=degrees+(minutes/60)+(seconds/3600);
       if (position==\'S\' || position==\'W\')
       {
           x=x*(-1);
       }
       return x;

}
double degreesToRadians(double degrees)
{
   double rads=(180*degrees)/3.14159;
   return rads;
}
double calculateDistance(double lat1,double lon1,double lat2,double lon2)
{
   //calculation as per formulae
   double latdiff=lat1-lat2;
   double londiff=lon1-lon2;
double w=(0.5*(1-cos(2*(latdiff/2))))+cos(lat1)*cos(lat2)*(0.5*(1-cos(2*(londiff/2)))); //sin^2(x) = 1/2(1 - cos(2x))
           double distance=6371*2*atan2(sqrt (w),sqrt (1-w));
           return distance;

}

//Program Ends //

Input :

Enter the latitude of location 1                                                                                                                                                                   

25 44 36.97 S                                                                                                                                                                                      

Enter the Longitude of location 1                                                                                                                                                                  

28 13 42.18 E                                                                                                                                                                                      

Enter the latitude of location 2                                                                                                                                                                   

25 54 5.02 S                                                                                                                                                                                       

Enter the Longitude of location 2                                                                                                                                                                  

43 10 23.49 W

Output :

Straight line distance from location 1 to 2 = 7149.8 kilometers

write program in c++ Question 01: The straight-line distance in kilometres between two locations on earth can be computed by the following two formulas: w = sin
write program in c++ Question 01: The straight-line distance in kilometres between two locations on earth can be computed by the following two formulas: w = sin
write program in c++ Question 01: The straight-line distance in kilometres between two locations on earth can be computed by the following two formulas: w = sin
write program in c++ Question 01: The straight-line distance in kilometres between two locations on earth can be computed by the following two formulas: w = sin

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site