简体   繁体   English

如何获得二维数组中每一列和每一行的总和?

[英]How to get the sum of each column and row in 2d array?

I have a 2d array of 7 x12.我有一个 7 x12 的二维数组。 I'm stuck on how to get the sum of each column and row of it and put each result in lists, one with results of sum of each column, and other with the results of each row.我被困在如何获得每列和每行的总和并将每个结果放入列表中,一个带有每列总和的结果,另一个带有每一行的结果。

So, for columns, I thought to save the result of each column in a temp integer variable, iterating in each row with 'a' and when 'a' is equal to 12 (which is the numbers of rows), then check if 'p' (which is the number of columns) is equal to 7 and exit the loop (which means that sum of each column has been added to a new array), if not so, then the temp variable will be added to new array, the temp variable and 'a' will reset, so the loop can start again and increment p by one for get the sum of next row.因此,对于列,我想将每列的结果保存在临时 integer 变量中,在每一行中使用 'a' 进行迭代,并且当 'a' 等于 12(这是行数)时,然后检查是否' p'(即列数)等于 7 并退出循环(这意味着每列的总和已添加到新数组中),如果不是这样,则临时变量将添加到新数组中,临时变量和“a”将重置,因此循环可以重新开始并将 p 加一以获得下一行的总和。

for (int a = 0; a < 12; a++)
        {
            int sum =+ students[a, p];

            if(a == 12)
            {
                if (p == 7)
                {
                    break;
                }

                sum_columns.Add(sum);
                sum = 0;
                a = 0;
                p++;

            }
        }

Thanks guys!多谢你们!

You can simply iterate as necessary.您可以根据需要简单地进行迭代。 No complex conditions or resets required:无需复杂的条件或重置:

for (int col = 0; col < 7; ++col) // iterate through the columns
{
    int sum = 0;
    for (int row = 0; row < 12; ++row) // iterate through the rows in the column and sum
    {
        sum += students[row, col];
    }
    sum_columns.Add(sum); // add sum to the list
}

Try it online在线尝试

Not the most efficient, but allows you to grab a specific row or column.不是最有效的,但允许您抓取特定的行或列。

class Program
{
    static IEnumerable<T> GetRow<T>(T[,] someArray, int row)
    {
        for (int i = 0; i <= someArray.GetUpperBound(1); i++)
            yield return someArray[row, i];
    }

    static IEnumerable<T> GetColumn<T>(T[,] someArray, int column)
    {
        for (int i = 0; i <= someArray.GetUpperBound(0); i++)
            yield return someArray[i, column];
    }

    static void Main(string[] args)
    {
        int[,] someArray = new int[4, 2]
            {
                { 1, 2 },
                { 3, 4 },
                { 5, 6 },
                { 7, 8 }
            };
        int rows = someArray.GetUpperBound(0) + 1;
        int columns = someArray.GetUpperBound(1) + 1;
        for (int i = 0; i < rows; i++)
            Console.WriteLine($"Row {i} sum is {GetRow(someArray, i).Sum()}");
        for (int i = 0; i < columns; i++)
            Console.WriteLine($"Column {i} sum is {GetColumn(someArray, i).Sum()}");
    }
}

Output: Output:

Row 0 sum is 3
Row 1 sum is 7
Row 2 sum is 11
Row 3 sum is 15
Column 0 sum is 16
Column 1 sum is 20

Using Linq,使用 Linq,

 public int GetSumOfColumn(int[,] matrix, int columnNumber)
    {
        return Enumerable.Range(0, matrix.GetLength(0))
                .Select(x => matrix[x, columnNumber])
                .Sum();
    }

    public int GetSumOfRow(int[,] matrix, int rowNumber)
    {
        return Enumerable.Range(0, matrix.GetLength(1))
                .Select(x => matrix[rowNumber, x])
                .Sum();
    }

Call these functions in individual for loop, I did it for rows, you can try out the same with columns.在单独的 for 循环中调用这些函数,我是为行做的,你可以对列进行同样的尝试。

    List<int> row_sum = new List<int>();
    for(var rowIndex = 0; rowIndex < 4; rowIndex++)
    {
        var sum = GetSumOfRow(array, rowIndex);
        row_sum.Add(sum);
    }
    Console.WriteLine(string.Join(Environment.NewLine, row_sum));

Implementation: .NET FIDDLE实施: .NET FIDDLE

Reference: https://www.codegrepper.com/参考: https://www.codegrepper.com/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM