Advertisement
stefanpu

Compare Two arrays

Jan 9th, 2013
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 8.13 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.                
  99.             }
  100.      
  101.             public static void PrintResultOfComparison<T,U>(int result, T[] arrOne, U[] arrTwo)
  102.             {
  103.                 if (result==0)
  104.                 {
  105.                     Console.WriteLine("Arrays are equal.");                              
  106.                 }
  107.                 else if (result == 1)
  108.                 {
  109.                     Console.WriteLine("arrayOne is bigger than arrayTwo.");
  110.                 }
  111.                 else if (result == -1)
  112.                 {
  113.                     Console.WriteLine("arrayTwo is bigger than arrayOne.");
  114.                 }
  115.                 else
  116.                 {
  117.                     Console.WriteLine("Can`t compare elements of type{0} and type {1}.",
  118.                         typeof(T), typeof(U));
  119.                 }
  120.                 Console.WriteLine();
  121.             }
  122.      
  123.      
  124.             static int Compare<T, U>(T[] firstArray, U[] secondArray)
  125.                 where T:IComparable where U:IComparable
  126.             {
  127.                 if (typeof(T)!= typeof(U))
  128.                 {
  129.                     //This way a different message is printed for arrays of different types -
  130.                     //They are not just different - they can`t be compared.
  131.                     //Note: The method does not implement comparison of types that have implicit cast
  132.                     //in one direction - it assumes that two types are different.
  133.                     return int.MinValue;
  134.                 }
  135.                 else
  136.                 {
  137.                     //If arrays have different length we must iterate only to the length of the shorter.
  138.                     int minLength = Math.Min(firstArray.Length, secondArray.Length);
  139.      
  140.                     for (int i = 0; i < minLength; i++)
  141.                     {
  142.                         //Here we use the defalt "CompareTo" method for the respective type
  143.                         //Since the case is : "typeof(T)== typeof(U)", it is only one type.
  144.                         //In order to compare two generic type arrays, the type must
  145.                         //implement the IComparable interface -
  146.                         //that condition is set through "where T:IComparable".
  147.                         //In our case the term "to implement the IComparable interface" means -
  148.                         //the type(int, char, double and so on) must have  the "CompareTo" method.
  149.                         //see http://msdn.microsoft.com/en-us/library/system.icomparable.compareto.aspx
  150.                         int resultOfCurrentComparison = firstArray[i].CompareTo(secondArray[i]);
  151.      
  152.                         //At the moment we find different elements we return the result of the
  153.                         //comparison.
  154.                         //Example: Though "net" is shorter than "validation"
  155.                         //in a standard English dictionary the former appears first.
  156.                    
  157.                         if (resultOfCurrentComparison != 0)
  158.                         {                        
  159.                             return resultOfCurrentComparison;                        
  160.                         }
  161.                     }
  162.      
  163.                     if (firstArray.Length!=secondArray.Length)
  164.                     {                    
  165.                         return firstArray.Length > secondArray.Length ? 1 : -1;
  166.                     }
  167.                     else
  168.                     {
  169.                         return 0;
  170.                     }
  171.                 }
  172.             }
  173.      
  174.            
  175.             //(You must be able to convert from int to specified type T) .
  176.             public static T[] InitializeArray<T>(T[] array)
  177.             {
  178.                 for (int i = 0; i < array.Length; i++)
  179.                 {
  180.                     //convert int to the required type
  181.                     //See :http://stackoverflow.com/questions/8625/generic-type-conversion-from-string
  182.                     //See: http://predicatet.blogspot.com/2009/04/c-string-to-generic-type-conversion.html
  183.                     array[i] = (T)Convert.ChangeType(i, typeof(T));                              
  184.                 }
  185.                 return array;
  186.             }
  187.         }
  188.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement