Advertisement
Guest User

Untitled

a guest
Apr 24th, 2019
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.24 KB | None | 0 0
  1. #include <iostream>
  2. #include <time.h>
  3. using namespace std;
  4.  
  5. void zadanie1() {
  6. int zad1_tab[4] = { 0, 0, 0, 0 };
  7. float probabilities[] = { 0.1, 0.2, 0.4, 0.3 };
  8. float dystrybuanta[4] = { 0 };
  9. for (int i = 0; i < 4; i++) {
  10. for (int j = 0; j <= i; j++) {
  11. dystrybuanta[i] += probabilities[j];
  12. }
  13. }
  14. for (auto __i = 0; __i < 100000; __i++) {
  15. float random = (double)rand() / (double)RAND_MAX;
  16. for (auto __j = 0; __j < 4; __j++) {
  17. if (random < dystrybuanta[__j]) {
  18. zad1_tab[__j]++;
  19. break;
  20. }
  21. }
  22. }
  23. cout << "Zadanie 1" << endl;
  24. for (auto __j = 0; __j < 4; __j++) {
  25. cout << __j+1 << ": " << zad1_tab[__j] << endl;
  26. }
  27. }
  28.  
  29. float funkcja(float y) {
  30. /*
  31. F(x) = ax+b
  32. F(50) = 0
  33. F(150) = 1
  34. 50a+b = 0
  35. 150a+b = 1
  36. 150a +3b = 0
  37. 150a + b = 1
  38. 2b = -1 => b = -1/2 => 50a = 1/2 => a = 1/100
  39. y = 1/100 * x - 1/2
  40. y + 1/2 =x/100
  41. x = 100(y+1/2) = 100y + 50
  42. */
  43. return 100 * y + 50;
  44. }
  45.  
  46. void zadanie2() {
  47. int zad2_tab[10] = { 0 };
  48. int granice[] = { 60,70,80,90,100,110,120,130,140,150 };
  49. for (auto __it = 0; __it < 100000; __it++) {
  50. float random = (double)rand() / (double)RAND_MAX;
  51. float x = funkcja(random);
  52. for (int __j = 0; __j < 10; __j++) {
  53. if (x < granice[__j]) {
  54. zad2_tab[__j]++;
  55. break;
  56. }
  57. }
  58. }
  59. cout << "Zadanie 2" << endl;
  60. for (auto __j = 0; __j < 10; __j++) {
  61. cout << __j + 1 << ": " << zad2_tab[__j] << endl;
  62. }
  63.  
  64. }
  65.  
  66. float f(float x) {
  67. return 0.0002*x - 0.01;
  68. }
  69.  
  70. void zadanie3() {
  71. /*
  72. a = 100
  73. P = 1
  74. P = ah/2 => h = 2P/a => h = 2*1/100=2/100=1/50
  75. */
  76. float fmax = 0.02;
  77. float d = fmax + fmax * 0.01;
  78. int success = 0;
  79. int zad3_tab[10] = { 0 };
  80. int granice[] = { 60,70,80,90,100,110,120,130,140,150 };
  81. while (success != 100000) {
  82. float y = d * ((double)rand() / (double)RAND_MAX);
  83. float x = ((double)rand() / (double)RAND_MAX) * 100 + 50;
  84. if (f(x) > y) {
  85. success++;
  86. for (int __j = 0; __j < 10; __j++) {
  87. if (x < granice[__j]) {
  88. zad3_tab[__j]++;
  89. break;
  90. }
  91. }
  92. }
  93. }
  94. cout << "Zadanie 3" << endl;
  95. for (auto __j = 0; __j < 10; __j++) {
  96. cout << __j + 1 << ": " << zad3_tab[__j] << endl;
  97. }
  98. }
  99.  
  100. int main()
  101. {
  102. srand(time(NULL));
  103. zadanie1();
  104. zadanie2();
  105. zadanie3();
  106. system("pause");
  107. return 0;
  108. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement