• 14
• 12
• 9
• 10
• 13

# Need help with storing data and variables

This topic is 3661 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}