C# Array search

I have an array and trying to search/return a value based on user input.

              // A two-dimensional array reference.
            int[,] arrBayWaveInfo = new int[15, 3];
            arrBayWaveInfo[0, 0] = 1;
            arrBayWaveInfo[0, 1] = 1;
            arrBayWaveInfo[0, 2] = 12;
            arrBayWaveInfo[1, 0] = 2;
            arrBayWaveInfo[1, 1] = 13;
            arrBayWaveInfo[1, 2] = 30;
            arrBayWaveInfo[2, 0] = 3;
            arrBayWaveInfo[2, 1] = 31;
            arrBayWaveInfo[2, 2] = 46;
            arrBayWaveInfo[3, 0] = 4;
            arrBayWaveInfo[3, 1] = 47;
            arrBayWaveInfo[3, 2] = 64;
            arrBayWaveInfo[4, 0] = 5;
            arrBayWaveInfo[4, 1] = 65;
            arrBayWaveInfo[4, 2] = 80;
            arrBayWaveInfo[5, 0] = 6;
            arrBayWaveInfo[5, 1] = 81;
            arrBayWaveInfo[5, 2] = 98;
            arrBayWaveInfo[6, 0] = 7;
            arrBayWaveInfo[6, 1] = 99;
            arrBayWaveInfo[6, 2] = 116;
            arrBayWaveInfo[7, 0] = 8;
            arrBayWaveInfo[7, 1] = 117;
            arrBayWaveInfo[7, 2] = 132;
            arrBayWaveInfo[8, 0] = 9;
            arrBayWaveInfo[8, 1] = 133;
            arrBayWaveInfo[8, 2] = 150;
            arrBayWaveInfo[9, 0] = 10;
            arrBayWaveInfo[9, 1] = 151;
            arrBayWaveInfo[9, 2] = 166;
            arrBayWaveInfo[10, 0] = 11;
            arrBayWaveInfo[10, 1] = 167;
            arrBayWaveInfo[10, 2] = 184;
            arrBayWaveInfo[11, 0] = 12;
            arrBayWaveInfo[11, 1] = 185;
            arrBayWaveInfo[11, 2] = 200;
            arrBayWaveInfo[12, 0] = 13;
            arrBayWaveInfo[12, 1] = 201;
            arrBayWaveInfo[12, 2] = 218;
            arrBayWaveInfo[13, 0] = 14;
            arrBayWaveInfo[13, 1] = 219;
            arrBayWaveInfo[13, 2] = 236;
            arrBayWaveInfo[14, 0] = 15;
            arrBayWaveInfo[14, 1] = 237;
            arrBayWaveInfo[14, 2] = 240;

            int intFinalWave;
            intFinalWave = Convert.ToInt32(txtFinalWaves.Text);

            int i;
            for (i=0; i < arrBayWaveInfo.GetUpperBound(0); i++)
            {
              // find arrBayWaveInfo[i, 0] where intFinalWave  = arrBayWaveInfo[i, 2] round down .
              

             }

Open in new window


For example.  If the user enters 167 for txtFinalWaves.Text, I want to compare agains arrBayWaveInfo[i, 2] and find the value of arrBayWaveInfo[i, 0].  So the return value would be 11.

Thanks
JedidiaAsked:
Who is Participating?
 
Fernando SotoRetiredCommented:
Hi Jedidia;

Try this code snippet, I think it will do what you need.

int lookingFor = int.Parse("245");
int valueForLookup = -1;
for (int i = 0; i < arrBayWaveInfo.GetLength(0); i++)
{
    // The current value to compare with
    int currentValue = arrBayWaveInfo[i, 2];

    // Edge case beginning of array
    if (i == 0 && currentValue > lookingFor)
    {
        valueForLookup = arrBayWaveInfo[i, 0];
        break;
    }

    // Middle cases
    if (currentValue >= lookingFor)
    {
        valueForLookup = arrBayWaveInfo[i, 0];
        break;
    }

    // Edge case Last element of the array
    if(i == arrBayWaveInfo.GetLength(0) - 1)
    {
        valueForLookup = arrBayWaveInfo[i, 0];
        break;
    }

}


Console.WriteLine("Value = {0}", valueForLookup);

Open in new window

0
 
Fernando SotoRetiredCommented:
You state, "I want to compare agains arrBayWaveInfo[i, 2] and find the value of arrBayWaveInfo[i, 0].  So the return value would be 11", there is no [i, 2] element that is equal to 167. What do you do then?
0
 
Daniel Van Der WerkenIndependent ConsultantCommented:
See if this works:

int iFoundIt = int.MinValue;
for (int i=0; i < arrBayWaveInfo.GetUpperBound(0); i++)
{
  for (int j = 0; j < arrBayWaveInfo.GetUpperBound(1); j++)
  {
	if (arrBayWaveInfo[i,j] == intFinalWave)
	{
		iFoundIt = arrBayWaveInfo[i,0];
	}
  }
}

Open in new window

0
Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
JedidiaAuthor Commented:
@Fernando Soto - In the event they type in a value not in the array, I need to round up to the next closest value of [i, 2] so I can determine the value of [i, 0].
0
 
JedidiaAuthor Commented:
Couldn't I do something lie the below

if (arrBayWaveInfo[i, 1] <= intFinalWave) && (arrBayWaveInfo[i, 2] > intFinalWave));
0
 
JedidiaAuthor Commented:
Works great.  Thanks!!
0
 
Fernando SotoRetiredCommented:
Not a problem, glad I was able to help.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.