Advertisement
filin

laba_3_z_1_mk1

Sep 25th, 2016
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.71 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 = "Cреднее арифметическое отрицательных элементов массива";
  14.             Console.Write("Введите число элементов массива; n= ");
  15.             int n = int.Parse(Console.ReadLine());
  16.             int[] arr = new int[n];
  17.             fillingArray(ref arr);
  18.             display(arr);
  19.             Console.WriteLine("среднее арифметическое отрицательных элементов массива = {0}", countingAverage(arr));
  20.             Console.ReadKey();
  21.  
  22.         }
  23.         static void fillingArray(ref 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.         }
  32.         static void display(int[] arr)
  33.         {
  34.             for (int i = 0; i < arr.Length; i++)
  35.             {
  36.                 Console.Write("{0} ", arr[i]);
  37.             }
  38.             Console.WriteLine();
  39.         }
  40.         static double countingAverage(int[] arr)
  41.         {
  42.             double countAverage = 0;
  43.             int count = 0;
  44.             for (int i = 0; i < arr.Length; i++)
  45.             {
  46.                 if (arr[i] < 0)
  47.                 {
  48.                     countAverage += arr[i];
  49.                     count++;
  50.                 }
  51.             }
  52.             countAverage = countAverage / count;
  53.             return countAverage;
  54.         }
  55.     }
  56. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement