(averrage - max - min) / length
using System;
namespace ConsoleApp13
{
class Program
{
static void Main(string[] args)
{
First first = new First();
Console.Write("Enter length array: ");
int length = Convert.ToInt32(Console.ReadLine());
int[] array = first.SetValues(length, new int[length]);
Console.WriteLine(first.Averrage(length, array));
}
}
class First
{
public int[] SetValues(int length, int[] arr)
{
int temp;
for (int i = 0; i < length; i++)
{
Console.Write($"Enter {i+1} number: ");
while (int.TryParse(Console.ReadLine(), out temp) && temp <= 2)
{
Console.Write("Enter number >2: ");
}
arr[i] = temp;
}
return arr;
}
public double Averrage(int length, int[] arr)
{
double averrage = 0;
int max = int.MinValue;
int min = int.MaxValue;
for (int i = 0; i <= length - 1; i++)
{
if (min > arr[i] )
{
min = arr[i];
}
if (max < arr[i])
{
max = arr[i];
}
averrage += arr[i];
}
return (averrage - max - min) / length;
}
}
}
var nums = new[] { 100, 3, 102, 7, 1, 9, 12 };
var max = default(int?);
var min = default(int?);
var avg = 0.0;
var avgCount = 0;
foreach (var number in nums)
{
if (min == null)
{
min = number;
}
else if (max == null)
{
max = number;
if (max < min)
{
max = min;
min = number;
}
}
else if (number < min)
{
avg = ((avg * avgCount) + min.Value) / ++avgCount;
min = number;
}
else if (number > max)
{
avg = ((avg * avgCount) + max.Value) / ++avgCount;
max = number;
}
else
{
avg = ((avg * avgCount) + number) / ++avgCount;
}
}
var ints = new[] { 22, 55, 11, 33, 77, 66 };
// вариант 1
var avg1 = ints.Where(a => a != ints.Max() && a != ints.Min()).Average();
// вариант 2
var avg2 = ints.Except(new[] { ints.Max(), ints.Min() }).Average();
Console.WriteLine(avg1); // 44
Console.WriteLine(avg2); // 44