C The great distance is the distance between two points on t

C++

The great distance is the distance between two points on the surface of a sphere. Although the earth is not a perfect sphere, it can be treated as a sphere with an average radius of 6,378.1 km for many applications. A location on the earth can be specified by its latitude and longitude GPS coordinates (in degrees), denoted as (lat, long). For example, Regina’s GPS coordinates is (50.4547, -104.6067).

Let (x1, y1) and (x2, y2) be two GPS coordinates. The great circle distance between the two

points can be computed using the following formula:

d = radius X arccos( sin(x1) X sin(x2) + cos(x1) X cos(x2) X cos(y1 – y2) )

(a) FindtheGPScoordinatesforthefollowingcitiesfromtheInternet:

Saskatoon

Calgary

Vancouver

Toronto

Ottawa

Montreal

I solved it but I am not sure if my answer is correct or not

here is my answer

#include <iostream>
#include <cmath>
#include <iomanip>
using namespace std;

const double PI = 3.14159;
const double RADIUS = 6378.1;

int main()
{
   // declare double type variables to store longitude and latitude of four cities
   // Saskatoon
   double x1 = 52.1332;
   double y1 = 106.6700;
   // Calgary
   double x2 = 51.0486;
   double y2 = 114.0708;
   // Vancouver
   double x3 = 49.2827;
   double y3 = 123.1207;
   // Toronto
   double x4 = 43.6532;
   double y4 = 79.3832;
   // Ottawa
   double x5 = 45.4215;
   double y5 = 75.6972;
   // Montreal
   double x6 = 45.5017;
   double y6 = 73.5673;

   // display longitude and latitude of four cities
   cout << \"Saskatoon (latitude and longitude) in degrees are: \"
   << fixed << setprecision(5) << x1 << \" \" << fixed << setprecision(5) << y1 << endl;
   cout << \"Calgary (latitude and longitude) in degrees are: \"
   << fixed << setprecision(5) << x2 << \" \" << fixed << setprecision(5) << y2 << endl;
   cout << \"Vancouver (latitude and longitude) in degrees are: \"
   << fixed << setprecision(5) << x3 << \" \" << fixed << setprecision(5) << y3 << endl;
   cout << \"Toronto (latitude and longitude) in degrees are: \"
   << fixed << setprecision(5) << x4 << \" \" << fixed << setprecision(5) << y4 << endl;
   cout << \"Ottawa (latitude and longitude) in degrees are: \"
   << fixed << setprecision(5) << x5 << \" \" << fixed << setprecision(5) << y5 << endl;
   cout << \"Montreal (latitude and longitude) in degrees are: \"
   << fixed << setprecision(5) << x6 << \" \" << fixed << setprecision(5) << y6 << endl;


   // convert the coordinates into radian
   x1 = x1 * (PI / 180);
   y1 = y1 * (PI / 180);
   x2 = x2 * (PI / 180);
   y2 = y2 * (PI / 180);
   x3 = x3 * (PI / 180);
   y3 = y3 * (PI / 180);
   x4 = x4 * (PI / 180);
   y4 = y4 * (PI / 180);
   x5 = x5 * (PI / 180);
   y5 = y5 * (PI / 180);
   x6 = x6 * (PI / 180);
   y6 = y6 * (PI / 180);


   // calculate and display distance between Saskatoon and Calgary
   double distance12 = RADIUS * acos(sin(x1) * sin(x2) + cos(x1) * cos(x2) * cos(y1 - y2));

   cout << \"Distance from Saskatoon to Calgary: \"
   << fixed << setprecision(5) << distance12 << \" km\" << endl;

   // calculate and display distance between Saskatoon and Vancouver
   double distance13 = RADIUS * acos(sin(x1) * sin(x3) + cos(x1) * cos(x3) * cos(y1 - y3));
  
   cout << \"Distance from Saskatoon to Vancouver: \"
   << fixed << setprecision(5) << distance13 << \" km\" << endl;

   // calculate and display distance between Saskatoon and Toronto
   double distance14 = RADIUS * acos(sin(x1) * sin(x4) + cos(x1) * cos(x4) * cos(y1 - y4));
  
   cout << \"Distance from Saskatoon to Toronto: \"
   << fixed << setprecision(5) << distance14 << \" km\" << endl;

   // calculate and display distance between Saskatoon and Ottawa
   double distance15 = RADIUS * acos(sin(x1) * sin(x5) + cos(x1) * cos(x5) * cos(y1 - y5));
  
   cout << \"Distance from Saskatoon to Ottawa: \"
   << fixed << setprecision(5) << distance15 << \" km\" << endl;

   // calculate and display distance between Saskatoon and Montreal
   double distance16 = RADIUS * acos(sin(x1) * sin(x6) + cos(x1) * cos(x6) * cos(y1 - y6));

   cout << \"Distance from Saskatoon to Montreal: \"
   << fixed << setprecision(5) << distance16 << \" km\" << endl;

   // calculate and display distance between Calgary and Vancouver
   double distance23 = RADIUS * acos(sin(x2) * sin(x3) + cos(x2) * cos(x3) * cos(y2 - y3));

   cout << \"Distance from Calgary to Vancouver: \"
   << fixed << setprecision(5) << distance23 << \" km\" << endl;

   // calculate and display distance between Calgary and Toronto
   double distance24 = RADIUS * acos(sin(x2) * sin(x4) + cos(x2) * cos(x4) * cos(y2 - y4));

   cout << \"Distance from Calgary to Toronto: \"
   << fixed << setprecision(5) << distance24 << \" km\" << endl;

   // calculate and display distance between Calgary and Ottawa
   double distance25 = RADIUS * acos(sin(x2) * sin(x5) + cos(x2) * cos(x5) * cos(y2 - y5));

   cout << \"Distance from Calgary Ottawa: \"
   << fixed << setprecision(5) << distance25 << \" km\" << endl;

   // calculate and display distance between Calgary and Montreal
   double distance26 = RADIUS * acos(sin(x2) * sin(x6) + cos(x2) * cos(x6) * cos(y2 - y6));

   cout << \"Distance from Calgary to Montreal: \"
   << fixed << setprecision(5) << distance26 << \" km\" << endl;

   // calculate and display distance between Vancouver and Toronto
   double distance34 = RADIUS * acos(sin(x3) * sin(x4) + cos(x3) * cos(x4) * cos(y3 - y4));

   cout << \"Distance from Vancouver to Toronto: \"
   << fixed << setprecision(5) << distance34 << \" km\" << endl;

   // calculate and display distance between Vancouver and Ottawa
   double distance35 = RADIUS * acos(sin(x3) * sin(x5) + cos(x3) * cos(x5) * cos(y3 - y5));

   cout << \"Distance from Vancouver to Ottawa: \"
   << fixed << setprecision(5) << distance35 << \" km\" << endl;

   // calculate and display distance between Vancouver and Montreal
   double distance36 = RADIUS * acos(sin(x3) * sin(x6) + cos(x3) * cos(x6) * cos(y3 - y6));

   cout << \"Distance from Vancouver to Montreal: \"
   << fixed << setprecision(5) << distance36 << \" km\" << endl;

   // calculate and display distance between Toronto and Ottawa
   double distance45 = RADIUS * acos(sin(x4) * sin(x5) + cos(x4) * cos(x5) * cos(y4 - y5));

   cout << \"Distance from Toronto to Ottawa: \"
   << fixed << setprecision(5) << distance45 << \" km\" << endl;

   // calculate and display distance between Toronto and Montreal
   double distance46 = RADIUS * acos(sin(x4) * sin(x6) + cos(x4) * cos(x6) * cos(y4 - y6));

   cout << \"Distance from Toronto to Montreal: \"
   << fixed << setprecision(5) << distance46 << \" km\" << endl;

   // calculate and display distance between Ottawa and Montreal
   double distance56 = RADIUS * acos(sin(x5) * sin(x6) + cos(x5) * cos(x6) * cos(y5 - y6));

   cout << \"Distance from Ottawa to Montreal: \"
   << fixed << setprecision(5) << distance56 << \" km\" << endl;


   // area of triangle having vertex 123
   double s = (distance12 + distance13 + distance23) / 2;
   double area123 = sqrt(s * (s - distance12) * (s - distance13) * (s - distance23));

   // area of triangle having vertex 134
   s = (distance13 + distance14 + distance34) / 2;
   double area134 = sqrt(s * (s - distance13) * (s - distance14) * (s - distance34));

   // area of triangle having vertex 156
   s = (distance15 + distance16 + distance56) / 2;
   double area156 = sqrt(s * (s - distance15) * (s - distance16) * (s - distance56));

   // area of triangle having vertex 245
   s = (distance24 + distance25 + distance45) / 2;
   double area245 = sqrt(s * (s - distance24) * (s - distance25) * (s - distance45));

   // area of triangle having vertex 246
   s = (distance24 + distance26 + distance46) / 2;
   double area246 = sqrt(s * (s - distance24) * (s - distance26) * (s - distance46));

   // area of triangle having vertex 356
   s = (distance35 + distance36 + distance56) / 2;
   double area356 = sqrt(s * (s - distance35) * (s - distance36) * (s - distance56));

   // display area covered four cities.
   cout << \"Total area enclosed by four cities: \" << fixed << setprecision(5)
   << (area123 + area134 + area156 + area245 + area246 + area356) << \" sq.km\" << endl;

   return 0;


}

Solution

//what you wrote is absolutely correct but make the statements reuseable

//convery the distance and to tadians to functios to make the code simple

//find the below modifies code.

#include <iostream>
#include <cmath>
#include <iomanip>
using namespace std;
const double PI = 3.14159;
const double RADIUS = 6371;
double toRad(double);
double calDistance(double, double, double, double);
int main()
{
// declare double type variables to store longitude and latitude of four cities
// Saskatoon
double x1 = 52.1332;
double y1 = 106.6700;
// Calgary
double x2 = 51.0486;
double y2 = 114.0708;
// Vancouver
double x3 = 49.2827;
double y3 = 123.1207;
// Toronto
double x4 = 43.6532;
double y4 = 79.3832;
// Ottawa
double x5 = 45.4215;
double y5 = 75.6972;
// Montreal
double x6 = 45.5017;
double y6 = 73.5673;

// display longitude and latitude of four cities
cout << \"Saskatoon (latitude and longitude) in degrees are: \"
<< fixed << setprecision(5) << x1 << \" \" << fixed << setprecision(5) << y1 << endl;
cout << \"Calgary (latitude and longitude) in degrees are: \"
<< fixed << setprecision(5) << x2 << \" \" << fixed << setprecision(5) << y2 << endl;
cout << \"Vancouver (latitude and longitude) in degrees are: \"
<< fixed << setprecision(5) << x3 << \" \" << fixed << setprecision(5) << y3 << endl;
cout << \"Toronto (latitude and longitude) in degrees are: \"
<< fixed << setprecision(5) << x4 << \" \" << fixed << setprecision(5) << y4 << endl;
cout << \"Ottawa (latitude and longitude) in degrees are: \"
<< fixed << setprecision(5) << x5 << \" \" << fixed << setprecision(5) << y5 << endl;
cout << \"Montreal (latitude and longitude) in degrees are: \"
<< fixed << setprecision(5) << x6 << \" \" << fixed << setprecision(5) << y6 << endl;

// calculate and display distance between Saskatoon and Calgary
double distance12 = calDistance(x1,y1,x2,y2);
cout << \"Distance from Saskatoon to Calgary: \"
<< fixed << setprecision(5) << distance12 << \" km\" << endl;
// calculate and display distance between Saskatoon and Vancouver
double distance13 = calDistance(x1,y1,x3,y3);
  
cout << \"Distance from Saskatoon to Vancouver: \"
<< fixed << setprecision(5) << distance13 << \" km\" << endl;
// calculate and display distance between Saskatoon and Toronto
double distance14 = calDistance(x1,y1,x4,y4);
  
cout << \"Distance from Saskatoon to Toronto: \"
<< fixed << setprecision(5) << distance14 << \" km\" << endl;
// calculate and display distance between Saskatoon and Ottawa
double distance15 = calDistance(x1,y1,x5,y5);
  
cout << \"Distance from Saskatoon to Ottawa: \"
<< fixed << setprecision(5) << distance15 << \" km\" << endl;
// calculate and display distance between Saskatoon and Montreal
double distance16 = calDistance(x1,y1,x6,y6);
cout << \"Distance from Saskatoon to Montreal: \"
<< fixed << setprecision(5) << distance16 << \" km\" << endl;
// calculate and display distance between Calgary and Vancouver
double distance23 = calDistance(x2,y2,x3,y3);
cout << \"Distance from Calgary to Vancouver: \"
<< fixed << setprecision(5) << distance23 << \" km\" << endl;
// calculate and display distance between Calgary and Toronto
double distance24 = calDistance(x2,y2,x4,y4);
cout << \"Distance from Calgary to Toronto: \"
<< fixed << setprecision(5) << distance24 << \" km\" << endl;
// calculate and display distance between Calgary and Ottawa
double distance25 = calDistance(x2,y2,x5,y5);
cout << \"Distance from Calgary Ottawa: \"
<< fixed << setprecision(5) << distance25 << \" km\" << endl;
// calculate and display distance between Calgary and Montreal
double distance26 = calDistance(x2,y2,x6,y6);
cout << \"Distance from Calgary to Montreal: \"
<< fixed << setprecision(5) << distance26 << \" km\" << endl;
// calculate and display distance between Vancouver and Toronto
double distance34 = calDistance(x3,y3,x4,y4);
cout << \"Distance from Vancouver to Toronto: \"
<< fixed << setprecision(5) << distance34 << \" km\" << endl;
// calculate and display distance between Vancouver and Ottawa
double distance35 = calDistance(x3,y3,x5,y5);
cout << \"Distance from Vancouver to Ottawa: \"
<< fixed << setprecision(5) << distance35 << \" km\" << endl;
// calculate and display distance between Vancouver and Montreal
double distance36 = calDistance(x3,y3,x6,y6);
cout << \"Distance from Vancouver to Montreal: \"
<< fixed << setprecision(5) << distance36 << \" km\" << endl;
// calculate and display distance between Toronto and Ottawa
double distance45 = calDistance(x4,y4,x5,y5);
cout << \"Distance from Toronto to Ottawa: \"
<< fixed << setprecision(5) << distance45 << \" km\" << endl;
// calculate and display distance between Toronto and Montreal
double distance46 =calDistance(x4,y4,x6,y6);
cout << \"Distance from Toronto to Montreal: \"
<< fixed << setprecision(5) << distance46 << \" km\" << endl;
// calculate and display distance between Ottawa and Montreal
double distance56 = calDistance(x5,y5,x6,y6);
cout << \"Distance from Ottawa to Montreal: \"
<< fixed << setprecision(5) << distance56 << \" km\" << endl;

// area of triangle having vertex 123
double s = (distance12 + distance13 + distance23) / 2;
double area123 = sqrt(s * (s - distance12) * (s - distance13) * (s - distance23));
// area of triangle having vertex 134
s = (distance13 + distance14 + distance34) / 2;
double area134 = sqrt(s * (s - distance13) * (s - distance14) * (s - distance34));
// area of triangle having vertex 156
s = (distance15 + distance16 + distance56) / 2;
double area156 = sqrt(s * (s - distance15) * (s - distance16) * (s - distance56));
// area of triangle having vertex 245
s = (distance24 + distance25 + distance45) / 2;
double area245 = sqrt(s * (s - distance24) * (s - distance25) * (s - distance45));
// area of triangle having vertex 246
s = (distance24 + distance26 + distance46) / 2;
double area246 = sqrt(s * (s - distance24) * (s - distance26) * (s - distance46));
// area of triangle having vertex 356
s = (distance35 + distance36 + distance56) / 2;
double area356 = sqrt(s * (s - distance35) * (s - distance36) * (s - distance56));

// display area covered four cities.
cout << \"Total area enclosed by four cities: \" << fixed << setprecision(5)
<< (area123 + area134 + area156 + area245 + area246 + area356) << \" sq.km\" << endl;
return 0;

}
double toRad(double degree) {
return (degree/180) * (3.14);
}

double calDistance(double x1, double y1, double x2, double y2) {
double distance;
distance = acos(sin(toRad(x1)) * sin(toRad(x2)) + cos(toRad(x1)) * cos(toRad(x2)) * cos(toRad(y1 - y2)));
distance = RADIUS * distance;
return distance;
}

C++ The great distance is the distance between two points on the surface of a sphere. Although the earth is not a perfect sphere, it can be treated as a sphere
C++ The great distance is the distance between two points on the surface of a sphere. Although the earth is not a perfect sphere, it can be treated as a sphere
C++ The great distance is the distance between two points on the surface of a sphere. Although the earth is not a perfect sphere, it can be treated as a sphere
C++ The great distance is the distance between two points on the surface of a sphere. Although the earth is not a perfect sphere, it can be treated as a sphere
C++ The great distance is the distance between two points on the surface of a sphere. Although the earth is not a perfect sphere, it can be treated as a sphere
C++ The great distance is the distance between two points on the surface of a sphere. Although the earth is not a perfect sphere, it can be treated as a sphere

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site