Advertisement
Guest User

Untitled

a guest
Jan 16th, 2019
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.86 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 Zad1._3
  8. {
  9. class Program
  10. {
  11. static void Main(string[] args)
  12. {
  13. // Stworzenie tablicy oraz obiektu klasy Random:
  14. int[] array = new int[3];
  15. Random random = new Random();
  16.  
  17. // Uzupełnienie tablicy losowymi wartościami oraz jej wyświetlenie:
  18. Console.WriteLine("Original array:");
  19. for (int i = 0; i < array.Length; i++)
  20. {
  21. array[i] = random.Next(70);
  22. }
  23. DisplayArray(array);
  24.  
  25. // Sortowanie (w tym przypadku: bąbelkowe):
  26. for (int j = 0; j < array.Length - 1; j++)
  27. {
  28. for (int i = 0; i < array.Length - 1; i++)
  29. {
  30. // Jeżeli element AKTUALNY jest większy od NASTĘPNY to zamien je ze sobą:
  31. if (array[i] > array[i + 1])
  32. {
  33. int temporary = array[i];
  34. array[i] = array[i + 1];
  35. array[i + 1] = temporary;
  36. }
  37. }
  38. }
  39.  
  40. // Wyświetlenie posortowanej tablicy:
  41. Console.WriteLine();
  42. Console.WriteLine("Sorted array:");
  43. DisplayArray(array);
  44.  
  45. // Ile razy występuje dana wartość:
  46. Console.WriteLine();
  47. Console.WriteLine("Occurence counts:");
  48. List<int> list = new List<int>();
  49. for (int j = 0; j < array.Length; j++)
  50. {
  51. int number = array[j];
  52. if (list.Contains(number))
  53. {
  54. continue;
  55. }
  56.  
  57. list.Add(number);
  58. int numberCount = 0;
  59. for (int i = 0; i < array.Length; i++)
  60. {
  61. if (number == array[i])
  62. {
  63. numberCount++;
  64. }
  65. }
  66.  
  67. Console.WriteLine(number + ": " + numberCount);
  68. }
  69.  
  70. // Suma:
  71. Console.WriteLine();
  72. Console.WriteLine("Sum:");
  73. int sum = 0;
  74. for (int i = 0; i < array.Length; i++)
  75. {
  76. sum += array[i];
  77. }
  78. Console.WriteLine(sum);
  79.  
  80. // Kopiowanie tablic:
  81. Console.WriteLine();
  82. Console.WriteLine("Performing two (wrong and proper) array copies...");
  83. int[] wrongCopiedArray = array;
  84. int[] properCopiedArray = CopyArray(array);
  85. Console.WriteLine();
  86. Console.WriteLine("Changing first original array item...");
  87. array[0] = -100;
  88. Console.WriteLine();
  89. Console.WriteLine("Original array with changed first element to -100:");
  90. DisplayArray(array);
  91. Console.WriteLine();
  92. Console.WriteLine("Wrong copied array:");
  93. DisplayArray(wrongCopiedArray);
  94. Console.WriteLine();
  95. Console.WriteLine("Proper copied array:");
  96. DisplayArray(properCopiedArray);
  97.  
  98. Console.ReadLine();
  99. }
  100.  
  101. private static void DisplayArray(int[] someOtherArray)
  102. {
  103. for (int i = 0; i < someOtherArray.Length; i++)
  104. {
  105. Console.WriteLine((i + 1) + ": " + someOtherArray[i]);
  106. }
  107. }
  108.  
  109. private static int[] CopyArray(int[] originalArray)
  110. {
  111. int[]copiedTable = new int[originalArray.Length];
  112.  
  113. for (int i = 0; i < originalArray.Length; i++)
  114. {
  115. copiedTable[i] = originalArray[i];
  116. }
  117.  
  118. return copiedTable;
  119. }
  120. }
  121. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement