desislava_topuzakova

1. Статистика на масив

Feb 27th, 2022
1,192
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.60 KB | None | 0 0
  1. using System;
  2. using System.Linq;
  3.  
  4. namespace _01._Array_Statistics
  5. {
  6.     internal class Program
  7.     {
  8.         static void Main(string[] args)
  9.         {
  10.             string input = Console.ReadLine(); //"2 3 4 5 6 1".Split(' ') -> ["2", "3", "4", "5", "6", "1"]
  11.  
  12.             int[] numbers = input              //"2 3 4 5 6 1"
  13.                             .Split(' ')        //["2", "3", "4", "5", "6", "1"]
  14.                             .Select(int.Parse) // "2" -> 2, "3" -> 3, "4" -> 4, "5" -> 5, "6" -> 6, "1" -> 1
  15.                             .ToArray();        //[2, 3, 4, 5, 6, 1]
  16.  
  17.  
  18.             //всички елементи
  19.             int sum = 0;
  20.             int min = int.MaxValue;
  21.             int max = int.MinValue;
  22.             for (int index = 0; index <= numbers.Length - 1; index++)
  23.             {
  24.                 int currentElement = numbers[index];
  25.                 sum = sum + currentElement; //сумираме числата
  26.  
  27.                 //проверка за минимум
  28.                 if (currentElement < min)
  29.                 {
  30.                     min = currentElement;
  31.                 }
  32.  
  33.                 //проверка за максимум
  34.                 if (currentElement > max)
  35.                 {
  36.                     max = currentElement;
  37.                 }
  38.             }
  39.  
  40.             Console.WriteLine("Min = " + min);
  41.             Console.WriteLine("Max = " + max);
  42.             Console.WriteLine("Sum = " + sum);
  43.             double average = sum * 1.0 / numbers.Length;
  44.             Console.WriteLine("Average = " + average);
  45.  
  46.  
  47.  
  48.  
  49.  
  50.         }
  51.     }
  52. }
  53.  
Advertisement
Add Comment
Please, Sign In to add comment