Thread: Advice on storing and recalling information (distances between cities)

  1. #1
    Registered User
    Join Date
    Jan 2007
    Posts
    14

    Lightbulb Advice on storing and recalling information (distances between cities)

    I am writing a program that takes information from a .txt file. On each line of the file are the names of two cities and the distance between them (in miles). I want the user to be able to type in a city of departure and a destination, and have the program find the shortest route and the distance.

    e.g. if the text file reads:
    Code:
    New York     Orlando     932
    California     Orlando     740
    and the user types in:
    Code:
    City of Departure: California
    Destination: New York
    I want the program to find a path between the two cities. However, to do this I will need to store some information for each city and look it up. I originally planned to use this structure:
    Code:
    e.g. for Orlando...
    
       char dep[] = Orlando;
       char dest1[] = California;
       int dist1 = 740;
       char dest2[] = New York;
       int dist2 = 932;
    But this means that to look up a city I would need to compare the city name (string) the user types in, with every single 'char dep[]' string. This could take a long time to look up If I have a large amount of cities in the .txt file.
    A friend suggested I use a hash table to store the information, but after looking into this it seems complicated to use to look up words (I'm still a beginner with C).
    Can anyone suggest any other methods to me?
    Or even a simplified explanation of how hash tables work would be great

  2. #2
    Registered User
    Join Date
    Jan 2007
    Posts
    40
    I believe that you can store input from cin into a char array

    Code:
    char* input = new char[MAX_SIZE];
    input = &cin;
    Maybe? Maybe not. Anyways, I say that you might want to use a different data structure. Instead of storing random departures and destinations and doing unnecessary comparisons, you could store all the departures and their data in separate text files. EX:

    orlando.txt
    Code:
    New York    439
    Miami     12
    Denver    1132
    Detroit    493
    detroit.txt
    Code:
    Orlando 493
    Denver   240
    Miami     564
    Then you can also sort the cities in each txt file so that you can apply a faster search algorithm to your application.

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,662
    Each city consists of
    - the name of the city
    - a list of cities which are neighbours, and the distance to that neighbour

    Note that it is possible that A->B is a different cost that B->A (which this structure will be able to represent).

    See also
    http://en.wikipedia.org/wiki/Graph_(mathematics)

    There are algorithms which work out the minimum cost of traversing such a graph, follow the links to read up on them.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  4. #4
    Registered User
    Join Date
    Jan 2007
    Posts
    14
    thanks guys, was hoping I could keep all the info in one .txt file, but that seems like the sensible thing to do. Gonna look into the algorithms. Had a quick scan through and it looks promising. Thanks for all the help!

Popular pages Recent additions subscribe to a feed