c++ putting objects into arrays -
i trying make city distance calculator using longitude , latitude of city. problem having putting cities array. array attempt commented out in main method. please let me know doing wrong.
#include <iostream> #include <math.h> #define pi 3.14159265358979323846 using namespace std; double distance(double lat1, double lon1, double lat2, double lon2, char unit); double deg2rad(double deg); double rad2deg(double rad); class city { public: //declare variables string name; double latitude; double longitude; city(string, double, double); //constructor double distance(string, string); }; double citydistance(city a, city b); int main() { //construct cities city providence("providence", 41.8239890, -71.4128340); city cranston("cranston", 41.7798230, -71.4372800); city newyork("newyork", 40.7143530, -74.0059730); city boston("boston", 42.3584310, -71.0597730); city killington("killington", 43.6775680, -72.7798250); city springfield("springfield", 42.1014830, -72.5898110); city bridgeport("bridgeport", 41.1865480, -73.1951770); city cambridge("cambridge", 42.3736160, -71.1097340); city norwalk("norwalk", 41.1175970, -73.4078970); city quincy("quincy", 42.2528770, -71.0022710); //put city array // city cityarray[] = { }; // cityarray[0] = {"providence", 41.8239890, -71.4128340}; // cityarray[1] = {"cranston", 41.7798230, -71.4372800}; // cityarray[2] = {"newyork", 40.7143530, -74.0059730}; // cityarray[3] = {"boston", 42.3584310, -71.0597730}; // cityarray[4] = {"killington", 43.6775680, -72.7798250}; // cityarray[5] = {"springfield", 42.1014830, -72.5898110}; // cityarray[6] = {"bridgeport", 41.1865480, -73.1951770}; // cityarray[7] = {"cambridge", 42.3736160, -71.1097340}; // cityarray[8] = {"norwalk", 41.1175970, -73.4078970}; // cityarray[9] = {"quincy", 42.2528770, -71.0022710}; //calculate distance cout << "distance between providence , cranston: " << distance(41.8239890, -71.4128340, 41.7798230, -71.4372800, 'm') << endl; cout << "distance between newyork , boston: " << distance(40.7143530, -74.0059730, 42.3584310, -71.0597730, 'm') << endl; cout << "distance between killington , norwalk: " << distance(43.6775680, -72.7798250, 41.1175970, -73.4078970, 'm') << endl; //print distance using city name cout << endl; cout << "distance between providence , cranston: " << citydistance(providence, cranston) << endl; cout << "distance between newyork , boston: " << citydistance(newyork, boston) << endl; cout << "distance between killington , norwalk: " << citydistance(killington, norwalk) << endl; //end return 0; } city::city(string n, double lt, double lg) { name = n; latitude = lt; longitude = lg; } double deg2rad(double deg) { return (deg * pi / 180); } double rad2deg(double rad) { return (rad * 180 / pi); } double distance(double lat1, double lon1, double lat2, double lon2, char unit) { double theta, dist; theta = lon1 - lon2; dist = sin(deg2rad(lat1)) * sin(deg2rad(lat2)) + cos(deg2rad(lat1)) * cos(deg2rad(lat2)) * cos(deg2rad(theta)); dist = acos(dist); dist = rad2deg(dist); dist = dist * 60 * 1.1515; switch(unit) { case 'm': break; case 'k': dist = dist * 1.609344; break; case 'n': dist = dist * 0.8684; break; } return(dist); } double citydistance(city a, city b) { return distance(a.latitude, a.longitude, b.latitude, b.longitude, 'm'); }
this great example of when use std::vector
or std::map
. in particular case, think std::map
better choice (since wouldn't have iterate entire list find 2 cities looking for):
std::map<std::string, city> cities; cities["providence"] = city("providence", 41.8239890, -71.4128340); // ...
Comments
Post a Comment