Advertisement
SUni2020

Chapter7 C#BOOK

Jul 18th, 2020
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.04 KB | None | 0 0
  1. using System;
  2.  
  3. namespace Problem1
  4. {
  5. class Program
  6. {
  7. static void Main(string[] args)
  8. {
  9. //Да се напише програма, която създава масив с 20 елемента от целочислен тип и инициализира всеки от елементите със стойност равна на индекса на елемента умножен по 5.
  10. //Елементите на масива да се изведат на конзолата.
  11. //Използвайте масив int[] и for цикъл.
  12.  
  13. int[] array = new int[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20 };
  14.  
  15. Console.Write("Output: ");
  16.  
  17. for (int index = 0; index < array.Length; index++)
  18.  
  19. {
  20.  
  21. //Multiply the number by 5
  22.  
  23. array[index] = 5 * array[index];
  24.  
  25. //Print the number
  26.  
  27. Console.Write(array[index] + " ");
  28.  
  29. }
  30. }
  31. }
  32. }
  33.  
  34. using System;
  35.  
  36. namespace Problem2
  37. {
  38. class Program
  39. {
  40. static void Main(string[] args)
  41. {
  42. // Да се напише програма, която чете два масива от конзолата и прове­рява дали са еднакви.
  43. // Два масива са еднакви, когато имат еднаква дължина и стойностите на елементите в тях съответно съвпадат.Второто условие можете да проверите с for цикъл.
  44.  
  45. Console.WriteLine("Please enter the length of the FIRST Array:");
  46. int f = int.Parse(Console.ReadLine());
  47. int[] firstArr = new int[f];
  48.  
  49. Console.WriteLine("Please enter the length of the SECOND Array:");
  50. int s = int.Parse(Console.ReadLine());
  51. int[] secondArr = new int[s];
  52.  
  53. if (firstArr.Length != secondArr.Length)
  54. {
  55. Console.WriteLine("Error! The FIRST Array length and the SECOND Array length are not equal. Please try again and enter EQUAL array lengths");
  56. }
  57.  
  58. for (int i = 0; i <= firstArr.Length - 1; i++)
  59. {
  60. Console.WriteLine("Please enter the {0} number of FIRST array", i);
  61. firstArr[i] = int.Parse(Console.ReadLine());
  62. }
  63.  
  64. for (int i = 0; i <= secondArr.Length - 1; i++)
  65. {
  66. Console.WriteLine("Please enter the {0} number of SECOND array", i);
  67. secondArr[i] = int.Parse(Console.ReadLine());
  68. }
  69.  
  70. bool areEqual = true;
  71. for (int i = 0; i < secondArr.Length; i++)
  72. {
  73. if (firstArr[i] != secondArr[i])
  74. {
  75. areEqual = false;
  76. break;
  77. }
  78. }
  79.  
  80. Console.WriteLine("The two arrays are equal: {0}", areEqual);
  81. }
  82.  
  83. }
  84. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement