Write a C# Sharp program to find the larger average value between the first and the second half of a given array of integers and minimum length is at least 2. Assume that the second half begins at index (array length)/2

  • برمجة سي شارب
  • برمجة

Write a C# Sharp program to find the larger average value between the first and the second half of a given array of integers and minimum length is at least 2. Assume that the second half begins at index (array length)/2

Sample Output:

6
10

الأجوبة

using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace exercises
{
   class Program
    {       
        static void Main(string[] args)
        {               
           Console.WriteLine(test(new[] { 1, 2, 3, 4, 6, 8 })); 
           Console.WriteLine(test(new[] { 15, 2, 3, 4, 15, 11 })); 
          }               
        static int test(int[] numbers)
         {
            var firstHalf = Average(numbers, 0, numbers.Length / 2);
            var secondHalf = Average(numbers, numbers.Length / 2, numbers.Length);
            return firstHalf > secondHalf ? firstHalf : secondHalf;
        }

        private static int Average(int[] num, int start, int end)
        {
            var sum = 0;
            for (var i = start; i < end; i++)
                sum += num[i];
            return sum / (num.Length / 2);
        }
    
    } 
}
هل كان المحتوى مفيد؟

تبحث عن مدرس اونلاين؟

محتاج مساعدة باختيار المدرس الافضل؟ تواصل مع فريقنا الان لمساعدتك بتأمين افضل مدرس
ماهو التخصص الذي تبحث عنه؟
اكتب هنا...