Facebook
From Gary, 7 Years ago, written in Plain Text.
Embed
Download Paste or View Raw
Hits: 249
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6.  
  7. namespace Lab4_test
  8. {
  9.     class Program
  10.     {
  11.         static void Main(string[] args)
  12.         {
  13.             //6
  14.             try
  15.             {
  16.                 //1
  17.                 int[] vector = new int[] { 3, 7, -2, -3, 1, 8, 2, 3, -5, 0 };
  18.                 //comment the line above and uncomment the line below to see the result of exception handling
  19.                 //int[] vector = new int[] {};
  20.  
  21.                 //6 cont.
  22.                 if (vector.Length < 1)
  23.                 {
  24.                     throw new Exception("Vector variable cannot be empty");
  25.                 }
  26.  
  27.                 //2
  28.                 List<int> vectList = new List<int>(vector);
  29.  
  30.                 //3
  31.                 int min = int.MaxValue, max = int.MinValue, sum = 0;
  32.                 foreach (int val in vectList)
  33.                 {
  34.                     //min
  35.                     if (val < min)
  36.                     {
  37.                         min = val;
  38.                     }
  39.  
  40.                     //max
  41.                     if (val > max)
  42.                     {
  43.                         max = val;
  44.                     }
  45.  
  46.                     //sum
  47.                     sum += val;
  48.                 }
  49.  
  50.                 //4
  51.                 Console.WriteLine("min:{0}, max:{1}, ave:{2}, sum:{3}, card:{4}", min, max, sum / (double)vectList.Count, sum, vectList.Count);
  52.  
  53.                 //5
  54.                 vectList.Sort();
  55.                 Console.WriteLine("List sorted asc.: " + string.Join(", ", vectList.ToArray()));
  56.                 vectList.Reverse();
  57.                 Console.WriteLine("List sorted desc.: " + string.Join(", ", vectList.ToArray()));
  58.             }
  59.             catch (Exception  e)
  60.             {
  61.                 Console.WriteLine(e);
  62.             }
  63.             //end
  64.             Console.Read();
  65.         }
  66.     }
  67. }