如何在2D数组中查找每列的总和

时间:2015-12-12 14:16:07

标签: c#

注意:数组和列总数应如下图所示(数字不应为 与下图相同,数字应该是随机的)

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 = new double [10,10];             随机x = new Random();             string s ="&#34 ;;

        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现在如何找到每列的总和

6 个答案:

答案 0 :(得分:3)

这在数组实现中非常基础。

有很多方法可以实现这一点。通过使用像这样的简单循环迭代数组结构,我有一种偏好的方法。

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

答案 1 :(得分: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];
    }
}

答案 2 :(得分:0)

您可以尝试以下代码。它是通用代码,可以在任意数量的列上工作,您不需要硬编码列数。 columnSum List包含单个列的总和

update Table2
set Estimated_Delivery_Date =
    dateadd(
        day,
        coalesce((select "No:of Days" from Table1 t1 where t1.Location = Table2.Location), 0)
        + case
            datepart(weekday, dateadd(
                day,
                (select "No:of Days" from Table1 t1 where t1.Location = Table2.Location),
                Ready_To_Ship_Date
            ))
            when (select WeekendDay1 from Table1 t1 where t1.Location = Table2.Location)
              then 2
            when (select WeekendDay2 from Table1 t1 where t1.Location = Table2.Location)
              then 1
            else 0
        end,
        Ready_To_Ship_Date
    )
    end

答案 3 :(得分: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());
}

答案 4 :(得分: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();

        }
    }
}
​

答案 5 :(得分: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;