2-d随机数组的和。

时间:2023-01-31 16:38:57

I am writing a program that creates a 2-d array, fills it with random numbers and then performs certain operations.

我正在编写一个程序,它创建一个二维数组,用随机数填充它,然后执行某些操作。

My SumArray() method is supposed to have a 2-dimensional array for the parameter list and will add all the numbers in the array and return the sum. I know the way i have it now is wrong and I am stumped on how to do this. Are my other methods in the program coded correctly so far or are they wrong and not allowing me to performm the tasks of the program?

我的SumArray()方法应该有一个2维数组,用于参数列表,并将数组中的所有数字相加并返回总和。我知道我现在的做法是错误的,我也为如何做到这一点而感到困惑。到目前为止,我在程序中的其他方法是否正确编码,或者它们是错误的,不允许我执行程序的任务?

Any help with the SumArray() method or any problems I have in other parts of the code is appreciated!! Thank you!

任何帮助使用SumArray()方法或我在代码的其他部分中遇到的任何问题都是值得赞赏的!!谢谢你!

When I run my program I get an error that says Index was outside the bounds of the Array!

当我运行程序时,我得到一个错误,说索引超出了数组的范围!

Here is my code:

这是我的代码:

static void Main(string[] args)
    {
        string[] months = { "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December" };
        string[,] randomNumArray = new string[3, 5];

        FillArray(randomNumArray);
        PrintArray(randomNumArray);
        SumRows(randomNumArray);
        SumCols(randomNumArray);
        SumArray(randomNumArray);

    }

    public static void FillArray(int[,] randomNumbersArray)
    {
        Random num = new Random();
        for (int r = 0; r < randomNumbersArray.GetLength(0); r++)
        {
            for (int c = 0; c < randomNumbersArray.GetLength(1); c++)
            {
                randomNumbersArray[r, c] = num.Next(15, 97);
            }
        }
    }

    public static void PrintArray(int[,] randomPrintArray)
    {
        for (int r = 0; r < randomPrintArray.GetLength(0); r++)
        {
            for (int c = 0; c < randomPrintArray.GetLength(1); r++)
            {
                Console.Write("{0}", randomPrintArray[r, c]);
            }
            Console.WriteLine();
        }
    }

    public static void SumRows(int[,] sumOfRowsArray)
    {
        int rowSum;
        for (int r = 0; r < sumOfRowsArray.GetLength(0); r++)
        {
            rowSum = 0;
            for (int c = 0; c < sumOfRowsArray.GetLength(1); c++)
            {
                rowSum += sumOfRowsArray[r, c];
            }
        }
    }

    public static void SumCols(int[,] sumOfColsArray)
    {
        int colsSum;
        for (int r = 0; r < sumOfColsArray.GetLength(1); r++)
        {
            colsSum = 0;

            for (int c = 0; c < sumOfColsArray.GetLength(0); c++)
            {
                colsSum += sumOfColsArray[r, c];
            }
        }
    }

    public static void SumArray(int[,] sumOfAllArray)
    {
        int sumOfAll 0;
        for (int r = 0; r < sumOfAllArray.GetLength(0); r++)
        {
            sumOfAll += sumOfAll;
            for (int c = 0; c < sumOfAllArray.GetLength(1); c++)
            {
                sumOfAll += sumOfAllArray[r, c];
            }
        }
        Console.Write(sumOfAll);
    }

1 个解决方案

#1


0  

I think you don't need that

我想你不需要那个。

sumOfAll += sumOfAll;

in SumArray method. If I understand your code correctly, in the inner loop you count the column sum, then add it to itself in the next iteration? For example after inner loop you have first columns sum of 10. You add that 10 to itself and, before iterating over second column, you have total sum of 20.

在SumArray方法。如果我正确地理解了您的代码,在内部循环中您计算了列和,然后在下一次迭代中添加它自己?例如,在内部循环之后,你有第一个列和10。你把这个10加到它本身,在迭代第二列之前,总共是20。

The correct code:

正确的代码:

public static void SumArray(int[,] sumOfAllArray)
{
    int sumOfAll;
    for (int r = 0; r < sumOfAllArray.GetLength(0); r++)
    {
        for (int c = 0; c < sumOfAllArray.GetLength(1); c++)
        {
            sumOfAll += sumOfAllArray[r, c];
        }
    }
}

#1


0  

I think you don't need that

我想你不需要那个。

sumOfAll += sumOfAll;

in SumArray method. If I understand your code correctly, in the inner loop you count the column sum, then add it to itself in the next iteration? For example after inner loop you have first columns sum of 10. You add that 10 to itself and, before iterating over second column, you have total sum of 20.

在SumArray方法。如果我正确地理解了您的代码,在内部循环中您计算了列和,然后在下一次迭代中添加它自己?例如,在内部循环之后,你有第一个列和10。你把这个10加到它本身,在迭代第二列之前,总共是20。

The correct code:

正确的代码:

public static void SumArray(int[,] sumOfAllArray)
{
    int sumOfAll;
    for (int r = 0; r < sumOfAllArray.GetLength(0); r++)
    {
        for (int c = 0; c < sumOfAllArray.GetLength(1); c++)
        {
            sumOfAll += sumOfAllArray[r, c];
        }
    }
}