0

I have a method that has 2 output parameters. The method should take an array and return both the sum and the average of the values in the array. There is another method that creates an array from user input. The array needs to be initialized from the main method. I am really stumped with this one. I hope you guys can help. I have included my code below.

// Create a console-based application whose Main() method declares an array of eight integers.
//
// Call a method to interactivelyfill the array with any number of values up to eight.
//
// Call a second method that accepts out parameters for the arithmetic average and the sum of the values in the array.
//
// Display the array values, the number of entered elements, and their average and sum in the Main() method.


using System;

namespace ArrayManagment
{
    class Program
    {
        static void arrayMath(out int sum, out int avg)
        {
           sum = myArray.Sum();
           avg = myArray.Average();
        }
        static void displayArray(int[] myArray)
        {
            Console.Write("Your numbers are: ");
            for (int i = 0; i < 8; i++)
                Console.Write(myArray[i] + " ");
            Console.WriteLine();

        }

        static int[] fillArray()
        {
            int[] myArray;
            myArray = new int[8];
            int count = 0;
            do
            {
                Console.Write("Please enter a number to add to the array or \"x\" to stop: ");
                string consoleInput = Console.ReadLine();
                if (consoleInput == "x")
                {
                    return myArray;
                }
                else
                {
                    myArray[count] = Convert.ToInt32(consoleInput);
                    ++count;
                }

            } while (count < 8);

            return myArray;

        }

        static void Main(string[] args)
        {
            int[] myArray;
            myArray = new int[8];
            myArray = fillArray();
            int sum, avg;
            arrayMath(out sum, out avg);

            displayArray(myArray);


        }
    }
}
1
  • 1
    Not asking for anyone to do the work. I am asking for help with understanding how to pass the array to the method. All the code is mine, I just don't know how do to what I want to do. Commented Feb 28, 2011 at 1:25

1 Answer 1

4

Just put it as a parameter before the output parameters:

static void arrayMath(int[] myArray, out int sum, out int avg)

(There is no need for it to be before the output parameters, but it just makes sense.)

Then send in the array to the method in the Main method:

arrayMath(myArray, out sum, out avg);
Sign up to request clarification or add additional context in comments.

2 Comments

The reason Guffa is saying to put the array before the out parameters is because it is usually considered best practice to put out parameters at the end.
Ok I thought about doing that. Just didn't think I could do both at the same time. Thanks for the help Guffa!

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.