HAVING PROBLEMS WHERE WHEN PICKING NUMBER for location it uses the location for the next number. #include
Question:
HAVING PROBLEMS WHERE WHEN PICKING NUMBER for location it uses the location for the next number.
#include #include
const double EARTH_RADIUS = 3958.8; const double speed = 500.0;
using namespace std;
double haversine(double lat1, double long1, double lat2, double long2); double getFlightTime(double lat1, double long1, double lat2, double long2);
int main() { string locations[] = { "Miami Beach, FL, USA", "Fargo, ND, USA", "Idaho City, ID, USA", "Peabody, MA, USA", "Northampton, MA, USA", "Newton, MA, USA", "Newburyport, MA, USA", "New Bedford, MA, USA", "Medford, MA, USA", "Malden, MA, USA" // Add more locations };
double latitudes[] = {25.793449, 46.877186, 43.828850, 42.536457, 42.328674, 42.341042, 42.810356, 41.638409, 42.419331, 42.429752}; double longitudes[] = {-80.139198, -96.789803, -115.837860, -70.985786, -72.664658, -71.217133, -70.893875, -70.941208, -71.119720, -71.071022};
cout << "Available Locations: "; for (int i = 0; i < sizeof(locations) / sizeof(locations[0]); ++i) { cout << i + 1 << ". " << locations[i] << " "; }
int departureIndex; string input; do { cout << "Enter the number corresponding to your departure location: "; cin >> input; departureIndex = atoi(input.c_str()) - 1; } while (departureIndex < 0 || departureIndex >= sizeof(locations) / sizeof(locations[0]));
double departureLat = latitudes[departureIndex]; double departureLong = longitudes[departureIndex];
// Remove the selected departure location // Remove the selected departure location for (int i = departureIndex; i < sizeof(locations) / sizeof(locations[0]) - 1; ++i) { locations[i] = locations[i + 1]; latitudes[i] = latitudes[i + 1]; longitudes[i] = longitudes[i + 1]; }
// Update the latitude and longitude arrays for the last element latitudes[sizeof(locations) / sizeof(locations[0]) - 1] = 0.0; // Update with an appropriate default value longitudes[sizeof(locations) / sizeof(locations[0]) - 1] = 0.0; // Update with an appropriate default value
// Decrease the size of the arrays // Decrease the size of the arrays int newSize = sizeof(locations) / sizeof(locations[0]) - 1;
cout << "Available Destinations: "; for (int i = 0; i < newSize; ++i) { cout << i + 1 << ". " << locations[i] << " "; }
int destinationIndex; do { cout << "Enter the number corresponding to your destination location: "; cin >> input; destinationIndex = atoi(input.c_str()) - 1; } while (destinationIndex < 0 || destinationIndex >= newSize);
double destinationLat = latitudes[destinationIndex]; double destinationLong = longitudes[destinationIndex];
double distance = haversine(departureLat, departureLong, destinationLat, destinationLong); double flightTime = getFlightTime(departureLat, departureLong, destinationLat, destinationLong);
cout << "Distance between " << locations[departureIndex] << " and " << locations[destinationIndex] << ": " << distance << " miles "; cout << "Approximate flight time: " << flightTime << " hours ";
return 0; }
double haversine(double lat1, double long1, double lat2, double long2) { double dLat = lat2 - lat1; double dLon = long2 - long1; double a = sqrt(pow(sin((dLat)/2),2)+(cos(lat1)*cos(lat2)*(pow(sin((dLon)/2),2))));
//double a = sin(dLat / 2) * sin(dLat / 2) + cos(lat1) * cos(lat2) * sin(dLon / 2) * sin(dLon / 2); //double c = 2 * asin(a);
double distance = 2 * EARTH_RADIUS * asin(a);
return distance; }
double getFlightTime(double lat1, double long1, double lat2, double long2) { double distance = haversine(lat1, long1, lat2, long2);
double flightTime = distance / speed;
return flightTime; }