Advertisement
Guest User

task4

a guest
Oct 22nd, 2019
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.65 KB | None | 0 0
  1. using System;
  2.  
  3. // Задача: реализовать метод FindMinMax, который должен возвращать минимальное и максимальное значение в массиве вложенных массивов (jagged array).
  4. //   * Метод должен возвращать true, если он содержит хотя бы одно числовое значение.
  5. //   * Метод должен пропускать null значения в массиве.
  6. //   * Метод должен выбрасывать исключение ArgumentNullException в случае, если в метод передали null.
  7. //   * В решении разрешается использовать только конструкции языка. Использовать LINQ запрещено.
  8.  
  9. public class Program
  10. {
  11.     public static bool FindMinMax(int[][] array, out int min, out int max)
  12.     {
  13.         min = int.MaxValue;
  14.         max = int.MinValue;
  15.  
  16.         bool result = false;
  17.  
  18.         if (array == null)
  19.         {
  20.             throw new ArgumentNullException("array");
  21.         }
  22.  
  23.         for (int i = 0; i < array.Length; i++)
  24.         {
  25.             if(result = array[i] != null)
  26.             { }
  27.             else
  28.             {
  29.                 array[i] = new int[] { 0 };
  30.             }
  31.             //Console.WriteLine(array[i]);
  32.             Console.WriteLine(result);
  33.  
  34.  
  35.             int[] inArray = array[i];
  36.  
  37.             for (int j = 0; j < inArray.Length; j++)
  38.             {
  39.                 if (inArray[j] < min)
  40.                 {
  41.                     min = inArray[j];
  42.                 }
  43.                 //Console.WriteLine(inArray[j]);
  44.             }
  45.             for (int j = 0; j < inArray.Length; j++)
  46.             {
  47.                 if (inArray[j] > max)
  48.                 {
  49.                     max = inArray[j];
  50.                 }
  51.                 //Console.WriteLine(inArray[j]);
  52.             }
  53.  
  54.            
  55.         }
  56.        
  57.  
  58.         Console.WriteLine(min);
  59.         Console.WriteLine(max);
  60.  
  61.        
  62.         return result;
  63.     }
  64.  
  65.     // ДОБАВЬТЕ НОВЫЕ МЕТОДЫ, ЕСЛИ НЕОБХОДИМО
  66.  
  67.     // ----- ЗАПРЕЩЕНО ИЗМЕНЯТЬ КОД МЕТОДОВ, КОТОРЫЕ НАХОДЯТСЯ НИЖЕ -----
  68.  
  69.     public static void Main()
  70.     {
  71.         Console.WriteLine("Task is done when all test cases are correct:");
  72.  
  73.         int testCaseNumber = 1;
  74.  
  75.         TestReturnedValues(testCaseNumber++, new int[][] { }, false, 0, 0);
  76.         TestReturnedValues(testCaseNumber++, new int[][] { null }, false, 0, 0);
  77.         TestReturnedValues(testCaseNumber++, new int[][] { new int[] { } }, false, 0, 0);
  78.         TestReturnedValues(testCaseNumber++, new int[][] { new int[] { 1 } }, true, 1, 1);
  79.         TestReturnedValues(testCaseNumber++, new int[][] { null, new int[] { } }, false, 0, 0);
  80.         TestReturnedValues(testCaseNumber++, new int[][] { new int[] { }, null }, false, 0, 0);
  81.         TestReturnedValues(testCaseNumber++, new int[][] { new int[] { 2 }, new int[] { 1 } }, true, 1, 2);
  82.         TestReturnedValues(testCaseNumber++, new int[][]
  83.             {
  84.                 new int[] { 1, 2, 3, 4, 5, 6, 7, 8, 9 },
  85.                 new int[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14 },
  86.                 new int[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12 },
  87.                 new int[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }
  88.             }, true, 1, 14);
  89.         TestReturnedValues(testCaseNumber++, new int[][]
  90.             {
  91.                 new int[] { 7, 395, 1, 9, 6, 62, 71, 80, 918 },
  92.                 new int[] { 102, 72, 84, 41, 89, 382, 7, 36, 53, 10, 1101, 93, 23, 1401 },
  93.                 new int[] { 942, 29, 3, 346, 53, 6, 73, 897, 384, 052, 3, 501 },
  94.                 new int[] { 39, 83, 11, 28, 385, 0, 5, 482, 947, 143 }
  95.             }, true, 0, 1401);
  96.         TestException<ArgumentNullException>(testCaseNumber++, null);
  97.  
  98.         if (correctTestCaseAmount == testCaseNumber - 1)
  99.         {
  100.             Console.WriteLine("Task is done.");
  101.         }
  102.         else
  103.         {
  104.             Console.WriteLine("TASK IS NOT DONE.");
  105.         }
  106.     }
  107.  
  108.     private static void TestReturnedValues(int testCaseNumber, int[][] array, bool expectedResult, int expectedMin, int expectedMax)
  109.     {
  110.         try
  111.         {
  112.             int min, max;
  113.             var result = FindMinMax(array, out min, out max);
  114.  
  115.             if (result == expectedResult && expectedMin == min && expectedMax == max)
  116.             {
  117.                 Console.WriteLine(correctCaseTemplate, testCaseNumber);
  118.                 correctTestCaseAmount++;
  119.             }
  120.             else
  121.             {
  122.                 Console.WriteLine(incorrectCaseTemplate, testCaseNumber);
  123.             }
  124.         }
  125.         catch (Exception)
  126.         {
  127.             Console.WriteLine(incorrectCaseTemplate, testCaseNumber);
  128.         }
  129.     }
  130.  
  131.     private static void TestException<T>(int testCaseNumber, int[][] array) where T : Exception
  132.     {
  133.         try
  134.         {
  135.             int min, max;
  136.             var result = FindMinMax(array, out min, out max);
  137.             Console.WriteLine(incorrectCaseTemplate, testCaseNumber);
  138.         }
  139.         catch (ArgumentException)
  140.         {
  141.             Console.WriteLine(correctCaseTemplate, testCaseNumber);
  142.             correctTestCaseAmount++;
  143.         }
  144.         catch (Exception)
  145.         {
  146.             Console.WriteLine(incorrectCaseTemplate, testCaseNumber);
  147.         }
  148.     }
  149.  
  150.     private static string correctCaseTemplate = "Case #{0} is correct.";
  151.     private static string incorrectCaseTemplate = "Case #{0} IS NOT CORRECT";
  152.     private static int correctTestCaseAmount = 0;
  153. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement