473,569 Members | 2,836 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

1 New Member
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=degree s * pi / 180)). PI must be set set to 20 decimals as follows: PI = 3.1419265358979 323846 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(lat 2)*sin(long2)+s in(lat1)*sin(la t2))*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 4756
ilikepython
844 Recognized Expert Contributor
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=degree s * pi / 180)). PI must be set set to 20 decimals as follows: PI = 3.1419265358979 323846 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(lat 2)*sin(long2)+s in(lat1)*sin(la t2))*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
4514
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 go to great lengths to restructure my code just to look concise and make it more manageable. When I say this, I'm also referring to the way I...
13
1587
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 driver to test the code. I now have 2 remaining problems: 1. The code will allow for the input of distances in either cm, m or km values;...
39
2572
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 PowerC, a 16-bit DOS compiler); if there are nonstandard or "accidentally-works" aspects, please let me know. {This is the sort of situation where...
2
1859
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.Inchs 4.Centimeters thank you.
3
2263
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 (read data from a file) where the first set of data includes the distance in feet of an area marked off on a parkway and the speed limit (miles per hour)...
2
1320
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 Dijktra's algorithm. But there one has two provide the other values too. eg He starts from A and reaches to B, via A1,A2,A3, B1,B2,B3 we get all...
11
6360
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 python coding style..so am posting it here) .... # i am using these numpy.ndarrays to do the calculation facespace # of...
3
6113
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 calculates the total distances between each city and all the other cities. The index of the results list should correspond to the index in the cities list so...
2
2884
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 user for angle and start calculating the vertical and horizantal positions. when the vertical position gets negative program should stop and check...
0
7695
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7612
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
8119
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
0
7964
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6281
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
0
5218
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
1
2111
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1209
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
936
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.