Advertisement
Guest User

zad 2

a guest
Oct 23rd, 2019
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.06 KB | None | 0 0
  1. //a
  2. int n = 20, k = 0, k2 = 0, a = 0, a2 = 0;
  3. Random rand = new Random();
  4. int[] tab = new int[21];
  5.  
  6. for (int i = 0; i < n; i++)
  7. {
  8. tab[i] = rand.Next(10, 21);
  9. Console.WriteLine(tab[i]);
  10.  
  11. if (tab[i] % 2 == 0)
  12. {
  13. a++;
  14. k += tab[i];
  15. }
  16. else
  17. {
  18. a2++;
  19. k2 += tab[i];
  20. }
  21. }
  22. double wynikp = (double)k / (double)a;
  23. double wynikn = (double)k2 / (double)a2;
  24.  
  25. double wynik = wynikp - wynikn;
  26. Console.WriteLine("Różnica : " + wynik);
  27. //b
  28. //start sortowania
  29. int n1 = 20, tmp = 0;
  30. do
  31. {
  32. for (int i = 0; i < n1 - 1; i++)
  33. {
  34. if (tab[i] > tab[i + 1])
  35. {
  36. tmp = tab[i];
  37. tab[i] = tab[i + 1];
  38. tab[i + 1] = tmp;
  39.  
  40. }
  41.  
  42. }
  43. n1--;
  44. }while (n1 > 1);
  45.  
  46. //koniec sortowania
  47.  
  48. int z;
  49. for ( z = 0; z < 20; z++)
  50. {
  51. Console.Write(tab[z]+", ");
  52. }
  53. tab[20] = 1;
  54. int l_w = 0, ilosc = 0;
  55. for (int c = 0; c < 20; c++)
  56. {
  57.  
  58. if (tab[c] == tab[c + 1])
  59. {
  60. l_w++;
  61. }
  62. else
  63. {
  64. l_w--;
  65. if (l_w % 2 == 0)
  66. {
  67. l_w = 0;
  68. ilosc++;
  69. }
  70. else
  71. {
  72. l_w = 0;
  73. }
  74. }
  75. }
  76. Console.WriteLine("Ilosc liczb (b): " + ilosc);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement