C program to calculate distance between two cities




















It is a special case of a more general formula in spherical trigonometry One can then solve for d either by simply applying the inverse haversine if available or by using the arcsine inverse sine function:. I'm personally going to recommend the use of latitude and longitude as mentioned above Well, I would start by writing the "city" struct, including the libraries you are going to use, setting up the function prototype for getting the distance between two cities, writing the function so that it just returns 0.

The results will be inaccurate, of course, but it'll show us the program skeleton. I wouldn't read in the data from the user yet or from a file , just put some comments in the program explaining where you plan on putting stuff. Then post the that code and we can all go from there. Basically just a light skeleton with comments, a function prototype with the function call, even though that function won't really do much, and a simple one line display of the "results". How's that sound?

The A-Star algorithm is a way of getting from point A to point B in an efficient way, or in your case city A to city B. You'd have a bunch of cities, with roads between them. The roads would be, I assume, straight lines well, arcs since it is a globe and you're using the haversine formula. It seems to me that your teacher is assuming that not every pair of cities would have a road in between.

Otherwise you would just go directly from city A to city B and there's no need from an algorithm. It's a graph theory algorithm. You have a bunch of nodes and a bunch of edges connecting the nodes, and each edge has a cost. You're trying to find a good, but not necessarily the best, path from node A to node B. I've never used A-Star, but I have heard it comes up a lot in games with obstacles. There are other graph theory algorithms to get from point A to point B, like Djikstra's Algorithm.

I'm more familiar with that one. Again, I've never used A-Star, so I have no idea how hard it is to implement. So to have any use for A-Star, it would seem to me that you not only have to enter in a bunch of cities, but somehow have a bunch of edges too.

The "cost" of each edge could be the distance, so that would be calculated rather than entered. This can be as elaborate and involved as you want to by randomly generating edges, by putting in obstacles, etc. Regardless, I still think you should tackle the easier problem first, get that working first flawlessly, then add in these other features.

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, learning, and sharing knowledge. You're trying to visit a URL that doesn't currently exist on the web. Most likely, a member posted a link a long time ago to a web page that has since been removed. It's also possible that there was a typo when posting the URL.

We redirect you to this notice instead of stripping out the link to preserve the integrity of the post. Answered by sarehu 84 in a post from 13 Years Ago. Jump to Post. Answered by ithelp in a post from 13 Years Ago.

According to the Pythagoras Theorem,. Do we have to use the -lm parameter if we compile this program in Windows. I am using Codeblocks Editor. You just need to hit the compile and run button in the menu header above.

Thanks for all these 3 methods of finding the distance between two planar points in C Programming Language. I believe the Structures method is better than the above two methods as it represents points and its co — ordinates in a better and easy to understand manner. Necessary cookies are absolutely essential for the website to function properly.

This category only includes cookies that ensures basic functionalities and security features of the website. In case you care about the reason: making an operating system call to write data to a stream usually has a fair amount of overhead, so each call takes a fair amount of time, even to write a small amount of data.

To avoid this, the code in the library stores data in a buffer. It only calls the OS to write the data to the external file once in a while, when the buffer gets full. For interactive input like this, it won't actually hurt much, but as a simple rule of thumb, I'd just avoid using it at all. In the rare cases that you really do want to flush a stream's buffer, use std::flush to do it. You should review your code, after calling it "finished" and remove any unused variable or routines.

Just go back and tidy up a bit. So, I'd remove int ch;. Two reasons: It wastes some memory, and it leads to questions in the reader's mind. Anyone who reads the code will think, "What happened to ch? Do I have all the code? You don't want to create questions in the mind of your reader. You want it to be as crystal clear as you can make it. So, along those lines, you've named your variables x, y, a, b.

Again, you'll create questions in the mind of your reader. Go back to what you're trying to accomplish. You're trying to get the X and Y co-ordinates of two points: Point1 and Point2. After you get some experience, you'll realize that some concepts, like points on the Cartesian plane, lend themselves well to being something we call Objects. It might be overkill for your skill level at this point, but you should look into some examples. The former implements a general exponential and may well be slower than a single multiplication.

This isn't bad for a beginner by no means. However, your code could use some improvements. I've commented on some of the things I think could be improved. Correct me if there are any mistakes or if there's something that needs elaboration:.

Here's an attempt at an improved version using a class to hide data. I haven't tested it but I think it gets the general idea across:. In the source code, you can collapse a lot of the output statements to improve readability and so that they take up less vertical space.

For example,. There is a fair amount of room for art and style in formatting, so don't take any particular suggestion as "you must do it this way" — in fact, I've mixed a couple different styles into the above example.

The goal is to write code for which it is easy to see what the intention is, and without much clutter. You can improve readability further by realizing you don't need the endl , and can simplify to printing newline characters:. Generally speaking, buffering is good for performance, so it's better to write newline characters rather than inserting endl when you don't actually need flushes to happen.

As a bonus, they're less typing and usually easier to read. Thus, the above code snippet demonstrates the general pattern: I use newline characters to write newlines, and I do something to flush the stream at the point I want to ensure that all of the output has been written. Using endl to do the flush isn't an option here, if I want the flush to happen at the right place! Sign up to join this community. The best answers are voted up and rise to the top. Stack Overflow for Teams — Collaborate and share knowledge with a private group.

Create a free Team What is Teams? Learn more.



0コメント

  • 1000 / 1000