Advertisement
MilenaSP

[C# 2] Arrays - Task 2

Dec 17th, 2013
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.36 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6.  
  7. namespace CompareArrays
  8. {
  9. /* Да се напише програма, която чете два масива от конзолата и прове­рява дали са еднакви
  10. * Два масива са еднакви, когато имат еднаква дължина и стойностите на елементите в тях
  11. * съответно съвпадат. Второто условие можете да проверите с for цикъл.
  12. */
  13.  
  14. class ArrayComparison
  15. {
  16. static void Main(string[] args)
  17. {
  18. Console.WriteLine("How many elements in the first array?");
  19. int firstArrayElmnts = int.Parse(Console.ReadLine());
  20. int[] arrayOne = new int[firstArrayElmnts];
  21.  
  22. Console.WriteLine("How many elements in the second array?");
  23. int secondArrayElmnts = int.Parse(Console.ReadLine());
  24. int[] arrayTwo = new int[secondArrayElmnts];
  25.  
  26. bool equal = false;
  27. int arrayOneValueTemp = 0;
  28. int arrayTwoValueTemp = 0;
  29.  
  30. //check if first condition (equal lenght) is true
  31. if (firstArrayElmnts != secondArrayElmnts)
  32. {
  33. Console.WriteLine("It's {0} that the arrays are equal", equal);
  34. }
  35. else //if length differs, then compare element values
  36. {
  37. for (int i = 0; i < arrayOne.Length; i++)
  38. {
  39. Console.WriteLine("Enter value for the first array");
  40. arrayOne[i] = int.Parse(Console.ReadLine());
  41. arrayOneValueTemp = arrayOne[i];
  42. }
  43. for (int i = 0; i < arrayTwo.Length; i++)
  44. {
  45. Console.WriteLine("Enter value for the second array");
  46. arrayTwo[i] = int.Parse(Console.ReadLine());
  47. arrayTwoValueTemp = arrayTwo[i];
  48. }
  49. if (arrayOneValueTemp != arrayTwoValueTemp)
  50. {
  51. Console.WriteLine("The arrays are not equal!");
  52. return;
  53. }
  54. }
  55. Console.WriteLine("The arrays are equal!");
  56. }
  57. }
  58. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement