Среднее арифметическое элементов в диапазоне от a до b - C#
Формулировка задачи:
Привет всем, проверьте мой код, пожалуйста, исправьте, что не правильно!))
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication3
{
class Program
{
static void Main(string[] args)
{
int n = 0;
{
Console.WriteLine("Введите размерность массива");
n = Convert.ToInt32(Console.ReadLine());
}
int[] mas = new int[n];
Random r = new Random();
Console.WriteLine("Исходный массив:\n");
for (int i = 0; i < n; i++)
{
mas[i] = r.Next(0, 10);
Console.WriteLine(" {0}", mas[i].ToString());
}
int a = 0;
int b = 0;
{
Console.WriteLine("Введите значение a:");
a = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("Введите значение b:");
b = Convert.ToInt32(Console.ReadLine());
}
int count=0;
int s=0;
double mid = 0;
for (int i = 0; i < n; i++)
{
if (mas[i] > a && mas[i] < b)
{
s += mas[i];
++count;
mid = s / count;
}
}
Console.WriteLine(mid);Решение задачи: «Среднее арифметическое элементов в диапазоне от a до b»
textual
Листинг программы
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication3
{
class Program
{
static void Main(string[] args)
{
int n = 0;
{
Console.WriteLine("Введите размерность массива");
n = Convert.ToInt32(Console.ReadLine());
}
int[] mas = new int[n];
Random r = new Random();
Console.WriteLine("Исходный массив:\n");
for (int i = 0; i < n; i++)
{
mas[i] = r.Next(0, 10);
Console.WriteLine(" {0}", mas[i].ToString());
}
int a = 0;
int b = 0;
{
Console.Write("Введите значение a : ");
a = Convert.ToInt32(Console.ReadLine());
Console.Write("Введите значение b : ");
b = Convert.ToInt32(Console.ReadLine());
}
int count = 0;
int s = 0;
double mid = 0;
for (int i = 0; i < n; i++)
{
if (mas[i] > a && mas[i] < b)
{
s += mas[i];
++count;
}
}
mid = s / count;
Console.WriteLine("Среднее арифметическое : "+mid);
}
}
}