Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Find difference between row elements in C#

Tags:

arrays

c#

list

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;


    }
like image 833
tasos Avatar asked Dec 06 '25 10:12

tasos


1 Answers

It's extremely crude, but hopefully you can take from this. Basically, it converts your jagged array into a List of arrays and then works out the average in a seperate method, and orders the results by average and takes one.

Worth noting that my solution always tries to store the positive value of a difference (as that seems to be what you were doing), i.e. instead of 1 and 3 giving -2 it gives 2.

I am convinced there is a cleaner way to create this solution but this is the best I could come up with for now.

static void Main(string[] args)
        {
            int[][] multi = new int[3][];
            multi[0] = new int[3] { 1, 2, 3 };
            multi[1] = new int[3] { 5, 10, 20 };
            multi[2] = new int[3] { 3, 4, 8 };
            List<int[]> t = multi.ToList();
            List<int[]> avg = t.OrderBy(x => GetAvgDifference(x)).Take(1).ToList();
        }

        public static double GetAvgDifference(int[] arr)
        {
            List<int> differences = new List<int>();
            for (int i = 0; i < arr.Length; i++)
            {
                for (int j = i; j < arr.Length; j++)
                {
                    int difference = arr[i] - arr[j];

                    if (difference < 1)
                    {
                        differences.Add(difference * -1);
                    }
                    else
                    {
                        differences.Add(difference);
                    }
                }
            }

            return differences.Average();
        }
like image 147
AndrewC Avatar answered Dec 08 '25 00:12

AndrewC



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!