stefanpu

Compare Two arrays - homework desicion

Feb 17th, 2013
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 7.36 KB | None | 0 0
  1. using System;
  2. /*
  3.     Write a program that reads two arrays from the console
  4.     and compares them element by element.
  5. */
  6.  
  7. namespace CompareTwoArrays
  8. {
  9.     public class GenericArrayComparison
  10.     {
  11.         static void Main(string[] args)
  12.         {
  13.             //First case: two arrays of different  non-castable types
  14.             #region
  15.             int[] array1 = new int[5];
  16.             string[] array2 = new string[5];
  17.  
  18.             array1 = InitializeArray(array1);
  19.             array2 = InitializeArray(array2);
  20.             #endregion
  21.             Console.WriteLine("First check: compare int and string of same length.");
  22.             int result = Compare(array1, array2);
  23.             PrintResultOfComparison(result, array1, array2);
  24.  
  25.             //Second case: two arrays of different castable types
  26.             #region
  27.             int[] array3 = new int[5];
  28.             byte[] array4 = new byte[5];
  29.  
  30.             array3 = InitializeArray(array3);
  31.             array4 = InitializeArray(array4);
  32.  
  33.             Console.WriteLine("Second check: compare int and byte of same length.");
  34.             result = Compare(array3, array4);
  35.             PrintResultOfComparison(result, array3, array4);
  36.             #endregion
  37.  
  38.             //Third case: two arrays of same types, different length
  39.             #region
  40.             int[] array5 = new int[5];
  41.             int[] array6 = new int[7];
  42.  
  43.             array5 = InitializeArray(array5);
  44.             array6 = InitializeArray(array6);
  45.  
  46.             Console.WriteLine("Third check: compare int and int of diff."+
  47.                 " length, same common elements.");
  48.             result = Compare(array5, array6);
  49.             PrintResultOfComparison(result, array5, array6);
  50.             #endregion
  51.  
  52.             //Fourth case: two arrays of same types, same length, same values
  53.             #region
  54.             char[] array7 = new char[5];
  55.             char[] array8 = new char[5];
  56.  
  57.             array7 = InitializeArray(array7);
  58.             array8 = InitializeArray(array8);
  59.  
  60.             Console.WriteLine("Fourth check: compare int and int of same. length, same values.");
  61.             result = Compare(array7, array8);
  62.             PrintResultOfComparison(result, array7, array8);
  63.             #endregion
  64.  
  65.             //Fifth case: two arrays of same types, same length, diff values arr9 > arr10
  66.             #region
  67.             double[] array9 = new double[5];
  68.             double[] array10 = new double[5];
  69.  
  70.             array9 = InitializeArray(array9);
  71.             array10 = InitializeArray(array10);
  72.  
  73.             array9[3] = 12;
  74.  
  75.             Console.WriteLine("Fifth check: compare int and int of same. length,"+
  76.                 " diff values. arrayOne is bigger than arrayTwo");
  77.             result = Compare(array9, array10);
  78.             PrintResultOfComparison(result, array9, array10);
  79.             #endregion
  80.  
  81.             //Sixth case: two arrays of same types, only one initialized arr11 > arr12
  82.             //See http://msdn.microsoft.com/en-us/library/system.icomparable.compareto.aspx
  83.             // "Remarks section"
  84.             #region
  85.             double[] array11 = new double[5];
  86.             double[] array12 = new double[5];
  87.            
  88.             array12= InitializeArray(array12);
  89.  
  90.             Console.WriteLine("Sixth case: two arrays of same types,"+
  91.                 "only one (arrayTwo) initialized; arrayTwo > arrayOne" +
  92.                 " ");
  93.             result = Compare(array11, array12);
  94.             PrintResultOfComparison(result, array11, array12);
  95.             #endregion          
  96.         }
  97.  
  98.         public static void PrintResultOfComparison<T,U>(int result, T[] arrOne, U[] arrTwo)
  99.         {
  100.             if (result==0)
  101.             {
  102.                 Console.WriteLine("Arrays are equal.");                              
  103.             }
  104.             else if (result == 1)
  105.             {
  106.                 Console.WriteLine("arrayOne is bigger than arrayTwo.");
  107.             }
  108.             else if (result == -1)
  109.             {
  110.                 Console.WriteLine("arrayTwo is bigger than arrayOne.");
  111.             }
  112.             else
  113.             {
  114.                 Console.WriteLine("Can`t compare elements of type{0} and type {1}.",
  115.                     typeof(T), typeof(U));
  116.             }
  117.             Console.WriteLine();
  118.         }
  119.  
  120.  
  121.         static int Compare<T, U>(T[] firstArray, U[] secondArray)
  122.             where T:IComparable where U:IComparable
  123.         {
  124.             if (typeof(T)!= typeof(U))
  125.             {
  126.                 //This way a different message is printed for arrays of different types -
  127.                 //They are not just different - they can`t be compared.
  128.                 //Note: The method does not implement comparison of types that have implicit cast
  129.                 //in one direction - it assumes that two types are different.
  130.                 return int.MinValue;
  131.             }
  132.             else
  133.             {
  134.                 //If arrays have different length we must iterate only to the length of the shorter.
  135.                 int minLength = Math.Min(firstArray.Length, secondArray.Length);
  136.  
  137.                 for (int i = 0; i < minLength; i++)
  138.                 {
  139.                     //Here we use the defalt "CompareTo" method for the respective type
  140.                     //Since the case is : "typeof(T)== typeof(U)", it is only one type.
  141.                     //In order to compare two generic type arrays, the type must
  142.                     //implement the IComparable interface -
  143.                     //that condition is set through "where T:IComparable".
  144.                     //In our case the term "to implement the IComparable interface" means -
  145.                     //the type(int, char, double and so on) must have the "CompareTo" method.
  146.                     //see http://msdn.microsoft.com/en-us/library/system.icomparable.compareto.aspx
  147.                     int resultOfCurrentComparison = firstArray[i].CompareTo(secondArray[i]);
  148.  
  149.                     //At the moment we find different elements we return the result of the
  150.                     //comparison.
  151.                     //Example: Though "net" is shorter than "validation"
  152.                     //in a standard English dictionary the former appears first.                
  153.                     if (resultOfCurrentComparison != 0)
  154.                     {                        
  155.                         return resultOfCurrentComparison;                        
  156.                     }
  157.                 }
  158.  
  159.                 if (firstArray.Length!=secondArray.Length)
  160.                 {                    
  161.                     return firstArray.Length > secondArray.Length ? 1 : -1;
  162.                 }
  163.                 else
  164.                 {
  165.                     return 0;
  166.                 }
  167.             }
  168.         }
  169.  
  170.        
  171.         //(You must be able to convert from int to specified type T) .
  172.         public static T[] InitializeArray<T>(T[] array)
  173.         {
  174.             for (int i = 0; i < array.Length; i++)
  175.             {
  176.                 //convert int to the required type
  177.                 //See :http://stackoverflow.com/questions/8625/generic-type-conversion-from-string
  178.                 //See: http://predicatet.blogspot.com/2009/04/c-string-to-generic-type-conversion.html
  179.                 array[i] = (T)Convert.ChangeType(i, typeof(T));                              
  180.             }
  181.             return array;
  182.         }
  183.     }
  184. }
Advertisement
Add Comment
Please, Sign In to add comment