473,386 Members | 1,715 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Please help program to find distance btwn cities using array of structs

Hi I am newer to programming and need a bit of help with this program.



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)*s in(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..
Jul 30 '07 #1
1 4742
ilikepython
844 Expert 512MB
Hi I am newer to programming and need a bit of help with this program.



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)*s in(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..
So by array of structs I think they means something like this:
Expand|Select|Wrap|Line Numbers
  1. struct City
  2. {
  3.     char cityname[30]; // "Ankara"
  4.     char country[30]; // "Turkey"
  5.     double latitude; // 40.03
  6.     double longitude; // 32.9
  7. };
  8.  
Then you would, read the information in from a file, and store it in an array of structs "City":
Expand|Select|Wrap|Line Numbers
  1. City cities[20];   // declare array of 20 "cities"
  2.  
  3. open file
  4. read file with loop
  5.     store results in cities[x]
  6.     increment x   // or whatever your variable will be
  7.  
Now that you have your information, you just need to make the loop of the program where you will ge the two locations, and calculate the distance. Do you have any problems with that?
Jul 30 '07 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

35
by: wired | last post by:
Hi, I've just taught myself C++, so I haven't learnt much about style or the like from any single source, and I'm quite styleless as a result. But at the same time, I really want nice code and I...
13
by: Chiller | last post by:
I'm now getting close to finishing my Distance class. In the code below I have included a number of overload operators that test for equality etc. I've also added more code in the TEST_DISTANCE...
39
by: gtippery | last post by:
Newbie-ish questions - I've been away from C for a _long_ time. It seems to me that there ought to be easier (or at least shorter) ways to do what this does. It does compile & run for me (with...
2
by: tukriss | last post by:
please give me the answer to this question The distance between two cities in killometers is input through a keyboard. Write a C program to convert and print this distance in; 1.Meters 2.Feets...
3
by: Starbuck01 | last post by:
I have to write a program for my AP Computer Science Class. Here is the instructions. The Police Department is asking for help in catching those who speed. You will write a batch style program...
2
by: cmrhema | last post by:
Hi, My client will input two values say A and B. Here A and B represent cities. The source is A and the Destination is B Now i have to find the shortest route for these two cities One is using...
11
by: devnew | last post by:
hello while trying to write a function that processes some numpy arrays and calculate euclidean distance ,i ended up with this code (though i used numpy ,i believe my problem has more to do with...
3
by: jcl43 | last post by:
so after that part, I have to make a function that returns a list of total distances between each of the cities in the cities list (a list of pair-lists) and I have to have it so the function...
2
by: alireza6485 | last post by:
Hi, Could you please rewrite the program for me?I tried my best and the program still does not do what it has to do. I have to write a code that generates random speed and distance .it ask the...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.