Need help with storing data and variables

Started by
16 comments, last by Antheus 16 years, 1 month ago
Okay thats fine, minus the fact my airports and names and not numbers. That would mean a mix of names and a mix of distances, numbers and strings. So again how would I implement this?
Advertisement
Quote:Original post by MadHaTr
Okay thats fine, minus the fact my airports and names and not numbers. That would mean a mix of names and a mix of distances, numbers and strings. So again how would I implement this?


Make a second array (1D) that maps numbers to names. So town 0 is chicago, town 1 is winnipeg, etc...
Then you can use these numbers to look at the data in the 2D distances array.

std::string names[3] = {"chicago", "winnipeg", "miami"}int distances[3][3] = {{  0, 400, 400},{400,   0, 400},{400, 400,   0} }
Quote:Original post by streamer
Make two dimension array with x,y coordinate for airports (longitude, latitude?) and then with Pythagorean Theorem just calculate the distance.

float distance = sqrt( xdist*xdist + ydist*ydist );


In geographical terms, phythagoras will not work. The Earth is not flat, it's not even a sphere. At best a (3D) application of pythagoras would involve tunneling some ditance through the Earth. If you were to calculate the distance from New York to Sydney with this appraoch, your calculated distance would be some margin out.

Depending on application, you may also have to take into account air corridors, terrain and such.
I would recommend storing coordinates instead of distances. You can get the distance from airport to airport through calculation, and get a result in say nautical miles.
This way you will now exactly where Airport A is in relation to Airport B.
Otherwise just a distance won't tell me anything. Ok, I know Airport A is 600 Miles From Airport B, but which direction do I need to go to get there. Traveling 600 Miles at a heading of 90 will definitly put me in a different area than traveling 600 Miles at a heading of 180.
Here is a link with a calculator to calculate the distance between coordinates,
as well as an explanation of the math behind it.

Link

You can also find Latitude/Longitude of airports online.
Here's an example of such a site that provides this information:
Airports

With this information you can just create a list or even use a database that stores your important airport information: Longitude/Latitude, Elevation, Radio Freqs., Etc.

That is just an idea as to how I would possibly implement such a system.

Okay thanks for the suggestion, but I don't need to know the direction or headings. This at the moment will be a simple simulation. I just need distances for fuel calc and maybe other. At a later point I may decide to upgrade and could use that possible then.

Once I get a chance I will try to make the 2 seperate arrays. This seems to be the best way. Or I could possible use one array and still use a numbering convention for the airports.

Thanks.
I have no idea about C# and I guess the C++ standard library would only confuse you, so here's some (dirty ol') C code:

#include <stdio.h>#define NUM_CITIES    4/*         chicago miami detroit winnipeg * chicago     0    400     300     600 * miami     400      0     500     200 * detroit   300    500       0     100 * winnipeg  600    200     100       0 */const char *citynames[NUM_CITIES] = {	"Chicago",	"Miami",	"Detroit",	"Winnipeg"};int matrix[NUM_CITIES][NUM_CITIES] = {	{  0, 400, 300, 600},	{400,   0, 500, 200},	{300, 500,   0, 100},	{600, 200, 100,   0}};int main(int argc, char **argv) {	int  i;	int  startindex;	int  destindex;	puts("This program checks the distance between two cities.");	puts("The following cities are available:");	for(i = 0; i < NUM_CITIES; ++i)		printf("%d: %s\n", i, citynames);	while(1) {		printf("Number of the city you depart at: ");		if((scanf("%d", &startindex) == 1) && (startindex >= 0) && (startindex < NUM_CITIES))			break;	}	while(1) {		printf("Number of the city you arrive at: ");		if((scanf("%d", &destindex) == 1) && (destindex >= 0) && (destindex < NUM_CITIES))			break;	}	printf("The distance between %s and %s is %d miles.\n",		citynames[startindex], citynames[destindex],		matrix[startindex][destindex]);	return 0;}
That is perfect, at least I think so anyways. Thanks.

I'll turn that to c# and progress can continue.
Quote:Original post by OldProgie2
Quote:Original post by streamer
Make two dimension array with x,y coordinate for airports (longitude, latitude?) and then with Pythagorean Theorem just calculate the distance.

float distance = sqrt( xdist*xdist + ydist*ydist );


In geographical terms, phythagoras will not work. The Earth is not flat, it's not even a sphere.


It's spherical enough for Great Circle Distance to be applicable to air flight.

This topic is closed to new replies.

Advertisement