Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- /*
- Write a program that reads two arrays from the console
- and compares them element by element.
- */
- namespace CompareTwoArrays
- {
- public class GenericArrayComparison
- {
- static void Main(string[] args)
- {
- //First case: two arrays of different non-castable types
- #region
- int[] array1 = new int[5];
- string[] array2 = new string[5];
- array1 = InitializeArray(array1);
- array2 = InitializeArray(array2);
- #endregion
- Console.WriteLine("First check: compare int and string of same length.");
- int result = Compare(array1, array2);
- PrintResultOfComparison(result, array1, array2);
- //Second case: two arrays of different castable types
- #region
- int[] array3 = new int[5];
- byte[] array4 = new byte[5];
- array3 = InitializeArray(array3);
- array4 = InitializeArray(array4);
- Console.WriteLine("Second check: compare int and byte of same length.");
- result = Compare(array3, array4);
- PrintResultOfComparison(result, array3, array4);
- #endregion
- //Third case: two arrays of same types, different length
- #region
- int[] array5 = new int[5];
- int[] array6 = new int[7];
- array5 = InitializeArray(array5);
- array6 = InitializeArray(array6);
- Console.WriteLine("Third check: compare int and int of diff."+
- " length, same common elements.");
- result = Compare(array5, array6);
- PrintResultOfComparison(result, array5, array6);
- #endregion
- //Fourth case: two arrays of same types, same length, same values
- #region
- char[] array7 = new char[5];
- char[] array8 = new char[5];
- array7 = InitializeArray(array7);
- array8 = InitializeArray(array8);
- Console.WriteLine("Fourth check: compare int and int of same. length, same values.");
- result = Compare(array7, array8);
- PrintResultOfComparison(result, array7, array8);
- #endregion
- //Fifth case: two arrays of same types, same length, diff values arr9 > arr10
- #region
- double[] array9 = new double[5];
- double[] array10 = new double[5];
- array9 = InitializeArray(array9);
- array10 = InitializeArray(array10);
- array9[3] = 12;
- Console.WriteLine("Fifth check: compare int and int of same. length,"+
- " diff values. arrayOne is bigger than arrayTwo");
- result = Compare(array9, array10);
- PrintResultOfComparison(result, array9, array10);
- #endregion
- //Sixth case: two arrays of same types, only one initialized arr11 > arr12
- //See http://msdn.microsoft.com/en-us/library/system.icomparable.compareto.aspx
- // "Remarks section"
- #region
- double[] array11 = new double[5];
- double[] array12 = new double[5];
- array12= InitializeArray(array12);
- Console.WriteLine("Sixth case: two arrays of same types,"+
- "only one (arrayTwo) initialized; arrayTwo > arrayOne" +
- " ");
- result = Compare(array11, array12);
- PrintResultOfComparison(result, array11, array12);
- #endregion
- }
- public static void PrintResultOfComparison<T,U>(int result, T[] arrOne, U[] arrTwo)
- {
- if (result==0)
- {
- Console.WriteLine("Arrays are equal.");
- }
- else if (result == 1)
- {
- Console.WriteLine("arrayOne is bigger than arrayTwo.");
- }
- else if (result == -1)
- {
- Console.WriteLine("arrayTwo is bigger than arrayOne.");
- }
- else
- {
- Console.WriteLine("Can`t compare elements of type{0} and type {1}.",
- typeof(T), typeof(U));
- }
- Console.WriteLine();
- }
- static int Compare<T, U>(T[] firstArray, U[] secondArray)
- where T:IComparable where U:IComparable
- {
- if (typeof(T)!= typeof(U))
- {
- //This way a different message is printed for arrays of different types -
- //They are not just different - they can`t be compared.
- //Note: The method does not implement comparison of types that have implicit cast
- //in one direction - it assumes that two types are different.
- return int.MinValue;
- }
- else
- {
- //If arrays have different length we must iterate only to the length of the shorter.
- int minLength = Math.Min(firstArray.Length, secondArray.Length);
- for (int i = 0; i < minLength; i++)
- {
- //Here we use the defalt "CompareTo" method for the respective type
- //Since the case is : "typeof(T)== typeof(U)", it is only one type.
- //In order to compare two generic type arrays, the type must
- //implement the IComparable interface -
- //that condition is set through "where T:IComparable".
- //In our case the term "to implement the IComparable interface" means -
- //the type(int, char, double and so on) must have the "CompareTo" method.
- //see http://msdn.microsoft.com/en-us/library/system.icomparable.compareto.aspx
- int resultOfCurrentComparison = firstArray[i].CompareTo(secondArray[i]);
- //At the moment we find different elements we return the result of the
- //comparison.
- //Example: Though "net" is shorter than "validation"
- //in a standard English dictionary the former appears first.
- if (resultOfCurrentComparison != 0)
- {
- return resultOfCurrentComparison;
- }
- }
- if (firstArray.Length!=secondArray.Length)
- {
- return firstArray.Length > secondArray.Length ? 1 : -1;
- }
- else
- {
- return 0;
- }
- }
- }
- //(You must be able to convert from int to specified type T) .
- public static T[] InitializeArray<T>(T[] array)
- {
- for (int i = 0; i < array.Length; i++)
- {
- //convert int to the required type
- //See :http://stackoverflow.com/questions/8625/generic-type-conversion-from-string
- //See: http://predicatet.blogspot.com/2009/04/c-string-to-generic-type-conversion.html
- array[i] = (T)Convert.ChangeType(i, typeof(T));
- }
- return array;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement