-3

I have a multidimentional array like this one with about 3000 rows and 200 columns:

+--+--+--+
|21|23|41|
+--+--+--+
|11|14|16| // 11 is the smalles value in 2nd row
+--+--+--+
|43|35|23|
+--+--+--+

I want to determine the smalles value of the second row. Is there a better / more readable / linq solution? I currently use a for-loop?

My current Approach:

int min = array[0,1];
for (int i= 1; i<len;i++)
{
    if (array[i,1] < min)
    {
        min = array[i,1];
    }
}
fubo
  • 44,811
  • 17
  • 103
  • 137
  • LINQ uses less lines than for-loop, granted. but more readable? may be not true in this case. – kennyzx Dec 01 '14 at 09:12
  • do you use a array? 2d array? List??? Show some code – Vajura Dec 01 '14 at 09:15
  • use a `int[][]` instead of `int[,]` http://stackoverflow.com/questions/597720/what-are-the-differences-between-a-multidimensional-array-and-an-array-of-arrays – Jodrell Dec 01 '14 at 09:48

2 Answers2

0

Let arr be the array and l.u == 1 suggests second row:

arr.Select((t, u) => new { u, t }).Where(l => l.u == 1).FirstOrDefault().t.Min();
Software Engineer
  • 3,906
  • 1
  • 26
  • 35
Taj
  • 1,718
  • 1
  • 12
  • 16
  • int[*,*]' does not contain a definition for 'Select' and no extension method 'Select' accepting a first argument of type 'int[*,*]' could be found (are you missing a using directive or an assembly reference?) – fubo Dec 01 '14 at 09:34
  • `FirstOrDefault` also accepts a predicate, so you can move the predicate from `Where` to `FirstOrDefault` and then remove `Where`. – default Dec 01 '14 at 10:05
0

found a working solution

int[,] array = new int[3, 3] { { 21, 23, 41 }, { 11, 14, 16 }, { 43, 35, 23 } };
int min = Enumerable.Range(0, array.GetLength(1)).Min(i => array[1, i]);
Console.WriteLine(min); // 11
fubo
  • 44,811
  • 17
  • 103
  • 137