4

I have a matrix, my mission is to fill 1D array from my matrix.

Example:

1 2 3

1 2 3

1 2 3

I need to sum the columns and fill the sum of every column in a 1D array Here is my code (that doesn't work), (int[,] mat) is the matrix that the function gets.

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 should I do this mission?

Thanks in advance.

3
  • Your mission was to fill 1D array, then why are you returning an integer value from the method instead for a 1D array? Commented Feb 15, 2017 at 13:57
  • delete return sum statment and it should works fine Commented Feb 15, 2017 at 13:59
  • I didn't understand how can I sum the columns and fill every sum of the columns into the 1D array Commented Feb 15, 2017 at 13:59

4 Answers 4

4

You need only 2 for loops. For each column run through all rows and sum up the content. Write the sum at the proper col index. Then after each column reset the sum. You also need to return the array with the sums. So I changed the return value:

Also it helps if you call the index variables with meaningful names.

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;
}

In the main you can test it like this:

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);

}
Sign up to request clarification or add additional context in comments.

5 Comments

I think this line sumcol[col] = sum; should have been outside the inner loop
@Jamiec it does not change the end result, but you are right it is not necessary to write the result at each step :)
Thank you so much!! It works perfect with my code :-)
You are welcome and welcome to StackOverflow. For a first question it is quite a clear and good one. Next time try to be a little more specific on what exactly "does not work" ;)
You can get rid of sum variable at all, just use sumcol[col] += mat[row, col] at the deepest level.
2

So you need to evaluate a 2D matrix to get the column wise sum to a 1D array. So first thing you have to do is change the return type of the method to int[] instead for int.

Let me quote Few things that you have to notice before moving to a fix:

  • If you execute a return during iteration of the loop rest of iterations will not be executed.
  • A function can return only one value in a single call.
  • Let i and j be two positive unequal integers then a[i,j] and a[j,i] will points to two different elements in the matrix a.

As a whole you have to modify the method signature like the following:

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;
}

Comments

1

Linq approach

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]

Comments

-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;
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.