Question

The following C++  code contains an incomplete program that should be able to calculate the distance between...

The following C++  code contains an incomplete program that should be able to calculate the distance between a series of geocoded locations. Two parts of the program need to be completed:

1. The portion of the main() function that calculates the distances between each of the hard-coded 5 locations, and stores it in a two-dimensional array declared as distances.

2. A portion of the calculateDistanceBetweenLocations(l1, l2) function; omitted code spaces are designed with a // TODO: comment.

The code is properly commented in order for you to understand exactly what is being done, however you should not touch parts of the program that are not earmarked with a // TODO: comment.
CODE:

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

using namespace std;

// Location Structure
struct Location {
// Name of Location
string name;
// Latitude of Location
double latitude;
// Longitude of Location
double longitude;
};

// Location Structure using Radians
struct LocationWRadians {
// Name of Location
string name;
// Latitude Radians
long double latitude;
// Longitude Radians
long double longitude;

LocationWRadians() {
name = "";
latitude = 0;
longitude = 0;
}

LocationWRadians(string n, long double lat, long double longi) {
name = n;
latitude = lat;
longitude = longi;
}
};

/**
* Calculates the distance between two geographic locations and returns this value
*
* @param Location l1 The first location
* @param Location l2 The second location
* @return double
*
*/
long double calculateDistanceBetweenLocations(Location l1, Location l2);

/**
* Displays Summary Table Header
*
* @param locations
* @param size
*/
void displaySummaryTableHeader(const Location locations[], int size);

/**
* Display Summary of Distances
* @param locations
* @param distances
*/
void displaySummaryTable(const Location locations[], const long double distances[5][5]);

/**
* Converts earthly degrees to radians
* @param degree
* @return long double
*/
long double toRadians(long double degree);

int main() {
// An array of five locations
const Location locations[5] = {
{"Moscow", 55.75, 37.62}, {"Calcutta", 22.6763858, 88.0495367},
{"Sao Paulo", -23.6815315,-46.8754883}, {"Buenos Aires", -34.6156625,-58.5033383},
{"New York", 40.6976701,-74.2598661}
};
// An two-dimensional array to hold the distance between each location
long double distances[5][5];
// Calculate distances between all locations and store them in the distances array declared above such that:
// each row of the array represents one of the locations AND
// each column of the array represents the set of distances between the row location and each other location
// HINT: You need two loops, one nested inside of the other. The calculateDistanceBetweenLocations function can be used
// to find the distance between two locations
// TODO: WRITE YOUR CODE BELOW

// END YOUR CODE
// Output Distances In A Table
displaySummaryTableHeader(locations, 5);
displaySummaryTable(locations, distances);

cout << endl << "Program ending...";
return 0;
}


/***********************************
***** Function Implementations ****
***********************************/
void displaySummaryTableHeader(const Location locations[], int size) {
cout << setw(15) << "";
for (int i = 0; i < size; i++) {
cout << setw(20) << locations[i].name;
}
cout << endl;
}

void displaySummaryTable(const Location locations[], const long double distances[5][5]) {
for(int i = 0; i < 5; i++) {
cout << setw(15) << locations[i].name;
for (int j = 0; j < 5; j++) {
cout << setw(20) << ((distances[i][j] == 0.00) ? "" : to_string(static_cast<int>(distances[i][j])));
}
cout << endl;
}
}

long double toRadians(const long double degree) {
long double single_degree = M_PI/180;
return single_degree * degree;
}

// TODO: Complete Function
long double calculateDistanceBetweenLocations(const Location l1, const Location l2) {
// Convert the latitude and longitudes from degree to radians and store the radians version of the two locations
// in the two new LocationWRadians objects, radians1 & radians2.
// TODO: Modify code below. You may use the toRadians(degree) function to calculate the radians value, passing the function either a longitude or latitude value
LocationWRadians radians1;
LocationWRadians radians2;


// Calculate Distance Using Haversine Formula
// 1. Calculate difference between radians longitude of second location and first location
long double longitude_difference = // TODO: Write Formula Here

// 2. Calculate difference between radians latitude of second location and first location
long double latitude_difference = // TODO: Write Formula Here

// LEAVE CODE BELOW UNTOUCHED
// Calculate a
long double a = pow(sin(latitude_difference/2), 2) + cos(radians1.latitude)
* cos(radians2.latitude) * pow(sin(longitude_difference/2), 2);

// Calculate b
long double b = 2 * asin(sqrt(a));

// Radius of Earth in
// Kilometers, R = 6371
// Use R = 3956 for miles
const long double R = 6371;

// Multiple b * R and return result
return b * R;
}

0 0
Add a comment Improve this question Transcribed image text
Answer #1

# include <toSt Team > uclude Sriug? 1.dat d ectenau.ip, ザ :/.. define -t_P13111(5926 5358974了238u6 淋eudtt // Location S4.tuchne. lNau of Locction navut double latitude ; cleuble longtudeRadiaus. tediste behoer bo geegraphic ょ@patam Locatin. M, Tha叔8tle catim , etum double og double calcwlate Diztarice Behweenl七大 херс.ua m disknutej onven easy degres to radians * e nastauy degree ardly degrees to tad a i Iut niain comt Lotation locatio(5J u moscou, 55.75,31. 6 1 3, { catatta, 오오 67638 58,3; l distauce betuween eact, locahio ouble t n2 812adiauy rona oble dege nge decre Calculate L tong double ntla)) Const tong dloobte P 67)Output Moscow Calcutta Sao Paulo Buenos Aires New York Moscow 5635.062416 12119.167677 14343.329026 8769.213280 Calcutta 6649.192927 16917806235-nan(ind) 16490.307877 Sao Paulo 14469.888336 16760.806754 1737.271268 7866.369342 Buenos Aires 17403.438783 18432.741719 1691627899 8577.320346 New York 10485.877171 13562839498 7746.763504 8561.398765

Add a comment
Know the answer?
Add Answer to:
The following C++  code contains an incomplete program that should be able to calculate the distance between...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • C++ Programming Hi! Sorry for all the material attached. I simply need help in writing the...

    C++ Programming Hi! Sorry for all the material attached. I simply need help in writing the Facility.cpp file and the other files are included in case they're needed for understanding. I was able to complete the mayday.cpp file but am stuck on Facility. The following link contains a tar file with the files provided by the professor. Thank you so much in advanced! http://web.cs.ucdavis.edu/~fgygi/ecs40/homework/hw4/ Closer.h: #ifndef CLOSER_H #define CLOSER_H #include <string> #include "gcdistance.h" struct Closer { const double latitude, longitude;...

  • Please help fix my code C++, I get 2 errors when running. The code should be...

    Please help fix my code C++, I get 2 errors when running. The code should be able to open this file: labdata.txt Pallet PAG PAG45982IB 737 4978 OAK Container AYF AYF23409AA 737 2209 LAS Container AAA AAA89023DL 737 5932 DFW Here is my code: #include <iostream> #include <string> #include <fstream> #include <vector> #include <cstdlib> #include <iomanip> using namespace std; const int MAXLOAD737 = 46000; const int MAXLOAD767 = 116000; class Cargo { protected: string uldtype; string abbreviation; string uldid; int...

  • Please program in C++ and document the code as you go so I can understand what...

    Please program in C++ and document the code as you go so I can understand what you did for example ///This code does~ Your help is super appreciated. Ill make sure to like and review to however the best answer needs. Overview You will revisit the program that you wrote for Assignment 2 and add functionality that you developed in Assignment 3. Some additional functionality will be added to better the reporting of the students’ scores. There will be 11...

  • soccerApp.cpp is used to test your code and contains the main function. No changes to this...

    soccerApp.cpp is used to test your code and contains the main function. No changes to this file are necessary. Inside of the soccerPlayer.cpp file, implement the three member functions that are specified in soccerPlayer.hpp. The member function definition for print is already provided. soccerPlayer.cpp: #include "soccerPlayer.hpp" void TopTwoPlayer::print() const { cout << left << setw(8) << "Name: " << setw(18) << name << setw(10) << "Country:" << setw(18) << country << setw(14) << "appearances:" << right << setw(3) << appearances...

  • I want to change this code and need help. I want the code to not use...

    I want to change this code and need help. I want the code to not use parallel arrays, but instead use one array of struct containing the data elements, String for first name, String for last name,Array of integers for five (5) test scores, Character for grade. If you have any idea help would be great. #include #include #include #include using namespace std; const int NUMBER_OF_ROWS = 10; //number of students const int NUMBER_OF_COLUMNS = 5; //number of scores void...

  • C++ Assignment Project 1 - NodeList Building upon the the ListNode/List code I would like you...

    C++ Assignment Project 1 - NodeList Building upon the the ListNode/List code I would like you to extend the interface of a list to have these member functions as well. struct ListNode { int element; ListNode *next; } Write a function to concatenate two linked lists. Given lists l1 = (2, 3, 1)and l2 = (4, 5), after return from l1.concatenate(l2)the list l1should be changed to be l1 = (2, 3, 1, 4, 5). Your function should not change l2and...

  • C++ Assignment Project 1 - NodeList Building upon the the ListNode/List code I would like you to extend the interface of...

    C++ Assignment Project 1 - NodeList Building upon the the ListNode/List code I would like you to extend the interface of a list to have these member functions as well. struct ListNode { int element; ListNode *next; } Write a function to concatenate two linked lists. Given lists l1 = (2, 3, 1)and l2 = (4, 5), after return from l1.concatenate(l2)the list l1should be changed to be l1 = (2, 3, 1, 4, 5). Your function should not change l2and...

  • In C programming, code the following function: Write the function Location() that takes in two pairs...

    In C programming, code the following function: Write the function Location() that takes in two pairs of DEGREE (latitude, longitude) coordinates and returns the great-circle-distance between the points on the surface of the Earth, which is calculated using the formula: d = R acos[ cos(latA)cos(latB)cos(lonB-lonA) + sin(latA)sin(latB) ] where (latA,lonA) and (latB,lonB) are the RADIAN coordinates for the two points, R = 6368 km is the radius of the Earth (provided in the template as a global variable), d is...

  • C++ Create a program that finds the dot product of two vectors. I'm currently trying to...

    C++ Create a program that finds the dot product of two vectors. I'm currently trying to display the dot product by calling the dotProduct member function however I am confused as to how to do this. What would be the proper way to display the dot product? I don't believe my dotProduct member function is set up correctly to access the proper data. Feel free to modify the dotProduct member function to allow for it to work with the other...

  • The following code skeleton contains a number of uncompleted methods. With a partner, work to complete...

    The following code skeleton contains a number of uncompleted methods. With a partner, work to complete the method implementations so that the main method runs correctly: /** * DESCRIPTION OF PROGRAM HERE * @author YOUR NAME HERE * @author PARTNER NAME HERE * @version DATE HERE * */ import java.util.Arrays; import java.util.Random; import java.util.Scanner; public class ArrayExercises { /** * Given a random number generator and a length, create a new array of that * length and fill it from...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT