# Need help with storing data and variables

This topic is 3566 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

I have a bunch of airports, they are all x distance from each other. I need some way to store them and the distance they are apart from one another chicago - winnipeg 200miles. chicago - miami 400miles miami - detroit 400miles and so on, there will be many airports. so there will be many distances to know. So its seems comlicated to me, though I imagine its not. I thought of an array at first with the cities lining the top and the side chi mia det chi 0 400 400 mia 400 0 400 det x 400 0 though I don't know if thats possible using names and than numbers like a spreadsheet. thanks

##### Share on other sites
Hi

This is the common problem of representing a graph. The airports are the nodes of your graph and the "connections" between them are the edges. The distances between airports would be the values of the edges.

On the representation of graphs, from Wikipedia:
Quote:
 Two main data structures for the representation of graphs are used in practice. The first is called an adjacency list, and is implemented by representing each node as a data structure that contains a list of all adjacent nodes. The second is an adjacency matrix, in which the rows and columns of a two-dimensional array represent source and destination vertices and entries in the graph indicate whether an edge exists between the vertices. Adjacency lists are preferred for sparse graphs; otherwise, an adjacency matrix is a good choice. Finally, for very large graphs with some regularity in the placement of edges, a symbolic graph is a possible choice of representation.

The adjacency matrix is basically what you came up with. It is also the more suitable of the two for your particular problem, as I imagine you want to store distances between most (if not all) of the airports.

Now if you need help with implementation specifics, you will have to give some more info about what programming language you're using.

##### Share on other sites
Maybe consider using a SQL table to store location values to make it easier?

Like the other poster said, depends on what language you are using..

##### Share on other sites
I am using C# with xna, I could use some help implementing.

Thanks for the fast help

##### Share on other sites
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 );

##### Share on other sites
I can't calculate distance. I don't have accurate locations for them on my map. Thus i need the above mentioned way to store pre determined distances.

Thanks though I appreciate all the offered help.

##### Share on other sites
Okay I still need help implementing this. I'm using C# and XNA.

As stated before, i'm storing the Distance(real world) between Airports.

Now I've looked up Adjacency Matrix and it appears to be a great way to store my data.

- - Chi - Bos - Mia - Was - Van

Chi -o- - 200 - 500 - 300 - 400

Bos 200 - -0- - 500 - 200 - 800

Mia 500 - 501 - -0- - 525 - 1000 -------Made up numbers

Was 300 - 200 - 525 - -0- - 850

Van 400 - 800 - 1000- 850 - -0

I saw some examples of the matrix and they used 1's to make the connections and 0's where their was no connection.

Since I am using actual distances and not 1's will this cause a problem?

Could someone also show me how to turn this into C# code? I had some trouble finding some coded examples.

##### Share on other sites
Quote:
 Original post by MadHaTrI saw some examples of the matrix and they used 1's to make the connections and 0's where their was no connection.

That is the notation used with certain types of graphs. 1 means edge is present, 0 means there's no edge.

Quote:
 Since I am using actual distances and not 1's will this cause a problem?

Depends what you're trying to do with it.

Quote:
 Could someone also show me how to turn this into C# code? I had some trouble finding some coded examples.

Just how much experience do you have, and what are you trying to do with the matrix? To merely store the matrix, you would use rectangular 2D array.

##### Share on other sites
I have a bunch of random experience. Mainly in C++

so a rectangle array? that would be this? array[x][x] (i'm really bad with terminolgy)

Basically I need to be able to grab the distance from airport X to airport y so I can calculate things for the planes. I'm making an airport/plane managment sim.

##### Share on other sites
Quote:
 Original post by MadHaTrso a rectangle array? that would be this? array[x][x] (i'm really bad with terminolgy)

Usually its called a 2-dimensional (2D) array, and is declared like array[rows][columns].

For example, to make a 4 column by 3 row matrix, it would be declared as:
int matrix[3][4] = {// Columns{1, 1, 1, 1}, // Row 1{2, 2, 2, 2}, // Row 2{3, 3, 3, 3}  // Row 3}

##### Share on other sites
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?

##### Share on other sites
Quote:
 Original post by MadHaTrOkay 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} }

##### Share on other sites
Quote:
 Original post by streamerMake 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.

##### Share on other sites
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.

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.

##### Share on other sites
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.

##### Share on other sites
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[i]);	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;}

##### Share on other sites
That is perfect, at least I think so anyways. Thanks.

I'll turn that to c# and progress can continue.

##### Share on other sites
Quote:
Original post by OldProgie2
Quote:
 Original post by streamerMake 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.

##### Share on other sites

This topic is 3566 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Create an account

Register a new account