jeudi 24 septembre 2015

Finding the sum of select numbers in a 2D array

I am doing some practice work and I have gotten a little stuck. The focus has been on 2D arrays. The method holding me up is meant to take a 2D array (that has been pre-filled with random integers [-100,100]) and find the sum of the even, negative numbers, returning the sum as an integer so I can print it in main (I'm trying to make sure my return methods are being done correctly too.)

I have figured out the first part using a nested for loop, and then nesting two if statements to check if its even and negative. What I don't know what to do is take the negative, even numbers it does find and store them somewhere (maybe another array or a list?) to be added and find the sum. Do I need any else statements, or will it just overlook any of non even/negative numbers? Here is the code so far for this particular method.

 public static int sumNegative(int[,] array)
 {
        for (int i = 0; i < array.GetLength(0); i++)
        {
            for (int j = 0; j < array.GetLength(1); j++)
            {
                if( array[i,j] % 2 == 0){
                    if(array[i,j] < 0){

                    }
                }
            }

    }
    //And this int (whatever I call it) will be the sum of the values, correct?
    return (int?);

}

I appreciate any insight you all can provide for me, I am pretty new at coding.

Aucun commentaire:

Enregistrer un commentaire