Fill a 1D array from matrix

I have a matrix, my task is to populate a 1D array from my matrix.

Example:

1 2 3

1 2 3

1 2 3

I need to sum the columns and fill in the sum of each column in the 1D array. Here is my code (which does not work) (int[,] mat)is the matrix that the function receives.

public static int sumcolumn(int[,] mat)
{
    int sum = 0;
    int[] sumcol = new int[mat.GetLength(0)];
    for (int y = 0; y < mat.GetLength(0); y++)
    {
        for (int i = 0; i < mat.GetLength(0); i++)
        {
           for (int j = 0; j < mat.GetLength(1); j++)
           {
               sum = sum + mat[j, i];
           }
           sumcol[i] = sum;
           return sum;
           sum = 0;
        }
        return sum;
    }
    return sumcol;
}

How do I complete this mission?

Thanks in advance.

+4
source share
4 answers

For loops, you need only 2. For each column, all the lines go through and summarize the contents. Write the amount in the corresponding col index. Then, after each column, reset the amount. You also need to return an array with the sum. So I changed the return value:

, .

public static int[] sumcolumn(int[,] mat)
{
    int[] sumcol = new int[mat.GetLength(1)];

    for (int col = 0; col < mat.GetLength(1); col++)
    {
        for (int row = 0; row < mat.GetLength(0); row++)
        {
             // since sumcol is initially filled with zeros you can just  
             // sum up the values from mat onto the zeros in each cell
            sumcol[col] += mat[row, col];
        }
    }
    return sumcol;
}

:

void Main()
{
    int[,] array = { 
    { 1, 2, 3 },
    { 1, 2, 3 },
    { 1, 2, 3 },};

    // this is just for test display
    Console.WriteLine(String.Join(" ", sumcolumn(array)));

    // normally you would call it like this and catch the return value in a new array
    int[] result = sumcolumn(array);

}
+4

, 2D-, 1D-. , int[] int.

, , :

  • , .
  • .
  • i j - , a[i,j] a[j,i] a.

:

public static int[] sumcolumn(int[,] mat)
{
    int sum = 0;
    int[] sumcol = new int[mat.GetLength(1)];

    for (int i= 0; i< mat.GetLength(1); i++)
    {
        sum = 0; // reset sum for next colomn
        for (int j= 0; j< mat.GetLength(0); j++)
        {
            sum += mat[i, j];
        }
        // iteration of column completed
        sumcol[i] = sum;
    }
    return sumcol;
}
+2

Linq

int[,] array = new int[3, 3] { { 1, 2, 3 }, 
                               { 1, 2, 3 }, 
                               { 1, 2, 3 } };  

int[] result = Enumerable.Range(0, array.GetUpperBound(1) + 1)
                         .Select(y => Enumerable.Range(0, array.GetUpperBound(0) + 1)
                         .Select(x => array[x, y]).Sum()).ToArray(); // [3,6,9]
+1
public static int[] sumColumn(int[,] mat)
{
    //int sum = 0;
    int colCount = mat.GetLength(0);
    int[] sumCol = new int[colCount];

    for (int y = 0; y < colCount; y++)
    {
        int rowCount = mat.GetLength(1);
        sumCol[y] = 0;

        for (int x = 0; x < rowCount; x++)
        {
            sumCol[y] += mat[y, x];
        }

        //sum += sumCol[y];
    }

    //return sum;
    return sumCol;
}
-1

Source: https://habr.com/ru/post/1669827/


All Articles