Advertisement
filin

laba_3_z_1_new

Oct 20th, 2016
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.27 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6.  
  7. namespace ConsoleApplication1
  8. {
  9.     class Program
  10.     {
  11.         static void Main(string[] args)
  12.         {
  13.             Console.Title = "Laba3_1";
  14.             Console.WriteLine("Cреднее арифметическое отрицательных элементов массива");
  15.             Console.Write("Введите число элементов массива; n= ");
  16.             int n = EnterInt();
  17.             int[] arr = new int[n];
  18.             FillingArray(arr);
  19.             DisplayArray(arr);
  20.             Console.WriteLine("среднее арифметическое отрицательных элементов массива = {0}", CountingAverage(arr));
  21.             Console.ReadKey();
  22.         }
  23.         static void FillingArray(int[] arr)
  24.         {
  25.             Random Rand = new Random();
  26.             for (int i = 0; i < arr.Length; i++)
  27.             {
  28.                 arr[i] = Rand.Next(-100, 100);
  29.             }
  30.         }
  31.         static void DisplayArray(int[] arr)
  32.         {
  33.             for (int i = 0; i < arr.Length; i++)
  34.             {
  35.                 Console.Write("{0} ", arr[i]);
  36.             }
  37.             Console.WriteLine();
  38.         }
  39.         static double CountingAverage(int[] arr)
  40.         {
  41.             double countAverage = 0;
  42.             int count = 0;
  43.             for (int i = 0; i < arr.Length; i++)
  44.             {
  45.                 if (arr[i] < 0)
  46.                 {
  47.                     countAverage += arr[i];
  48.                     count++;
  49.                 }
  50.             }
  51.             double Average = countAverage / count;
  52.             return Average;
  53.         }
  54.  
  55.         static int EnterInt()
  56.         {
  57.             int value;
  58.             bool result = false;
  59.             result = int.TryParse(Console.ReadLine(), out value);
  60.             if (result == false)
  61.             {
  62.                 do
  63.                 {
  64.                     Console.Write("Некорректные данные. Введите заново: ");
  65.                     result = int.TryParse(Console.ReadLine(), out value);
  66.                 }
  67.                 while (!result);
  68.             }
  69.             return value;
  70.         }
  71.     }
  72. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement