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?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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
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
Build an E-Commerce Site with Angular 5

Learn how to build an E-Commerce site with Angular 5, a JavaScript framework used by developers to build web, desktop, and mobile applications.

JedidiaAuthor Commented:
Couldn't I do something lie the below

if (arrBayWaveInfo[i, 1] <= intFinalWave) && (arrBayWaveInfo[i, 2] > intFinalWave));
0
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
JedidiaAuthor Commented:
Works great.  Thanks!!
0
Fernando SotoRetiredCommented:
Not a problem, glad I was able to help.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C#

From novice to tech pro — start learning today.