Solved

need to search a multidimensional array in c#

Posted on 2004-10-15
5
509 Views
Last Modified: 2012-06-21
HI,

I'm having a hard time figuring out how to use a multidemensional array. Basically a deck of cards.
Here is the Deck:
--------------------------------------
int[,] cards = new int[52,2]
            {
                  //11 = jack
                  //12 = queen
                  //13 = king
                  //14 = Ace

                  //1 = clubs
                  //2 = diamonds
                  //3 = hearts
                  //4 = spades

                  {2,1},{2,2},{2,3},{2,4},
                  {3,1},{3,2},{3,3},{3,4},
                  {4,1},{4,2},{4,3},{4,4},
                  {5,1},{5,2},{5,3},{5,4},
                  {6,1},{6,2},{6,3},{6,4},
                  {7,1},{7,2},{7,3},{7,4},
                  {8,1},{8,2},{8,3},{8,4},
                  {9,1},{9,2},{9,3},{9,4},
                  {10,1},{10,2},{10,3},{10,4},
                  {11,1},{11,2},{11,3},{11,4},
                  {12,1},{12,2},{12,3},{12,4},
                  {13,1},{13,2},{13,3},{13,4},
                  {14,1},{14,2},{14,3},{14,4},
            };
------------------
Am I using the right array, or should I use a jagged array?
I'm having a hard time searching the array.
For example, how would I search for all the objects with 4 as the first number in the array?
{4,1}, {4,2}, {4,3}, {4,4}?
0
Comment
Question by:unomateo
  • 2
5 Comments
 
LVL 1

Expert Comment

by:RogueAce
ID: 12325168
With a simple unsorted array, the only reliable way to search it is sequentially. For example:

for(uint c=0,c<52,c++){
if(cards[c,0]==4){
//Put code here to deal with 4s
}
}

The [] operator indexes into the array. Keeping in mind that the array is 0-based, cards[3,0] will be 2 and cards[3,1] will be 4, because the 3rd row of the cards array is {2,4}.

If you want more efficient searching you could sort using some strategy and do a binary search on the array.

Hope this helps.
0
 

Author Comment

by:unomateo
ID: 12325441
Thanks

I think I am really confused about how to do this.
When I list the full array, I have 104 items. I think I need 52 items.
I though that each item in the array would be in this format: (2, 1)(2, 2), but actually the array lists like this: 2, 1, 2, 2...

What I need to do is make a deck of cards, that does not need to be shuffled. I want to pull a card out, for example 2 of clubs...then count to see how many more 2's are left, and how many more clubs are left. The code you gave me works, but I think my array is wrong to start off with?
Should I use a single-dimension array, and just parse it using the binary search you mentioned earlier?
0
 
LVL 1

Accepted Solution

by:
RogueAce earned 250 total points
ID: 12325613
Here's how I would do it if I were you:

//this is outside your function in your class
enum cardSuit {Clubs, Hearts, Diamonds, Spades};

struct Card {
  cardSuit Suit;
  uint Value;}

//this is in your function
Card[] cards = new Card[52];

c=0;
Card aCard=new Card;
for(cardSuit s=Clubs;s<=Spades;s++){
  for(uint v=2;v<=14;v++){
    aCard.Suit=s;
    aCard.Value=v;
    cards[c]=cCard;}}

Let's say using this approach you want to find all the 4s. Adapting the code I gave you earlier:
for(uint c=0,c<52,c++){
  if(cards[c].Value==4){
    //Put code here to deal with 4s
  }
}

If you don't understand structs and enums, this can be confusing, but I think it will ultimately work the best for you.

Good luck!
0

Featured Post

NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
New to SOAP and need some direction 10 80
Filename to be appended with DateTimeof Download 3 55
Printing 1 51
Create a WebAPI using a generic return parameter 9 25
Extention Methods in C# 3.0 by Ivo Stoykov C# 3.0 offers extension methods. They allow extending existing classes without changing the class's source code or relying on inheritance. These are static methods invoked as instance method. This…
This article describes a simple method to resize a control at runtime.  It includes ready-to-use source code and a complete sample demonstration application.  We'll also talk about C# Extension Methods. Introduction In one of my applications…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question