views:

264

answers:

3
+4  Q: 

C# Array Maximum

  • I have 2 arrays named Arr1 and Arr2 in C#. They are of the exact same dimensions...

I need to get the element of Arr1 corresponding to maximum of elements in Arr2 beginning with given indices ...

e.g
Get indices of the max of Arr2 [ 1 , 10 , 3 , i , j ] for all i,j
Return Arr1 [ 1 , 10 , 3 , i , j ]

Of course I need the elegant solution (not the "loop for them" one...)

Please Note:
I do not want to loop through the arrays, because it is 11 dimensional!!.. the code will be ugly and error prone.. and I may run out of variable names :)

EDIT:
The normal solution would be:

for(int i=0;i<10;i++)
    for(int j=0;j<10;j++)
       if(Arr2[1,10,3,maxi,maxj]<Arr2[1,10,3,i,j])
       {
         maxi=i
         maxj=j
       }
 return Arr1[1,10,3,maxi,maxj];

But I need to do it in less and more beautiful code.. may be using querys or linq..

A: 

I know this will not really help you, but you can not use LINQ in this case, because multidimensional Arrays are not implementing the IEnumerable Interface.

So do not waste your time to solve your problem with LINQ :)

spookycoder
A: 

How can you not iterate over all elements when you need to calculate the maximum value?

Imo it would make your life easier if you placed your data in a more accessible data structure, at least something supporting IEnumerable<T>.

Peter Lillevold
+1  A: 

Assuming that you have to have an 11-dimensional array (doing some M-theory work?) and that no other data structure is possible, and given that you don't want to use looping to find the maximum value in the array, that leaves you with just one possibility that I can think of:

Create a new ManagedArray class with two private fields, one for values and one for maximums.
Create an indexer with 11 index parameters. The get is as simple as anything with 11 parameters can be:

        public int this[int index1, int index2, int index3, int index4, int index5, int index6, int index7, int index8, int index9, int index10, int index11]
        {
            get
            {
                return (int) values.GetValue( index1, index2, index3, index4, index5, index6, index7, index8, index9, index10, index11 );
            }

The set will call SetValue and then update the current maximums with the new value if the new value is bigger than the existing maximum for that set of dimensions.
Create a Maximum property that takes a bunch of index values, have it return the current value of maximums at those index values. Now change Arr1 and Arr2 to be instances of the ManagedArray class.

What we've done is to trade a loop at request time (when you want to know the maximum) with a slightly more time-consuming array setter that tracks maximum values so we can look them up in constant time whenever requested.

AFAIK, that's the only way you can do it.

Note that you can make it a lot easier to read by using the params keyword, but then the setter will be a little more complicated when you're updating the maximums. Up to you.

Task
+1 for M-Theory work
vitorbal