Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
• Status: Solved
• Priority: Medium
• Security: Public
• Views: 328

# How do this: myArray2 [0,0] = MAX (myArray1 [11,0] ~ myArray1 [17,0])

With myArray1 [320.18] and myArray2 [8.18]

How do this:

myArray2 [0,0] = MAXIMUM  (myArray1 [11,0] ~ myArray1 [17,0])
myArray2 [1,0] = AVERAGE (myArray1 [18,0] ~ myArray1 [24,0])

?
Regards,
ocaccy
0
ocaccy
• 8
• 7
1 Solution

Commented:
Any time you do an average or a max, you just do two for loops. In this case, you just do it over a smaller range of the array.
for(int i = 11; i <= 17; i++)
for(int j = 0; j<=0; j++
{
//code for max or avg
}
Note that you don't actually need the j loop here, but I included it since it doesn't hurt anything and makes it easy to change later.
I would use constants.

const int FIRST_AVG_COLUMN = 18;
const int LAST_AVG_COLUMN = 24;
const int FIRST_AVG_ROW = 0;
const int LAST_AVG_ROW = 0;

Then just base the for loops on those. That way if it changes it's super easy.
0

Commented:
If you have to do averages and maxes and mins over several portions of the array, make it a function.

float average(int array[][18], int first_col, int last_col, int first_row, int last_row)
0

Author Commented:
Can it be so?

Regards,
ocaccy
``````for(int i = 11; i <= 17; i++)
for(int j = 0; j<=0; j++
{
int i_Max3 = 0;
foreach (int i_array in myArray1)
{
if (i_array > i_Max2)
{
i_Max2 = i_array;
}
}
myArray2[i,0]=i_Max3;
}
``````
0

Author Commented:
if (i_array > i_Max3)
{
i_Max3 = i_array;
}
0

Commented:
No don't use a for each. It should look like this:
``````int i_Max = myArray1[11][0];
for(int i = 11; i <= 17; i++)
for(int j = 0; j<=0; j++
if (myArray[i][j]> i_Max)
i_Max = myArray[i][j];
``````
0

Commented:
The foreach is going to look at everything in the array which you certainly don't want.

You can start i_Max at 0 if you are 100% positive that the values are not all negative. Starting it at the first spot in the array guarantees that it will work no matter what.
0

Author Commented:
I will have negative values too!

I am suffering a lot with my communication with 6 devices on  the USB port.

When sending the button command below,
the flow goes to the code below the button.
But I do not receive the data correctly.

Help me make a code.
If not comes the answer from all six devices, the system will resubmit the commands?

Regards,
ocaccy
``````private void button10_Click(object sender, EventArgs e)
{
timr_ID06.Enabled = true;
textBox2.Clear();
CommPort com = CommPort.Instance;

for (int i = 0; i <= 6; i++)
{
string vstr = "ID0" + i.ToString();
vstr = ConvertEscapeSequences(vstr);
com.Send(vstr);
}
}
``````
``````/// <summary>
/// </summary>
private Line partialLine = null;

/// <summary>
/// Add data to the output.
/// </summary>
/// <param name="StringIn"></param>
/// <returns></returns>
{
String StringOut = PrepareData(StringIn);

// if we have a partial line, add to it.
if (partialLine != null)
{
// tack it on
partialLine.Str = partialLine.Str + StringOut;
outputList_Update(partialLine);
return partialLine;
}

}

// delegate used for Invoke
internal delegate void StringDelegate(string data);

/// <summary>
/// Handle data received event from serial port.
/// </summary>
/// <param name="data">incoming data</param>
{
if (InvokeRequired)
{
Invoke(new StringDelegate(OnDataReceived), new object[] { dataIn });
return;
}

// pause scrolling to speed up output of multiple lines
bool saveScrolling = scrolling;
scrolling = false;

// if we detect a line terminator, add line to output
int index;
while (dataIn.Length > 0 &&
((index = dataIn.IndexOf("\r")) != -1 ||
(index = dataIn.IndexOf("\n")) != -1))
{
String StringIn = dataIn.Substring(0, index);
dataIn = dataIn.Remove(0, index + 1);

partialLine = null;	// terminate partial line
}

// if we have data remaining, add a partial line
if (dataIn.Length > 0)
{
}

// restore scrolling
scrolling = saveScrolling;
outputList_Scroll();
listBox1_Scroll();
}

/// <summary>
/// Update the connection status
/// </summary>
public void OnStatusChanged(string status)
{
if (InvokeRequired)
{
Invoke(new StringDelegate(OnStatusChanged), new object[] { status });
return;
}

textBox1.Text = status;
}
``````
0

Commented:
This really should be a new question or I will be the only one that sees it.
If you post the new question in the Misc Programming or Algorithms zones, I'll see it but I don't know how much help I'd be with serial port communication. When you post the question, make sure to explain what the code is supposed to be doing (I can't tell)

In order to wrap this question up, I recommend writing the max and min stuff as functions like this
``````int max(int array[][WIDTH], int first_col, int last_col, int first_row, int last_row)
{
int i_max = array[first_col][last_col];

for(int i = first_col; i <= last_col; i++)
for(int j = first_row; j<=last_row; j++)
if (array[i][j]> i_max)
i_max = myArray[i][j];

return i_max;
}

double average(int array[][WIDTH], int first_col, int last_col, int first_row, int last_row)
{
double the_avg = 0;

for(int i = first_col; i <= last_col; i++)
for(int j = first_row; j<=last_row; j++)
if (array[i][j]> i_max)
the_avg += myArray[i][j];

the_avg /= ( (last_col - first_col + 1)*(last_row - first_row + 1) );

return the_avg;
}

//Then you could call them like
//max(myArray, 11, 17, 0, 0)
//average(myArray, 55, 250, 10, 200)
``````
0

Author Commented:
Hi, Tommy.

I am a little confused about that.
When I move this code to VS2010, show the red error and not indents.

``````int max(int array[][WIDTH],
``````
Tommy.JPG
0

Author Commented:
Hi Tommy,

how to convert: string myArray[310,120] to int myArray[310.120]
or
string myArray[310,120] to int myArray2[310,120].

Best Regards,
ocaccy
0

Commented:
Sorry about those errors. I forgot you were using C# and I gave you the C++ version.
Just do it the usual way you pass a 2D array around.

int max(int[,] array, int first_col, int last_col, int first_row, int last_row)
0

Commented:
To convert arrays from ints to strings, you just need something like
``````for(int i = 0; i < height; i++)
for(int j = 0; j < width; j++)
array_of_ints[i][j] = array_of_strings[i][j].ToInt();
``````
0

Author Commented:
Thank you.
0

Author Commented:
Hi Tommy,

I did as you taught me, but is not working.

Regards,
ocaccy
``````int[,] f = new int[10,10]{{2,4,18,7,2,1,5,8,4,9},{5,4,82,7,2,1,5,8,4,9},{2,4,48,7,2,1,5,8,4,9},{5,4,8,7,2,1,5,8,4,9},{25,4,8,37,2,1,5,8,4,9}
,{5,4,8,7,42,1,5,8,4,9},{51,4,8,7,27,1,5,8,4,9},{5,4,8,7,32,61,85,8,4,9},{31,4,8,27,2,51,5,8,4,9},{5,4,8,7,2,1,5,8,4,9}};

//int max(int[,] array, int first_col, int last_col, int first_row, int last_row)
int max(int[,] f, 2, 2, 3, 6);
``````
0

Commented:
When you call the function you just send the variables.
Like this
the_max = max(f, 2, 2, 3, 6);
0

## Featured Post

• 8
• 7
Tackle projects and never again get stuck behind a technical roadblock.