nKolchakoV

CompareTwoArrays

Dec 7th, 2013
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.49 KB | None | 0 0
  1. using System;
  2. //Write a program that reads two arrays from the console and compares them element by element.
  3.  
  4. class Program
  5. {
  6.     static void Main()
  7.     {
  8.         Console.Write("Enter the length of 1st array: ");
  9.         int lengthOne = int.Parse(Console.ReadLine());
  10.         string[] arrayOne = new string[lengthOne];
  11.         for (int firstArray = 0; firstArray < lengthOne; firstArray++)
  12.         {
  13.             Console.Write("Enter array[{0}] = ", firstArray);
  14.             arrayOne[firstArray] = Console.ReadLine();
  15.         }
  16.         Console.Write("Enter the length of 2nd array: ");
  17.         int lengthTwo = int.Parse(Console.ReadLine());
  18.         string[] arrayTwo = new string[lengthTwo];
  19.         for (int secondArray = 0; secondArray < lengthTwo; secondArray++)
  20.         {
  21.             Console.Write("Enter array[{0}] = ", secondArray);
  22.             arrayTwo[secondArray] = Console.ReadLine();
  23.         }
  24.         bool isEqual = true;
  25.         //Start comparing
  26.         if (arrayOne.Length == arrayTwo.Length)
  27.         {
  28.             for (int i = 0; i < arrayOne.Length; i++)
  29.             {
  30.                 if (arrayOne[i] == arrayTwo[i])
  31.                 {
  32.                     isEqual = true;
  33.                 }
  34.                 else
  35.                 {
  36.                     isEqual = false;
  37.                 }
  38.             }
  39.         }
  40.         else
  41.         {
  42.             isEqual = false;
  43.         }
  44.         Console.WriteLine("The two arrays are {0} !", isEqual ? "Equal" : "Different");
  45.     }
  46. }
Advertisement
Add Comment
Please, Sign In to add comment