0

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!

2 Answers 2

4

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;
}
Sign up to request clarification or add additional context in comments.

3 Comments

Thank you Ian for the help. I'm especially thankful for you explaining it so thoroughly and clearly. I've been having a lot of trouble with finding a place to learn all of this that makes sense to me, and you all have been incredibly helpful and easy to understand.
If you're still around I have a question, how do i call the method? do i need to create a new object like 'Object obj = new Object();' and then call it like 'int getTotal = obj.SumArray(myArray);'? I cant seem to find a solution that doesn't involve another object or class being made.
Within the same class you don't need to specify the class/object name. This is because they are within the same scope (basically it means everything that is inside the same set of {curly braces}). Normally from outside your class you would need to instantiate it (create an instance) like so: Program p = new Program(); p.SumArray(myArray); But you've used the keyword static which means it can be accesses even without an instance of the class, like Program.SumArray(myArray); Research the keywords instance, scope and static to understand more.
1

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"

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.