-5

I have 6 devices sending data at intervals of 10 seconds. Receive this data and send to 8 arrays. The first is the index. The second is the hours. The next six, are one for each device. After a predetermined time, the system will stop receiving data. After stopping, I need to get the maximum, minimum and average of Selections of some 6 arrays and populate another array. I need a loop for, how do this? For example. myArray1 [320,18] and myArray2 [8,18]

How do this:

/*    
//myArray2 [0,0] = MAXIMUM (myArray1 from Here [11,0] to Here myArray1 [17,0])
//myArray2 [1,0] = AVERAGE (myArray1 from Here [18,0] to Here myArray1 [24,0])
//myArray2 [2,0] = AVERAGE (myArray1 from Here [25,0] to Here myArray1 [75,0])
//myArray2 [3,0] = AVERAGE (myArray1 from Here [76,0] to Here myArray1 [180,0])
//myArray2 [4,0] = AVERAGE (myArray1 from Here [181,0] to Here myArray1 [320,0])
*/

Regards, ocaccy

soushinsha
  • 83
  • 3
  • 11

2 Answers2

3

I assume you want to max and average only over specific ranges in the multidimensional array. Using extension methods it's a bit complicated, but you can do it like this:

var myArray1 = new double[320, 18];
var myArray2 = new double[8, 18];

int dim2 = myArray1.GetLength(1);
  myArray2[0, 0] =
      myArray1.Cast<double>().Select((val, idx) => new { idx, val }).Where(
        x => x.idx % dim2 == 0 && x.idx / dim2 >= 11 && x.idx / dim2 < 18).Max(x => x.val);
  myArray2[1, 0] =
      myArray1.Cast<double>().Select((val, idx) => new { idx, val }).Where(
        x => x.idx % dim2 == 0 && x.idx / dim2 >= 18 && x.idx / dim2 < 25).Average(x => x.val);
// ...

The downside is that you'll be iterating over all elements all the time. So if this is performance critical, I rather suggest doing it the old fashioned way and in one pass:

myArray2[0, 0] = double.MinValue;
myArray2[1, 0] = 0;
for (int i = 0; i < myArray1.GetLength(0) + 1; i++) {
  if (i >= 11 && i < 18 && myArray1[i, 0] > myArray2[0, 0]) myArray2[0, 0] = myArray1[i, 0];
  if (i >= 18 && i < 25) myArray2[1, 0] += myArray1[i, 0];
  if (i == 25) myArray2[1, 0] /= Math.Abs(25 - 18);
  // ...
}

And a final suggestion: Do not put magic numbers into your code (11, 18, 25, ...), but use consts for this. Later on nobody knows anymore what 25 actually means.

EDIT2: I got the extension method solution to work finally.

Community
  • 1
  • 1
Andreas
  • 6,447
  • 2
  • 34
  • 46
  • Thank you DonAndre. I have string myArray[200,50]; How to convert this string myArray[200,50] to int myArray2[200,50]? Regards, ocaccy – soushinsha Oct 22 '11 at 02:20
  • `int.parse(string)`... you should get a book on programming in C#. Btw, if this was some kind of homework you should add the homework tag. – Andreas Oct 22 '11 at 09:17
2

If you want average of array of numeric values you can use Enumerable's extension method http://msdn.microsoft.com/en-us/library/system.linq.enumerable.average.aspx or just write your simple method that will sum all array items and divide on count.

0x49D1
  • 8,505
  • 11
  • 76
  • 127