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;
}
The following C++ code contains an incomplete program that should be able to calculate the distance between...
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 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 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 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 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 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 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 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 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 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...