2017-02-15 132 views
4

我有一個矩陣,我的任務是從我的矩陣填充一維數組。從矩陣填充一維數組

實施例:

1 2 3 

1 2 3 

1 2 3 

我需要總結列和填充每一列的總和在一維數組 這是我的代碼(即不工作),(int[,] mat)是矩陣,該函數得到。

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

我該怎麼做這個任務?

在此先感謝。

+0

你的任務是填補一維數組,那麼你爲什麼從該方法返回一個整數值,而不是一維數組? –

+0

刪除'返回總數'的聲明,它應該工作正常 – MKasprzyk

+0

我不明白我怎麼能總結列和填充列的每個總和到一維數組 –

回答

4

您只需要2個for循環。對於每一列貫穿所有行並總結內容。在合適的索引處寫下總和。然後在每列重設總和。您還需要返回數組。所以我改變了返回值:

如果您使用有意義的名稱調用索引變量,它也會有所幫助。

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

} 
+0

我覺得這行'sumcol [col] = sum;'should已經在內部循環之外 – Jamiec

+0

@Jamiec它不會改變最終結果,但你是對的,沒有必要在每一步都寫出結果:) –

+0

非常感謝你! 它適用於我的代碼完美:-) –

2

所以,你需要評估二維矩陣,以獲得逐列總和爲一維數組。因此,首先必須將方法的返回類型更改爲int[],而不是int

讓我引用你必須移動到修復之前要注意幾件事情:

  • 如果執行迭代循環其餘的迭代過程中的返回將不會被執行。
  • 函數只能在一次調用中返回一個值。
  • 假設ij是兩個正整數不等整數,則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; 
} 
-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

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]