-->

Finding the sum of the values in a 2D Array in C#

2020-08-01 08:57发布

问题:

Trying to build a method that will find the sum of all the values within the 2D array. I'm very new to programming and can't find a good starting point on trying to figure out how its done. Here is what I have so far (forgive me, I'm usually an english/history guy, logic isn't my forte...)

class Program
{
    static void Main(string[] args)
    {
        int[,] myArray = new int[5,6];
        FillArray(myArray);
        LargestValue(myArray);

    }
    //Fills the array with random values 1-15
    public static void FillArray(int[,] array)
    {
        Random rnd = new Random();
        int[,] tempArray = new int[,] { };
        for (int i = 0; i < tempArray.GetLength(0); i++)
        {
            for (int j = 0; j < tempArray.GetLength(1); j++)
            {
                tempArray[i, j] = rnd.Next(1, 16);
            }
        }
    }
    //finds the largest value in the array (using an IEnumerator someone 
    //showed me, but I'm a little fuzzy on how it works...)
    public static void LargestValue(int[,] array)
    {
        FillArray(array);
        IEnumerable<int> allValues = array.Cast<int>();
        int max = allValues.Max();
        Console.WriteLine("Max value: " + max);
    }
    //finds the sum of all values
    public int SumArray(int[,] array)
    {
        FillArray(array);
    }
}

I guess I could try to find the sum of each row or column and add them up with a for loop? Add them up and return an int? If anyone could shed any insight, it would be greatly appreciated, thanks!

回答1:

Firstly, you don't need to call FillArray in the beginning of each method, you have already populated the array in the main method, you are passing a populated array to these other methods.

A loop similar to what you use to populate the array is the easiest to understand:

//finds the sum of all values
public int SumArray(int[,] array)
{
    int total = 0;
    // Iterate through the first dimension of the array
    for (int i = 0; i < array.GetLength(0); i++)
    {
        // Iterate through the second dimension
        for (int j = 0; j < array.GetLength(1); j++)
        {
            // Add the value at this location to the total
            // (+= is shorthand for saying total = total + <something>)
            total += array[i, j];
        }
    }
    return total;
}


回答2:

To sum an array if you know the length is easy As a bonus code included to get the highest valeu too. You could easily expand this to get other kinds of statistical code. I asume below Xlength and Ylength are integers too, and known by you. You could also replace them by a number in the code.

int total = 0;
int max=0;
int t=0;  // temp valeu
For (int x=0;x<Xlength;x++)
{
 for (int y=0;y<Ylength;y++)
 {
   t = yourArray[x,y]; 
   total =total +t;
   if(t>max){max=t;}   // an if on a single line
 }
}

here is a link with an MSDN sample on how to retrieve unknown array lengths. and there is a nice site to have around when you start in c# google ".net perls"