C# School Assignment? Project Due Tomorrow? Chat LIVE With A Programming Expert!

Welcome to Dream.In.Code
Become a C# Expert!

Join 309,245 C# Programmers for FREE! Get instant access to thousands of C# experts, tutorials, code snippets, and more! There are 2,635 people online right now. Registration is fast and FREE... Join Now!




Best Data Structure to hold a 'grid' ?

 

Best Data Structure to hold a 'grid' ?

dev101

2 Mar, 2009 - 09:46 AM
Post #1

New D.I.C Head
*

Joined: 2 Mar, 2009
Posts: 4

hi fellow coders, what is the best data structure to store a grid / matrix, with 2 Keys (row, col), and the Values (cell entries) can be any object? Similar to a 2-dimensional array, but i am trying to avoid using arrays.

Specifically i am trying to implement an "Availability Calendar", where i want to show RoomTypes as row headers (variable number), and Weekdays as column headers (fixed, limited to 2 weeks at a time); and the intersection being all information about the particular cell (e.g. booked, available, guest names, etc).

..............1/Mon....2/Tue....3/Wed....14/Sun
+--------+--------+---------+---------+---------+
Type1
+--------+--------+---------+---------+---------+
Type2
+--------+--------+---------+---------+---------+
etc...
+--------+--------+---------+---------+---------+

Thanks in advance.

User is offlineProfile CardPM
+Quote Post


Chepoll

RE: Best Data Structure To Hold A 'grid' ?

2 Mar, 2009 - 10:48 AM
Post #2

D.I.C Head
**

Joined: 27 Feb, 2009
Posts: 67



Thanked: 5 times
My Contributions
well, you could use one dimensional array or list.
imagine one list, that was created by adding a row to the end of previous row

image in the content 2D-list being like following :
CODE

[A0][B0][C0][D0]
[A1][B1][C1][D1]
[A2][B2][C2][D2]

1d form would be like :
CODE

[A0][B0][C0][D0][A1][B1][C1][D1][A2][B2][C2][D2]

and, you would reach to an item at rowNo,colNo by
CODE

int noOfColumns = 4;
Object itemAtPosition = List[(rowNo-1)*noOfColumns + (colNo-1)];

as an example, "A1" is row:2, col:1, while in 2d format.
put those in the calculation of index for "itemAtPosition" above, and you'll get 4...

does this work for you?
User is offlineProfile CardPM
+Quote Post

dev101

RE: Best Data Structure To Hold A 'grid' ?

2 Mar, 2009 - 06:04 PM
Post #3

New D.I.C Head
*

Joined: 2 Mar, 2009
Posts: 4

Thanks for your reply Chepoll. Your suggestion may work, but

QUOTE(Chepoll @ 2 Mar, 2009 - 10:48 AM) *

well, you could use one dimensional array or list.

I may as well use a two-dim array since it's logically easier to imagine.

I was trying to avoid arrays altogether, since its indexes are integers and cumbersome to deal with. e.g. rather than use ArrayOfObjects[x,y] , i wanted something more OO and strongly-typed such as AvailabilityCal<RoomTypes,Weekdays>, maybe i have to write my own object, but wasn't sure how to approach it.

User is offlineProfile CardPM
+Quote Post

Footsie

RE: Best Data Structure To Hold A 'grid' ?

2 Mar, 2009 - 11:23 PM
Post #4

D.I.C Regular
Group Icon

Joined: 20 Sep, 2007
Posts: 348



Thanked: 9 times
Dream Kudos: 50
My Contributions
I'm not sure if I understand exactly what you're trying to do here, but if you are trying to store a 'grid' as you put it, my feeling is to use a DataSet. Have you tried that? Would that not work?
User is offlineProfile CardPM
+Quote Post

Chepoll

RE: Best Data Structure To Hold A 'grid' ?

3 Mar, 2009 - 12:05 AM
Post #5

D.I.C Head
**

Joined: 27 Feb, 2009
Posts: 67



Thanked: 5 times
My Contributions
I said "List" as well...

CODE

using System.Collections.Generics;

List<Object> litsOfThings = new List<Object()>;

User is offlineProfile CardPM
+Quote Post

dev101

RE: Best Data Structure To Hold A 'grid' ?

3 Mar, 2009 - 10:21 PM
Post #6

New D.I.C Head
*

Joined: 2 Mar, 2009
Posts: 4

QUOTE(Footsie @ 2 Mar, 2009 - 11:23 PM) *

my feeling is to use a DataSet. Have you tried that? Would that not work?

It would probably work, but i was trying to use something more clever and easier to manipulate, since DataSets are not strongly typed (or that is my limited understanding of them).

QUOTE(Chepoll @ 3 Mar, 2009 - 12:05 AM) *

I said "List" as well...


ok, thanks guys, i'll keep working on it.
User is offlineProfile CardPM
+Quote Post

baavgai

RE: Best Data Structure To Hold A 'grid' ?

4 Mar, 2009 - 04:25 AM
Post #7

Dreaming Coder
Group Icon

Joined: 16 Oct, 2007
Posts: 4,374



Thanked: 418 times
Dream Kudos: 550
Expert In: C, C++, Java, C#, ASP.NET, PHP, Perl, Python, Oracle, SQL Server, MySql, HTML, JavaScript, Lua, Cheese

My Contributions
It's sounds like you have a hotel? You may be working backwards.

Figure out how to represent a room, a physical place. The room has the RoomType. The room as keeps track of it's bookings. So, you need a reservation type. The reservation type should simply be a time span with a guest list. The site is made up of rooms.

To print your calendar, you loop through all the rooms for a given day and ask them if they're free. Or have a list of reservations that references the rooms and query that.

There are many ways to do this, but starting with final presentation of the data may not be the best approach.

User is offlineProfile CardPM
+Quote Post

dev101

RE: Best Data Structure To Hold A 'grid' ?

4 Mar, 2009 - 05:18 PM
Post #8

New D.I.C Head
*

Joined: 2 Mar, 2009
Posts: 4

QUOTE(baavgai @ 4 Mar, 2009 - 04:25 AM) *

It's sounds like you have a hotel? ... but starting with final presentation of the data may not be the best approach.

Yes a hotel, and agreed with your point.

But i don't think i can loop through rooms. Due to presentation being as per the matrix above, i should be looping thru roomTypes (for each row) and then Weekdays (left to right) and show availabilty this way.

Thanks for your input, cheers.
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic

Time is now: 11/26/09 09:40AM

Live C# Help!

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter Fan Us On Facebook

C# Tutorials

Reference Sheets

C# Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month