We help IT Professionals succeed at work.

How to Find an Element in an Array

Dovberman
Dovberman asked
on
I have the following multidimensional array:

        arrPreFlopRec[1, 1] = "A";
        arrPreFlopRec[2, 1] = "A";
        arrPreFlopRec[3, 1] = "A";
        arrPreFlopRec[4, 1] = "A";
        arrPreFlopRec[5, 1] = "A";
        arrPreFlopRec[6, 1] = "A";
        arrPreFlopRec[7, 1] = "A";
        arrPreFlopRec[8, 1] = "A";
        arrPreFlopRec[9, 1] = "B";

        arrPreFlopRec[1, 2] = "AA";
        arrPreFlopRec[2, 2] = "AA";
        arrPreFlopRec[3, 2] = "KK";
        arrPreFlopRec[4, 2] = "QQ";
        arrPreFlopRec[5, 2] = "AKs";
        arrPreFlopRec[6, 2] = "KK";
        arrPreFlopRec[7, 2] = "QQ";
        arrPreFlopRec[8, 2] = "AKs";
        arrPreFlopRec[9, 2] = "JJ";
       
        arrPreFlopRec[1, 3] = "N";
        arrPreFlopRec[2, 3] = "Y";
        arrPreFlopRec[3, 3] = "Y";
        arrPreFlopRec[4, 3] = "Y";
        arrPreFlopRec[5, 3] = "Y";
        arrPreFlopRec[6, 3] = "N";
        arrPreFlopRec[7, 3] = "N";
        arrPreFlopRec[8, 3] = "N";
        arrPreFlopRec[9, 3] = "N";

I need to find the row where  column 2 ="KK" and collumn 3 = "Y"
correct answer is row 3 .(arrPreFlopRec[3, 2] ,arrPreFlopRec[3,3]

 string strCombo = "KK";
 string strRaised = "Y";
 // Find the array elements:
      for (int i=0; i < 10; i++)
         for (int j=0; j < 5; j++)
// I need help with this
   if (arrPreFlopRec[i,j] == strCombo && arrPreFlopRec[i,j] == strRaised )
        {
         // Row found
         // Do Something
        }  
Comment
Watch Question

Freelance programmer / Consultant
Commented:
string strCombo = "KK";
 string strRaised = "Y";

//columns that have to be found
int col1 = 2;
int col2 = 3;

      for (int i=0; i < 10; i++)
      if (arrPreFlopRec[i,col1] == strCombo && arrPreFlopRec[i,col2] == strRaised )
        {
         // Row found
         // Do Something
        }  

Author

Commented:
I do not know which columns need to be found.  I am searching for the column and row numbers that
contain the strings "KK" and "Y" in the same row.

The search probably needs a drill down statement
for (int j=0;j < 4; j++)

I just need to know how to do this.


string strCombo = "KK";
 string strRaised = "Y";
 int match = 0;
      for (int i=0; i < 10; i++) {
         match = 0;
         for (int j=0; j < 5; j++) {
            if (arrPreFlopRec[i,j] == strCombo or arrPreFlopRec[i,j] == strRaised )      
            {
                     match++;
            }
         }
         if match > 1
         {
                 // Row found
               // Do Something
         }  
If you need row and column:
 
string strCombo = "KK";
 string strRaised = "Y";
 int row = -1;
 int col1 = -1;
 int col2 = -1;
      for (int i=0; i < 10; i++) {
         row = i;
         col1 = -1;
         col2 = -1;
         for (int j=0; j < 5; j++) {
            if (arrPreFlopRec[i,j] == strCombo){
              col1 = j;
            }
            if (arrPreFlopRec[i,j] == strRaised )       
            {
              col2 = j;              
            } 
         }
         if (col1 >= 0 and col2 >=0 )
         {
               // Row found
               // Do Something
         }

Open in new window

I hope you can't have more then one appearance of "KK" or "Y" in one row
AndyAinscowFreelance programmer / Consultant
Commented:
string strCombo = "KK";
 string strRaised = "Y";


      for (int i=0; i < 10; i++)
{
  for(int j = 0; j < 5; j++)
  {
      if (arrPreFlopRec[i,j] == strCombo || arrPreFlopRec[i,j] == strRaised )
        {
//found one, now check for the other
for(int k = 0; k < 5; k++)
{
      if (arrPreFlopRec[i,k] == strCombo || arrPreFlopRec[i,k] == strRaised )
  {
    //a second match - the pairing is found
  }
}
        }  
  }
}

Author

Commented:
These may work. I am testing and will reply this afternoon.

Thanks,

Author

Commented:
Thanks for your guidance.

Author

Commented:
Thanks for your guidance.
This is the code I used:    

  string strCombo = "KK";
        string strRaised = "Y";

        int intMatch = 0;
        int intColCtr = 0;
        int intMatchRow1 = 0;
        int intMatchRow2 = 0;
        intMatch = 0;
        string strRaised1 = "";
        string strRaised2 = "";
        string strUnRaisedRec = "";
        string strRaisedRec = "";

        // There are always 2 rows that contain the specified
        // value of strCombo.
        // Find both matches for strCombo
        for (int intRowCtr = 0; intRowCtr < 10; intRowCtr++)
        {
   
            if (arrPreFlopRec[intRowCtr, 2] == strCombo )
            {
                intMatch++;

                if (intMatch >= 0)
                {
                    // Store value of column3
                    strRaised = arrPreFlopRec[intRowCtr, 3];
                   
                    // Store value of column 4
                    if (strRaised == "N")
                    {
                        strUnRaisedRec = arrPreFlopRec[intRowCtr, 4];
                    }

                    if (strRaised == "Y")
                    {
                        strRaisedRec = arrPreFlopRec[intRowCtr, 4];
                    }
                }  //  End if (intMatch >= 0)
            }  // End if (arrPreFlopRec[intRowCtr, 2] == strCombo )
        }  // End of row parsing for (int intRowCtr = 0; intRowCtr < 10; intRowCtr++)