• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 759
  • Last Modified:

Array [310,120] Sum, Average, Min, Max in all Array or in a range of cell

Hot to use Sum, Average, Min, Max or any other equation with this Array or a range of cell this Array with C#?

int[,] myArray = new int[310,120];

Thank you..
ocaccy
0
ocaccy
Asked:
ocaccy
  • 4
  • 3
1 Solution
 
TommySzalapskiCommented:
Assuming the array is full of useful data you would just need to do a double for loop to iterate through the entire 2D array.

Since this appears to be academic in nature, I will give you pieces and let you put them together yourself (please no one jump in with a full solution).

Generic double for loop
for(int i = 0; i < height; i++)
  for(int j = 0; j < width; j++)
    dosomething(array[i ][j];

For max and min, just set them to array[0][0] and then check each element if it is higher (or lower)
Sum should be easy, start at 0 and add them all in (hint += operator is great)
Average is just sum/count.

Let me know if you need any more help.
0
 
ocaccyAuthor Commented:
I'm with 6 Laser Particle Counter (LPC) devices connected to a RS485/USB.
I get the data in intervals of 10 seconds and populating the Array with [310.120].
With the data that we receive we are populating the Array of cells,
after entering all data let's make equations as the line below.
This line is excel and is just one sample:

array = ([32,6]-MIN([28,5]:[41,96]))/(MAX([28,5]:[41,96])-MIN([28,5]:[41,96]))
 
How to make this with this Array in C#?

Thank you.
ocaccy
0
 
TommySzalapskiCommented:
So every 10 seconds you refill the array of 310x120 data points?

To get the minimum from 28,5 to 41,96 you would do something like this
double minVal = array[28][5]
for(int i = 28; i <= 41; i++)
  for(int j = 5; j <= 96; j++)
    if(array[i][j] < minVal)
      minVal = array[i][j];
//I would recommend writing it as a function
double minArrayVal(double array[][120], int iStart, int jStart, int iEnd, int jEnd)
{
  double minVal = array[iStart][jStart];
  for(int i = iStart; i <= iEnd; i++)
    for(int j = jStart; j <= jEnd; j++)
      if(array[i][j] < minVal)
        minVal = array[i][j];
  return minVal;
}
//And a similar one for max.

Open in new window


Then you can just do something like
Value = (array[32][6]-minArrayVal(array, 28,5,41,96))/(maxArrayVal(array, 28,5,41,96)-minArrayVal(array, 28,5,41,96));
0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

 
ocaccyAuthor Commented:
UOOWOOU, Tommy.
I spoke in a confused way, forgive me!
The system relies on each device every 10 seconds.
In response, each device returns a string with 6 fields interspersed with commas (,).
This string will then be shared with substring (), stored in variables, sent to the first row of the array and displayed on a label on the screen.
In the next iteration will go to the second line and so on.
That every 10 seconds.

Can I ask more things in this regard within the scope of this communication or should I ask another question?
If I can:
According to the above explained, how to send data to the array every time the data is received from devices?
I'm having problem in the code to invoke the devices.
Can I send you the code for you to analyze it for me?
If I can not disregard the questions and please forgive me.
I run this code and go back.
private void button6_Click(object sender, EventArgs e)
        {
            textBox2.Clear();
            string ID01 = "ID01";
            string ID02 = "ID02";
            string ID03 = "ID03";
            string ID04 = "ID04";
            string ID05 = "ID05";
            string ID06 = "ID06";
           
            CommPort com = CommPort.Instance;

            ID01 = ConvertEscapeSequences(ID01);
            com.Send(ID01); 
            com.Send(ID01);
            
            if (outputList != null)
            {
                ID02 = ConvertEscapeSequences(ID02);
                com.Send(ID02);
                com.Send(ID02);

                if (outputList != null)
                {
                    ID03 = ConvertEscapeSequences(ID03);
                    com.Send(ID03); 
                    com.Send(ID03);
                    
                    if (outputList != null)
                    {
                        ID04 = ConvertEscapeSequences(ID04);
                        com.Send(ID04); 
                        com.Send(ID04);

                        if (outputList != null)
                        {
                            ID05 = ConvertEscapeSequences(ID05);
                            com.Send(ID05); 
                            com.Send(ID05);

                            if (outputList != null)
                            {
                                ID06 = ConvertEscapeSequences(ID06);
                                com.Send(ID06); 
                                com.Send(ID06);
                            }
                        }
                    }
                }
            }
        }

Open in new window

private void button6_Click(object sender, EventArgs e)
        {
            textBox2.Clear();
            string ID01 = "ID01";
            string ID02 = "ID02";
            string ID03 = "ID03";
            string ID04 = "ID04";
            string ID05 = "ID05";
            string ID06 = "ID06";
           
            CommPort com = CommPort.Instance;

            ID01 = ConvertEscapeSequences(ID01);
            com.Send(ID01); 
            com.Send(ID01);
            
            if (outputList != null)
            {
                ID02 = ConvertEscapeSequences(ID02);
                com.Send(ID02);
                com.Send(ID02);

                if (outputList != null)
                {
                    ID03 = ConvertEscapeSequences(ID03);
                    com.Send(ID03); 
                    com.Send(ID03);
                    
                    if (outputList != null)
                    {
                        ID04 = ConvertEscapeSequences(ID04);
                        com.Send(ID04); 
                        com.Send(ID04);

                        if (outputList != null)
                        {
                            ID05 = ConvertEscapeSequences(ID05);
                            com.Send(ID05); 
                            com.Send(ID05);

                            if (outputList != null)
                            {
                                ID06 = ConvertEscapeSequences(ID06);
                                com.Send(ID06); 
                                com.Send(ID06);
                            }
                        }
                    }
                }
            }
        }

Open in new window

0
 
TommySzalapskiCommented:
Instead of using substring, since you are in C#, you can just use the Split function and immediately split the comma separated string into each individual piece and put that in the array. The array code that I have posted should give you the direction you need even if it isn't exactly the solution you need.

You are working with decimal values in the array?
0
 
ocaccyAuthor Commented:
I think not, why?

I'll open another question with 500 points on it and you help me analyze my code and give me advice on what I can improve.
Can it be so?

0
 
ocaccyAuthor Commented:
Thank you.

Helped Me a lot in this part, but my field is wide and I need to speed up the step.
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.

Join & Write a Comment

Featured Post

Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

  • 4
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now