Advertisement
lina94

ComparingElementsInArrays

Jul 8th, 2013
56
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.36 KB | None | 0 0
  1. using System;
  2.  
  3. // 2. Write a program that reads two arrays from the console and compares them element by element.
  4.  
  5. class ComparingElementsInArrays
  6. {
  7.     static void Main()
  8.     {
  9.         Console.Write("Type the number of integers in the arrays here: ");
  10.         // the length of the arrays
  11.         int n = int.Parse(Console.ReadLine());
  12.  
  13.         // allocating the arrays
  14.         int[] firstArr = new int[n];
  15.         int[] secondArr = new int[n];
  16.  
  17.         Console.WriteLine("Type {0} numbers in the first array: ", n);
  18.        
  19.         for (int i = 0; i < firstArr.Length; i++) // firstArr.Length = secondArr.Length = n
  20.         {
  21.             firstArr[i] = int.Parse(Console.ReadLine());
  22.         }
  23.  
  24.         Console.WriteLine("Type {0} numbers in the second array: ", n);
  25.  
  26.         for (int i = 0; i < secondArr.Length; i++)
  27.         {
  28.             secondArr[i] = int.Parse(Console.ReadLine());
  29.         }
  30.  
  31.         // checking if the elements in the arrays with equal index have equal values too
  32.         for (int i = 0; i < n ; i++)
  33.         {
  34.             if (firstArr[i] != secondArr[i])
  35.             {
  36.                 Console.WriteLine("{0} is not equal to {1} !", firstArr[i], secondArr[i]);
  37.             }
  38.             else
  39.             {
  40.                 Console.WriteLine("{0} is equal to {1} !!! ", firstArr[i], secondArr[i]);
  41.             }
  42.         }
  43.     }
  44. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement