Venciity

[Jumpstart C#] Arrays 03-MinMaxAndAverage

Feb 17th, 2014
156
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.21 KB | None | 0 0
  1. /*Напишете програма която намира и отпечатва най-големия и най-малкия елемент в масива,
  2.  *както и средно-аритметичната стойност на елементите в него.
  3.   Пример: { 2, 7, 1, 5, 4, 1, 5, 3, 6, 2 }
  4.   Изход: Min = 1 Max = 7 Average = 3.6     */
  5.  
  6. using System;
  7. using System.Collections.Generic;
  8. using System.Linq;
  9. using System.Text;
  10. using System.Threading.Tasks;
  11.  
  12. namespace _03_MinMaxAndAverage
  13. {
  14.     class MinMaxAndAverage
  15.     {
  16.         static void Main()
  17.         {
  18.             int[] array = { 2, 7, 1, 5, 4, 1, 5, 3, 6, 2 };
  19.             int min = array[0];
  20.             int max = array[0];
  21.             int sum = array[0];
  22.  
  23.             for (int i = 1; i < array.Length; i++)
  24.             {
  25.                 int n = array[i];
  26.  
  27.                 if (min > n)
  28.                 {
  29.                     min = n;
  30.                 }
  31.                 if (max < n)
  32.                 {
  33.                     max = n;
  34.                 }
  35.                 sum += n;
  36.             }
  37.             Console.WriteLine("Minx = {0} , Max = {1} , Average = {2}",min,max,sum / (float)array.Length);
  38.         }
  39.     }
  40. }
Advertisement
Add Comment
Please, Sign In to add comment