I am making a program to calculate the distance between 2 location by inputting the latitude and longitude of the locations using DMS, then convert the given DMS to angular measurement. I have made the program using functions but when it finally calculate for the distance, it always goes to zero. Need help on using reference or pointer right because I believe I'm not using it right.

This is the program. it is kind of long so please bear with me.

#include <cmath>
#include <iostream>

using namespace std;

void Latitude(int, int, int, char, double&);
void Longitude(int, int, int, char, double&); 
double const Pi = acos(-1.0);

int main()
{
   
      int  degrees[4], minutes[4], seconds[4];         
      char  direction[4];
      double Alpha[2], Beta[2], R = 3958.89, theta, distance, temp[2], alpha, beta; 
      
cout << "\nThis program will estimate the distance between two"
     << "\npoints on the earth when location is input by"
     << "\nLatitude and Longitude." << endl ;
                 
cout << "\nEnter the Latitude - degrees, minutes, seconds and first letter of the"
     << "\ndirection (ex. N for north) (From)" << endl;
    
     cin >> degrees[0] 
         >> minutes[0]
         >> seconds[0]
         >> direction[0];      

cout << "\nNow enter the Longitude "
     << " (From)" << endl;
        
     cin >> degrees[1] 
         >> minutes[1]
         >> seconds[1]
         >> direction[1];

cout << "\nEnter the Latitude "
     << " (To)" << endl;
    
     cin >> degrees[2] 
         >> minutes[2]
         >> seconds[2]
         >> direction[2];      

cout << "\nNow enter the Longitude "
     << " (To)" << endl;
        
     cin >> degrees[3] 
         >> minutes[3]
         >> seconds[3]
         >> direction[3];                  
//------------------------Calls Function         
         cout << "\n\t From"<< endl ;
         cout << "----------------------------------" << endl ;
Latitude(degrees[0], minutes[0], seconds[0], direction[0], alpha); 
Longitude(degrees[1], minutes[1], seconds[1], direction[1], beta); 
         cout << "\n---------------------------------" << endl ;
Alpha[0] = alpha;       
Beta[0]  = beta;

         cout << "\n\n\t To" << endl ;
         cout << "-----------------------------------" << endl ;
Latitude(degrees[2], minutes[2], seconds[2], direction[2], alpha); 
Longitude(degrees[3], minutes[3], seconds[3], direction[3], beta);
         cout << "\n---------------------------------" << endl ;
         
Alpha[1] = alpha;
Beta[1]  = beta;
//-------------------------Formula to find distance
temp[0] = cos(Alpha[0]*180.0/Pi)*cos(Alpha[0]*180.0/Pi);
temp[1] = sin(Alpha[0]*180.0/Pi)*sin(Alpha[0]*180.0/Pi)*cos(Beta[0]*180.0/Pi-Beta[1]*180.0/Pi);

theta = acos(temp[0]+temp[1]);

distance = R * theta;

cout << "\n\nThe Distance between the two coordinates are " << distance << " miles" << endl << endl ;
 

system("pause");
return 0;
      
}

//-----------------------FUNCTIONS-------------------------------

void Latitude(int degrees, int minutes, int seconds, char direction, double& alpha )
{
      double new_degrees, alp;
      
       if (direction == 'N' ||  direction == 'n')
       {
             new_degrees = degrees + (minutes / 60.0)
                       + (seconds / 6.00 / 60.0 );
       alp = 90.0 - new_degrees;
        cout << "\nAngular Measurement : " << new_degrees << " N";
            
       }      
       else if (direction == 'S' ||  direction == 's')
       {
             new_degrees = degrees + (minutes / 60.0)
                       + (seconds / 60.0 / 60.0 );
       alp = 90.0 + new_degrees;
       cout << "\nAngular Measurement : " << new_degrees << " S";
       }
        else 
       cout << "\nInvalid Direction";         
        
       }  
          
void Longitude(int degrees, int minutes, int seconds, char direction, double& beta)      
      {
        
       double new_degrees, bet;
       if (direction == 'W' ||  direction == 'w')
       {
             new_degrees = degrees + (minutes / 60.0)
                       + (seconds / 60.0 / 60.0 ); 
       cout << "\nAngular Measurement : " << new_degrees << " W"; 
            beta = new_degrees;
            
       }      
       else if (direction == 'E' ||  direction == 'e')
       {
             new_degrees = degrees + (minutes / 60.0)
                       + (seconds / 60.0 / 60.0 );
                       
                 beta = 360.0 - new_degrees;      
       cout << "\nAngular Measurement : " << new_degrees << " E"; 
        
       }     
       
       else 
            cout << "\nInvalid Direction";

             
}

//-------------------------------------------------------------------------

I think the only problem is that on line 95 and 103, you are using "alp" instead of "alpha". For the rest, it is correct. To some extent, your use of outputting the value of alpha and beta by passing them by reference is correct but not necessary. If you have a function that has one output value (alpha and beta, respectively) then these should probably be the returned value and not a parameter. This is merely for good form though, not a requirement.

Look first dont pass arg to the 2 functions by ref.

You misuse the ref. calls in the functions. You over write the memory

beta = new_degrees;

and alpha's ref. is never used.

Forget about ref. for not ok? Just get the grip first.

When I change the alp to alpha, I get reasonable answer however it is not the right answer. Is it better to use double as a result on my function call than to use void so I can get the number that I need to calculate the distance and forget the ref. command? but I want to learn how to use ref. correctly. What is the best way to do the calculation?

Edited 5 Years Ago by Tinee: n/a

I believe you've got a typo on line 94:

+ (seconds / 6.00 / 60.0 );

Edited 5 Years Ago by Dingbats: n/a

Another note with lines 70 and 71:
Alpha[0] but no Alpha[1]

I'm getting very suspicous that these two lines of code do not calculate the correct values - they don't seem to tally-up with other examples on the net.

You appear to get it right with "*180.0/Pi" for the conversion to radians.

I'll look into this in more detail.
Could you tell me where you got your original idea for the math from?

David

Edited 5 Years Ago by Dingbats: Spelinggg!

Yes, that's right. The conversion from Degree to Radians is upside-down. It should be Pi / 180. As so:

#include <iostream>
#include <cmath>
#ifndef M_PI
#define M_PI 3.14159265358979323846
#endif
using namespace std;

int main() {
  double angle_in_degrees = 60;
  double angle_in_radians = angle_in_degrees * M_PI / 180.0;
  cout << "The cosine of " << angle_in_degrees 
       << " is " << cos(angle_in_radians) << endl;
  return 0;
};

Another note with lines 70 and 71:
Alpha[0] but no Alpha[1]

I'm getting very suspicous that these two lines of code do not calculate the correct values - they don't seem to tally-up with other examples on the net.

You appear to get it right with "*180.0/Pi" for the conversion to radians.

I'll look into this in more detail.
Could you tell me where you got your original idea for the math from?

David

This is a project that my teacher pass out couple days ago; the formula is from the sheet that he gave out. I got the program to run with the corrections and I thank you for that however, the mileage is just off by the decimals. Is there a correct way to find out the true distance or we can't calculate the true distance?

Is there a correct way to find out the true distance or we can't calculate the true distance?

Yes, but the formula changes significantly.

Just to clarify:
A fact which will introduce errors is that your using a mean earth radius (double R = 3958.89). This naturally simplifies the math but the earth isn't flat.

Another point is that the formula that your using (and many other examples found on the net) assume a spherical earth; the earth is in fact slightly ellipsoidal. This will also introduce errors into the calculation. Imagine calculating the distance from one side of a sphere to the opposite side and doing the same with the "long" side of an egg.

If you want to read the Vincenty’s formula (which is accurate to within 0.5mm, or 0.000015″) then go here

This question has already been answered. Start a new discussion instead.