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++)
{
sumcol[col] += mat[row, col];
}
}
return sumcol;
}
:
void Main()
{
int[,] array = {
{ 1, 2, 3 },
{ 1, 2, 3 },
{ 1, 2, 3 },};
Console.WriteLine(String.Join(" ", sumcolumn(array)));
int[] result = sumcolumn(array);
}