【问题标题】:Sum of columns in c# arraysc#数组中的列总和
【发布时间】:2016-04-09 18:13:33
【问题描述】:
static int[,] matrix ={
                          { 4, 6, 9, 2, 5, 7},
                          { 4, 7, 5, 3, 7, 5},
                          { 4, 2, 6, 9, 1, 6}

                         };

static int rowLength = matrix.GetLength(0);
static int colLength = matrix.GetLength(1);       

public static void Main(string[] args) {
            displayMatrix();
            Console.ReadKey();

        }//end Main

static void displayMatrix() { // Display The matrix
for (int i = 0; i < rowLength; i++) {
                for (int j = 0; j < colLength; j++) {
                    Console.Write(string.Format("{0} ", matrix[i, j]));
                }
                Console.Write(Environment.NewLine + Environment.NewLine);
            }
        }// end displayMatrix

我不知道如何计算每列的总和,然后我想显示具有最大值的列总和。 我不确定是在我已经存在的函数中使用一组嵌套函数,还是创建另一个数组来存储列总和结果? (矩阵也可以修改)

【问题讨论】:

  • 您是否在互联网上搜索过这个问题?这些问题都有流利的答案。在这里发送问题之前最好先搜索问题。
  • 我在这里看不到任何尝试求和的代码,更不用说识别和/或显示最大总和了。我也不知道您所说的“嵌套函数”是什么意思; C# 中唯一与之相关的是匿名方法(可以在其他方法中声明),但目前尚不清楚这与您的问题有何关系。的确,在 SO 和 Internet 上有很多关于矩阵和算术的示例,但是您的问题是如此模糊和不清楚,我什至不确定您的问题会与您的哪个重复。

标签: c# arrays for-loop sum max


【解决方案1】:

对于每一列我都这样做了。不要忘记声明 int iValue=0;和 int y=0。

    public void TryThis()
    {
        for (int x = 0; x < matrix.GetLength(1); x++)
        {
            for (y = 0; y < matrix.GetLength(0); y++)
            {
                iValue = iValue + matrix[y, x]; 
            }
            textBox1.Text =textBox1.Text+ ("Colum " + x + " Sum=" + iValue);
            iValue = 0;
        }
    }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-10-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多