I’m new to this page and new to programming as well.
Let me pose my problem.
I have an array let’s say
1 2 3
4 5 6
7 8 9
3 2 1
what I want to do is calculate for each row the difference between all elements.
{Math.Abs(1-2)=1
Math.Abs (2-3)=1
Math.Abs (1-3)=2}
for the first row. Next I want to find the average for each row(in first (1+1+2)/3) and finally keep the row with the smallest average value.
What is the most efficient way to do it??(I was thinking that perphaps LINQ could work but I don’t know how to properly use LINQ).
Any help would be appreciated.
Thank you all in advance!!!!
EDIT:
Thank you all for the answers…there are really usefull and helped me understand the way I should process my problem. I see that most all of you have recommended the use of List. So that raises another problem for me (remember little programming knowledge) how can I convert an int[,] array to List, is this is possible?? Should I convert int[,] to int[][] first and then to List ?
For one more time thank you for your answers and your time.
I created the following function for converting the 2d array to a List..but it seems that it’s not working properly..Any help would be truly appreciated..
public static List<int[]> GetMyNumbers(int[,] result)
{
int[,] res = result;
int length = res.GetUpperBound(0) + 1;
int width = res.GetUpperBound(1) + 1;
int[] rows = new int[width];
List<int[]> numberArrays = new List<int[]>();
for (int i = 0; i < result.Length / width; i++)
{
for (int k = 0; k < width; k++)
{
rows[k] = res[i, k];
Console.Write(rows[k]);
}
Console.WriteLine();
numberArrays.Add(rows);//this doesn't fill the list properly..It adds to all list items the last row of the 2d array
}
return numberArrays;
}
Here is what you are looking for basically. No I did not test the code to make sure that it is 100% correct, but should get you moving in the right direction at very least.