Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- namespace ConsoleApplication1
- {
- class Program
- {
- static void Main(string[] args)
- {
- Console.Title = "Laba3_1";
- Console.WriteLine("Cреднее арифметическое отрицательных элементов массива");
- Console.Write("Введите число элементов массива; n= ");
- int n = EnterInt();
- int[] arr = new int[n];
- FillingArray(arr);
- DisplayArray(arr);
- Console.WriteLine("среднее арифметическое отрицательных элементов массива = {0}", CountingAverage(arr));
- Console.ReadKey();
- }
- static void FillingArray(int[] arr)
- {
- Random Rand = new Random();
- for (int i = 0; i < arr.Length; i++)
- {
- arr[i] = Rand.Next(-100, 100);
- }
- }
- static void DisplayArray(int[] arr)
- {
- for (int i = 0; i < arr.Length; i++)
- {
- Console.Write("{0} ", arr[i]);
- }
- Console.WriteLine();
- }
- static double CountingAverage(int[] arr)
- {
- double countAverage = 0;
- int count = 0;
- for (int i = 0; i < arr.Length; i++)
- {
- if (arr[i] < 0)
- {
- countAverage += arr[i];
- count++;
- }
- }
- double Average = countAverage / count;
- return Average;
- }
- static int EnterInt()
- {
- int value;
- bool result = false;
- result = int.TryParse(Console.ReadLine(), out value);
- if (result == false)
- {
- do
- {
- Console.Write("Некорректные данные. Введите заново: ");
- result = int.TryParse(Console.ReadLine(), out value);
- }
- while (!result);
- }
- return value;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement