Advertisement
Teodor92

02. Comapre

Jan 6th, 2013
554
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.16 KB | None | 0 0
  1. /* Write a program that reads
  2.  * two arrays from the console
  3.  * and compares them element by element.
  4.  */
  5.  
  6. using System;
  7.  
  8. class TwoArrayCompare
  9. {
  10.     static void Main()
  11.     {
  12.         Console.WriteLine("Enter the array length:");
  13.         int n = int.Parse(Console.ReadLine());
  14.         int[] firstArray = new int[n];
  15.         int[] secondArray = new int[n];
  16.         for (int i = 0; i < firstArray.Length; i++)
  17.         {
  18.             Console.WriteLine("Enter element - {0} from the first array",i);
  19.             firstArray[i] = int.Parse(Console.ReadLine());
  20.         }
  21.         for (int i = 0; i < firstArray.Length; i++)
  22.         {
  23.             Console.WriteLine("Enter element - {0} from the second array", i);
  24.             secondArray[i] = int.Parse(Console.ReadLine());
  25.         }
  26.         Array.Sort(firstArray);
  27.         Array.Sort(secondArray);
  28.         bool areEqual = true;
  29.         for (int i = 0; i < firstArray.Length; i++)
  30.         {
  31.             if (firstArray[i] != secondArray[i])
  32.             {
  33.                 areEqual = false;
  34.                 break;
  35.             }
  36.         }
  37.         Console.WriteLine("The two arrays are equal: {0}", areEqual);
  38.     }
  39. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement