| | |
Programming student find distances btwn cities using array of structs
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: May 2007
Posts: 37
Reputation:
Solved Threads: 0
Hi I am newer to programming and need a bit of help with this program.
You all have helped me in the past with my programs but this is a very complicated array of structs program and I don't really understand it very well.
OK, heres the directions.
The distance between two places on earth can be calculated by using their latitudes and longitudes. The calculation for this is as follows: (The latitudes and longitudes must be converted to radians (radians=degrees * pi / 180)). PI must be set set to 20 decimals as follows: PI = 3.1419265358979323846 Earth's Radius = 3963.1
Definitions: acos is arccosine; sin is sine; cos is cosine.
Calculation formula:
miles = acos(cos(lat1)*cos(long1)*cos(lat2)*cos(long2)+cos(lat1)*sin(long1)*cos(lat2)*sin(long2)+sin(lat1)*sin(lat2))*earth radius
The chart shows the latitude and longitude in degrees of 20 various locations around the world. These should be read in from a file, named latlong.txt and for each location hold its name, latitude and longitude.
Write a Program using an array of structs, that will allow the user to choose location 1 and location 2 from a menu and calculate the approximate distance between them.
So here is what the file should look like
Ankara Turkey 40.03000 32.90000
Aukland New Zealand -36.88320 174.75000
Buenos Aires Argentina -34.33320 -58.49990
Calcutta India 22.53330 88.36670
Copenhagen Denmark 55.71670 12.45000
There is a total of 20 locations but here are the first few. When displayed in the menu in the beginning they need to be listed with numbers next to them. So it will output enter starting destination and you type in the number of the starting location and then the number of the ending location. and it will compute the distance using the formula.
I am not familiar with the array of structs so any help would be great
I think the file can contain the city and its coordinates as listed about of can be listed like this:
Ankara Turkey
40.03000
32.90000
I really appreciate all the help..
You all have helped me in the past with my programs but this is a very complicated array of structs program and I don't really understand it very well.
OK, heres the directions.
The distance between two places on earth can be calculated by using their latitudes and longitudes. The calculation for this is as follows: (The latitudes and longitudes must be converted to radians (radians=degrees * pi / 180)). PI must be set set to 20 decimals as follows: PI = 3.1419265358979323846 Earth's Radius = 3963.1
Definitions: acos is arccosine; sin is sine; cos is cosine.
Calculation formula:
miles = acos(cos(lat1)*cos(long1)*cos(lat2)*cos(long2)+cos(lat1)*sin(long1)*cos(lat2)*sin(long2)+sin(lat1)*sin(lat2))*earth radius
The chart shows the latitude and longitude in degrees of 20 various locations around the world. These should be read in from a file, named latlong.txt and for each location hold its name, latitude and longitude.
Write a Program using an array of structs, that will allow the user to choose location 1 and location 2 from a menu and calculate the approximate distance between them.
So here is what the file should look like
Ankara Turkey 40.03000 32.90000
Aukland New Zealand -36.88320 174.75000
Buenos Aires Argentina -34.33320 -58.49990
Calcutta India 22.53330 88.36670
Copenhagen Denmark 55.71670 12.45000
There is a total of 20 locations but here are the first few. When displayed in the menu in the beginning they need to be listed with numbers next to them. So it will output enter starting destination and you type in the number of the starting location and then the number of the ending location. and it will compute the distance using the formula.
I am not familiar with the array of structs so any help would be great
I think the file can contain the city and its coordinates as listed about of can be listed like this:
Ankara Turkey
40.03000
32.90000
I really appreciate all the help..
I guess something along the line of...
struct
string country
double lattitude
double longitude
click_me
struct
string country
double lattitude
double longitude
click_me
Last edited by iamthwee; Jul 30th, 2007 at 5:21 pm.
*Voted best profile in the world*
An array of structs is just like an array of other stuff. Once you index the array you have a single struct and can use it just like normal.
C++ Syntax (Toggle Plain Text)
#include <fstream> #include <iostream> #include <string> using namespace std; struct Location { string name; double latitude; double longitude; }; int main() { Location test[5]; ifstream fin( "locations" ); if ( fin.is_open() ) { int n; for ( n = 0; n < 5 && !fin.eof(); ++n ) { if ( getline( fin, test[n].name ) ) { fin>> test[n].latitide >> test[n].longitude; } } for ( int i = 0; i < n; ++i ) { cout<< test[i].name <<" | (" << test[i].latitude <<", " << test[i].longitude <<")\n"; } } return 0; }
Last edited by Hamrick; Jul 30th, 2007 at 5:27 pm.
The truth does not change according to our ability to stomach it.
•
•
Join Date: Jan 2008
Posts: 11
Reputation:
Solved Threads: 0
Hi! im interested with this post... actually i have a similar problem same as the guy who made this post... i dont understand arrays and structs much.. is there any other way to make this kind of program without using stucts and arrays? using only basic knowledge of c++..i also tried the code here and im not able to run it... also have you made this program work? if yes, can you help me with this kind of program? plz...i need your help..<snipped>
Last edited by happygeek; Jan 15th, 2008 at 7:08 am. Reason: request for email help deleted
![]() |
Similar Threads
- How to find the maximum value in each column of 2D? (C++)
- working with array of structs (C++)
- String declaration in class definition (C)
- Question about putting structs into arrays (C++)
- Please help me with CSocket programming (C++)
- Array limit (C)
Other Threads in the C++ Forum
- Previous Thread: Destructor Function for List structure
- Next Thread: Replacing system("pause"); with a function
| Thread Tools | Search this Thread |
api array arrays beginner binary bitmap c++ c/c++ calculator char class classes code compile compiler console conversion convert count data database delete desktop developer directshow dll download dynamic encryption error file forms fstream function functions game generator getline givemetehcodez google graph gui homeworkhelper iamthwee ifstream input int integer java lib linkedlist linker linux loop looping loops map math matrix memory multiple news node number output parameter pointer problem program programming project proxy python random read recursion recursive return string strings struct temperature template templates test text text-file tree unix url variable vector video visualstudio win32 windows winsock word wordfrequency wxwidgets






