Thread: Trip Calculator Project

  1. #1
    Registered User
    Join Date
    Oct 2009
    Posts
    15

    Trip Calculator Project

    Hi,

    I'm working on a CS project, and I'm somewhat stuck on my project. I was wondering if somebody could offer some suggestions or hints to get me moving again.

    The project description is as follows. I am given a 12 x 12 array that holds the distances between different cities. I will call this my distance array. For instance, the array entry [2][3] may list the distance between Atlanta, GA and Los Angeles, CA. I also have a second array listing all the cities as well. I will call this my cities array.

    Now I am supposed to create a program that displays the cities to the user. The user is then supposed to select a city, or the exit (EOF) option, and the program is supposed to calculate the distance between the selected city and the previous city selected. Once that is done, I am supposed to prompt the user for another selection. The user can either select another city or simply exit the program altogether.

    I haven't written any code for this since I'm still confused about two details. Before I explain my problems, here is a brief outline of how I think the program should look.

    Code:
    While ( selection != EOF) {
    
    Print all the menu options, ie. my cities array.
    
    Input a menu option from the user.
    
    Calculate distance and display total distance to the user.
    
    }
    I know this is a very very short outline, but that is the gist of the program should work. Now my two problems are the following:

    1 . How am I supposed to take an input from the user? scanf or getchar? The problem I have is that I need the input to tell the program "Go to this array element in the distance array".

    2. I am having problems figuring out how to compute the total distance. Now, in the first selection, the total distance must be zero since the user has not traveled anywhere. I will initialize Total Distance to be zero. However, in the second selection the total distance = previous total + distance [ current selection][previous selection]

    My real problem is that I cannot figure out a way to tell the program to go to the appropriate array element. I need the array element to be the one selected that holds the distance between the current selection and previous selection. I don't know how to write this out in C. Any thoughts would be appreciated.

    BTW, on second thought I think my 1st question is ill-posed. So if people cannot make sense of it, then it's okay to forget about it.

  2. #2
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    It might be overkill for your purpose, but here's one way:

    You have your cities array, but now instead of just a city name, it's a struct array and has the cities name and a unique number (like an ID for a student).

    The distance array has entries like:
    Code:
    ID Number Athens GA ID #  Boston MA #  Chicago IL #  Denver CO #
    ===============================================================
    0              0          860           978           1450
    1            860            0          1726           2100
    
    etc.
    Where 0 is the ID number for Athens, GA, 1 is the number for Boston MA,
    2 is the ID # for Chicago, ILL, and 4 for Denver CO.

    The header info (======= and above), would not be in the array.

    Now you can use this data, just like a scorecard for a sports tournament. Everything can be easily found because of the association of each city name, with an ID #, and that number, with the index to that # within the distance data.


    If you don't see this, search for my name and "scorecard", and you can see a more detailed description and example code.
    Last edited by Adak; 11-15-2009 at 01:46 PM.

  3. #3
    Registered User
    Join Date
    Oct 2009
    Posts
    15
    Thanks. Unfortunately, I am really not allowed to deviate from the set up my professor has instructed us to follow. My problem is really revolves around #2, figuring out the total function.

    I essentially need to write the total function to be:

    total = previous total + distance [current selection][ previous selection]

    I need to figure out a way to store the previous selection for each iteration of the while loop, and I think I need to create some sort of "temporary" variable that will store the previous value, but, in the next iteration, write over it with the other selection. Okay, this is what I mean. Suppose, the user selects city 1, then city 4, and then city 6.

    The total distance in the first iteration is from city 1 to itself, so zero.

    In the 2nd run of the loop, the user selects city 4, then the total = previous total + distance[city 4][city 1]

    In the 3rd run, the user selects city 6, so the total = previous total + distance[city 6][city 4].

    I guess some sort of temp variable is required. Hmm...


    Thanks anyways for your help.

  4. #4
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    All you need is just:

    Code:
    totalDistance += tripDistance;
    That's the same as: totalDistance = totalDistance + tripDistance

  5. #5
    Registered User
    Join Date
    Oct 2009
    Posts
    15
    Quote Originally Posted by Adak View Post
    All you need is just:

    Code:
    totalDistance += tripDistance;
    That's the same as: totalDistance = totalDistance + tripDistance
    Heh. I agree with you. We're saying the same thing. The problem is that when I want to narrow down what tripDistance is supposed to b; it is a particular array element I want to call. Specifically, I want to call: distance [ currently selected city][previously selected city], but I don't know how I would go about doing that.

  6. #6
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,834
    Just keep a variable such as int curr_city - where the user's input will be stored (after looking up the city index in a name array).

    For subsequent queries, old_city = curr_city and then get a new city.

    Access the distance array using distance_array[old_city][curr_city]

    I'm not understanding how this is a major problem.

  7. #7
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Quote Originally Posted by littlefermat245 View Post
    Heh. I agree with you. We're saying the same thing. The problem is that when I want to narrow down what tripDistance is supposed to b; it is a particular array element I want to call. Specifically, I want to call: distance [ currently selected city][previously selected city], but I don't know how I would go about doing that.
    I explained how to do it in the #2 post in your thread. <sigh>

    It's more difficult sounding, than it really is. It works exactly the same way as a tournament scoreboard for games.

    If you search this forum for my name and "scorecard" or "scoreboard", there's a complete program and a thread to describe it way beyond mere program comments.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. HELP with Program!
    By afnitti in forum C Programming
    Replies: 9
    Last Post: 04-15-2009, 08:06 PM
  2. Road Trip Tips
    By B0bDole in forum A Brief History of Cprogramming.com
    Replies: 48
    Last Post: 10-17-2005, 11:52 PM
  3. my trip to california
    By Silvercord in forum A Brief History of Cprogramming.com
    Replies: 7
    Last Post: 10-06-2003, 03:43 PM
  4. easy if you know how to use functions...
    By Unregistered in forum C Programming
    Replies: 7
    Last Post: 01-31-2002, 07:34 AM