В массиве найти процент положительных, отрицательных и нулевых элементов - C#
Формулировка задачи:
В массиве Х(N) найти процент положительных, отрицательных и нулевых элементов и вывести сообщение, каких элементов больше.
Решение задачи: «В массиве найти процент положительных, отрицательных и нулевых элементов»
textual
Листинг программы
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
namespace ConsoleApplication4 {
class Program {
static void Main(string[] args) {
Random r = new Random();
int arrSize;
Console.Write("Enter Array size: ");
while (!int.TryParse(Console.ReadLine(), out arrSize)) {
Console.WriteLine("Wrong size! Try again");
}
int[]array = Enumerable.Range(0,arrSize).Select(n=>r.Next(-10,11)).ToArray();
int nullPercent = (array.Count(n => (n == 0)) * 100) / arrSize;
int posPercent = (array.Count(n => (n > 0)) * 100) / arrSize;
int negPercent = (array.Count(n => (n < 0)) * 100) / arrSize;
Console.WriteLine("Positive percentage: {0}\nNegative percentage: {1}\nNull percentage: {2}",
posPercent, negPercent, nullPercent);
Console.ReadLine();
}
}
}