0

So I'm having trouble with arrays, i'm new to c#. This is in the class program;

    enum Stations { FortitudeValley, Central, SouthBank, Toowong, Taringa};
    const int No_OF_TRAINS = 55;
    const int No_OF_STATIONS = 5;
    static int[,] timetable = new int[No_OF_STATIONS, No_OF_TRAINS];

I know how to get the user's input for the departing/arriving stations and their desired time to be at their arrival station. I need to match the user's desired arrival time to a time in the array. My question is how do I do this? How do I do a 'for loop' that goes through each station and how do i look in the array for a value that equals the user's desired arrival time? I'm assuming that the first index is the train stations and the second is the times.

6
  • So, are you having issues with the array or with how to loop over the array? Your question suggest you don't understand the array and your question suggests you don't understand how to perform a for loop. Commented Apr 17, 2014 at 2:42
  • No problems with the array, I just dont know how to do a for loop to search through the array. Sorry If I was confusing. Commented Apr 17, 2014 at 2:47
  • You should update your question and adjust your tags to reflect your actual issue then. Commented Apr 17, 2014 at 2:48
  • Your approach of using arrays here seems to be inappropriate. What do you intend to do? Commented Apr 17, 2014 at 2:50
  • Possible duplicate of stackoverflow.com/questions/8184306/… Commented Apr 17, 2014 at 2:50

1 Answer 1

2

You are using a wrong data structure here. You should take a look at Dictionary. And then use it like this (perhaps)

Dictionary<string, List<DateTime>>

where string will be the station name, and List will contain your departing times.

Furthermore, you can create your own class and store all the attributes of your station in that class. Then you can use that in dictionary or via direct LINQ queries.

Edit:

You need to use nested for loops to loop through the multi-dimensional arrays. Take a look here

Sign up to request clarification or add additional context in comments.

3 Comments

+1. Dictionary may be wrong structure for something that definitely have order (station names presumably are in first-second-third... order)... But indeed looks like a good index helper structure.
@AlexeiLevenkov I agree to what you are saying here. But, from the information that he has given this is all i could infer to get him on track.
Thanks for the reply, but the array was given to me as a problem solving exercise. So the thing I need is how to do a for loop to search through the array and match a user input value. Sorry if I was confusing

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.