配列内の最大値、最小値、総和、平均値を求める方法を書きます.

4555 ワード

 class Program
    {   
        /// <summary>
        ///  , , 。
         /// </summary>
        /// <param name="nums"> </param>
        /// <returns> (max,min,sum,avg)</returns>
        public static int[] GetMaxMinSumAvg(int[] nums)
        {
            int[] arr = new int[4];
            // arr[0] ,arr[1] ,arr[2] ,arr[3] 。
              arr[0] = nums[0];//max
            arr[1] = nums[0];//min
            arr[2] = 0;//sum
            for (int i = 0; i <nums.Length ; i++)
            {
                if(arr[0]<nums[i])
                {
                    arr[0] = nums[i];
                }
                if (arr[1] > nums[i])
                {
                    arr[1] = nums[i];
                }
                arr[2] += nums[i];
            }
                arr[3] = arr[2] / arr.Length;
                return arr;
        }
        static void Main(string[] args)
        {
          int[] number = { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
          int []arr = GetMaxMinSumAvg(number);
          Console.WriteLine(" {0}, {1}, {2}, {3}",arr[0],arr[1],arr[2],arr[3]);
          Console.ReadLine();