Advertisement
ntodorova

Arrays - 02_TwoArraysEqual

Jan 12th, 2013
265
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.22 KB | None | 0 0
  1. using System;
  2.  
  3. /*
  4.  * 2. Write a program that reads two arrays from the console and compares them element by element.
  5.  */
  6. class TwoArraysEqual
  7. {
  8.     static void Main()
  9.     {
  10.         int n;
  11.         int m;
  12.         bool equal = true;
  13.  
  14.         Console.Write("Number of the first array elements: ");
  15.         string strN = Console.ReadLine();
  16.  
  17.         Console.Write("Number of the second array elements: ");
  18.         string strM = Console.ReadLine();
  19.  
  20.         if (!int.TryParse(strN, out n) || !int.TryParse(strM, out m))
  21.         {
  22.             Console.WriteLine("Invalid numbers!");
  23.         }
  24.         else
  25.         {
  26.             if (n != m)
  27.             {
  28.                 equal = false;
  29.             }
  30.             else
  31.             {
  32.                 int[] firstArr = new int[n];
  33.                 int[] secondArr = new int[m];
  34.  
  35.                 for (int i = 0; i < firstArr.Length; i++)
  36.                 {
  37.                     Console.Write("Please enter a first array elements: ");
  38.                     firstArr[i] = int.Parse(Console.ReadLine());
  39.                 }
  40.  
  41.                 for (int j = 0; j < secondArr.Length; j++)
  42.                 {
  43.                     Console.Write("Please enter a second array elements: ");
  44.                     secondArr[j] = int.Parse(Console.ReadLine());
  45.                 }
  46.  
  47.                 for (int i = 0; i < firstArr.Length; i++)
  48.                 {
  49.                     if (firstArr[i] != secondArr[i])
  50.                     {
  51.                         equal = false;
  52.                     }
  53.                 }  
  54.             }
  55.  
  56.             Console.WriteLine("Arrays are equal? - {0}", equal);
  57.         }
  58.     }
  59. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement