Advertisement
Guest User

Untitled

a guest
Apr 18th, 2015
191
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.10 KB | None | 0 0
  1. #include <cstdio>
  2. #include <cstdlib>
  3. #include <conio.h>
  4. #include <windows.h>
  5. #include <ctime>
  6. #include <math.h>
  7. #include <iostream>
  8. #include <iomanip>
  9.  
  10. using namespace std;
  11.  
  12. long double f(int x) {
  13. // Wartosc pi z pliku math.h: M_PI.
  14. return (1/(sqrt(2*M_PI)))*exp(-(pow(x,2))/2);
  15. }
  16.  
  17.  
  18. int main()
  19. {
  20. int maximum=4;
  21. int minimum=-4;
  22.  
  23. double x=0;
  24. int j=0;
  25.  
  26. float y=0;
  27. float pi=3.14;
  28. long double fmax=0;
  29. long double fxi=0;
  30. int d=0;
  31.  
  32.  
  33. int tablica [9];
  34. int z=0;
  35. for (z=0; z <9; ++z){
  36. tablica[z]=0;
  37. }
  38.  
  39. fmax=f(0);
  40. cout <<"f(0)="<<fmax<< endl;
  41.  
  42. srand(time( NULL ));
  43.  
  44. for( int i = 1; i <= 10000; )
  45. {
  46. x= minimum+rand()% (maximum-(minimum)+1);
  47. fxi=f(x);
  48. y=(((double)(rand())/RAND_MAX)*fmax);
  49.  
  50.  
  51. cout << i << ": " << "y = " <<setprecision(3) << y << ", x = " << x << ", fxi = " <<setprecision(3) << fxi;
  52.  
  53.  
  54. if (y <= fxi) {
  55. cout << " - OK";
  56. i++;
  57. z=x+4;
  58. tablica[z]++;
  59. }
  60. cout << endl;
  61. }
  62.  
  63. for(z=0;z<9;++z){
  64. printf("%d\n",tablica[z]);
  65. }
  66.  
  67. return 0;
  68. getch();
  69.  
  70.  
  71. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement