2015-12-12 40 views
-1

注意:列數組和總應出現如下圖所示(數字不應 相同如下圖所示,數應該是隨機的)如何找到總在二維陣列的每一列的

5 1 1 1 1 1 1 1 1 1 
2 2 4 5 6 7 8 8 9 9 
1 1 1 1 1 1 1 1 1 1 
2 2 2 2 2 2 2 2 2 2 
3 3 3 3 3 3 3 3 3 3 
5 5 5 5 5 5 5 5 5 5 
4 4 4 4 4 4 4 4 4 4 
7 7 7 7 7 7 7 7 7 7 
3 3 3 3 3 3 3 3 3 3 
1 1 1 1 1 1 1 11 1 1 

每列的總

33 29 31 32 33 34 35 45 36 36 

`進入coddouble [,] A =新的雙[10,10]; 隨機x =新隨機(); string s =「」;

 for (int r = 0; r < 10; r++) 
     { 
      for (int c = 0; c < 10; c++) 
      { 
       a[r, c] = x.Next(1, 21); 
       s = s + a[r, c].ToString() + "\t"; 


      } 
     } 
     textBox1.Text = s;e here` 

這是如何打印ARRY現在如何找到每一列

+0

我不能找到每列的總數 – sara

+1

好的要求。順便說一句,你有什麼嘗試迄今解決這個問題? – Rahul

+1

請添加數組的定義,以便我們知道您是如何聲明它的。 –

回答

2

這是數組實現非常基本的總和。

有很多種方法,通過使用簡單的循環像這樣在陣列結構迭代實現this.One我的首選方式。

static int array[10][10]; 
//or like 
int[,] array = new int[,] 
{ 
    <declare array structure> 
}; 
int i, j, rowlength=10, columnlength=10, sum = 0; //assuming your order 10 as given example .To make it dynamic use array.Length 

sum = 0; 
for (j = 0; j < columnlength; ++j) 
{ 
    for (i = 0; i < rowlength; ++i) 
    { 
     sum = sum + array[i][j]; 
    } 
    Console.WriteLine(String.Format("Summation of {0}th column is {1}", j,sum)); 
    sum=0;  
} 
+0

這種方式找到的所有元素的總和,但我需要找到每列 – sara

+0

的總和@sara檢查現在編輯。 –

+0

它不工作 – sara

0
var arr2d = new int[5]; 
arr2d[0] = {1,2,3,4,5}; 
// etc... 

var arr = new int[arr2d.length]; 

for(int i=0; i<arr2d.length; i++) { 
    var item = arr2d[i]; 
    for(int j=0; j<item.length; j++) { 
     arr[i] += item[j]; 
    } 
} 
0

你可以試試下面的代碼。它的通用代碼,可以在任意數量的列上工作,不需要硬編碼列號。 columnSum列表包含個別列的總和

var array2D = Get2DArray(); //some function to populate, you can have your own way to populate your 2D array 

List<int> columnSum = new List<int>(); 

for (int j = 0; j < array2D.length ; ++j) 
{ 
    int sum = 0; 
    for (int i = 0; i < array2D[j].length; ++i) 
    { 
     sum = sum + array2D[i][j]; 
    } 

    columnSum.Add(sum); // columnSum List contain the sum of individual column 
} 
0

嘗試使用您的代碼。

我所做的就是和你添加到數組列。這是假設c是列,r是行。

double[,] a = new double[10, 10]; 
Random x = new Random(); 
string s = ""; 
var colsum = new List<int>(); 
for (int r = 0; r < 10; r++) 
{ 
    var col = 0; 
    for (int c = 0; c < 10; c++) 
    { 
     col += x.Next(1, 21); 
     a[r, c] = col; 
     s = s + a[r, c].ToString() + "\t"; 
    } 
    colsum.Add(col); 
} 
foreach(var col in colsum){ 
    Console.WriteLine(colsum.ToString()); 
} 
0

試試這個

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using System.IO; 

namespace ConsoleApplication1 
{ 
    class Program 
    { 
     static void Main(string[] args) 
     { 

      int[,] data = new int[,] { 
       {5, 1, 1, 1, 1, 1, 1, 1, 1, 1 }, 
       {2 ,2, 4, 5, 6, 7, 8, 8, 9, 9}, 
       {1, 1, 1, 1, 1, 1, 1, 1, 1, 1}, 
       {2, 2, 2, 2, 2, 2, 2, 2, 2, 2}, 
       {3, 3, 3, 3, 3, 3, 3, 3, 3, 3}, 
       {5, 5, 5, 5, 5, 5, 5, 5, 5, 5}, 
       {4, 4, 4, 4, 4, 4, 4, 4, 4, 4}, 
       {7, 7, 7, 7, 7, 7, 7, 7, 7, 7}, 
       {3, 3, 3, 3, 3, 3, 3, 3, 3, 3}, 
       {1, 1, 1, 1, 1, 1, 1, 11, 1, 1} 
      }; 
      int[] sum = new int[10]; 

      for (int row = 0; row < 10; row++) 
      { 
       for (int col = 0; col < 10; col++) 
       { 
        if (row == 0) 
        { 
         sum[col] = data[row, col]; 
        } 
        else 
        { 
         sum[col] += data[row, col]; 
        } 
       } 
      } 

      List<List<int>> data2 = new List<List<int>>() { 
       new List<int>() {5, 1, 1, 1, 1, 1, 1, 1, 1, 1 }, 
       new List<int>() {2 ,2, 4, 5, 6, 7, 8, 8, 9, 9}, 
       new List<int>() {1, 1, 1, 1, 1, 1, 1, 1, 1, 1}, 
       new List<int>() {2, 2, 2, 2, 2, 2, 2, 2, 2, 2}, 
       new List<int>() {3, 3, 3, 3, 3, 3, 3, 3, 3, 3}, 
       new List<int>() {5, 5, 5, 5, 5, 5, 5, 5, 5, 5}, 
       new List<int>() {4, 4, 4, 4, 4, 4, 4, 4, 4, 4}, 
       new List<int>() {7, 7, 7, 7, 7, 7, 7, 7, 7, 7}, 
       new List<int>() {3, 3, 3, 3, 3, 3, 3, 3, 3, 3}, 
       new List<int>() {1, 1, 1, 1, 1, 1, 1, 11, 1, 1} 
      }; 

      //using linq 
      int[] sum2 = data2.Select(s => s.Select((t, i) => new { num = t, col = i })).SelectMany(u => u).GroupBy(v => v.col).ToList().Select(w => w.Select(x => x.num).Sum()).ToArray(); 

     } 
    } 
} 
​ 
0

這很簡單 - 只需2套for環路喜歡與你這段代碼

static double[] SumColumns(double[,] source) 
{ 
    int rowCount = source.GetLength(0); 
    int colCount = source.GetLength(1); 
    var colSums = new double[colCount]; 
    for (int row = 0; row < rowCount; row++) 
     for (int col = 0; col < colCount; col++) 
      colSums[col] += source[row, col]; 
    return colSums; 
} 

使用範例:

int rows = 10, cols = 10; 
var a = new double[rows, cols]; 
var x = new Random(); 
var sb = new StringBuilder(); 
for (int r = 0; r < rows; r++) 
{ 
    for (int c = 0; c < cols; c++) 
    { 
     a[r, c] = x.Next(1, 21); 
     if (c > 0) sb.Append("\t"); 
     sb.Append(a[r, c]); 
    } 
    sb.AppendLine(); 
} 
var colsSums = SumColumns(a); 
for (int c = 0; c < cols; c++) 
{ 
    if (c > 0) sb.Append("\t"); 
    sb.Append(colsSums[c]); 
} 
var s = sb.ToString(); 
textBox1.Text = s; 
+0

我真的不知道該怎麼做以上答案都不對工作 – sara

+0

@sara你或許應該知道究竟是不是爲你工作 –

+0

所有這些答案都是對數組這個例子的總和關於在二維數組中的隨機元素,我必須計算每列的總和,然後打印在其列 – sara