Advertisement
Siwy_Krzysiek

Policz PI

Oct 19th, 2017
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.14 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <time.h>
  4. #include <stdbool.h>
  5.  
  6. double Losowa();
  7. double PoliczPI(int liczbaLosowan);
  8.  
  9. int main()
  10. {
  11.     printf("Krzysztof Dabrowski gr. 1I1\nProjekt obliczajacy liczbe PI metoda \"orzel-reszka\"\n-----------------------------------\n\n");
  12.  
  13.     srand(time(0)); //Ustawienie zarodka losowania od aktualnego czasu
  14.  
  15. //    int liczba = 200;
  16. //    int czesc = liczba/10;
  17. //
  18. //    for (int i=1; i<=liczba; i++)
  19. //    {
  20. //        if(i % czesc == 0)
  21. //            printf("%d\n", i);
  22. //    }
  23.  
  24.     printf("Podaj liczbe losowan\n");
  25.     int liczbaLosowan;
  26.     if (scanf("%d", &liczbaLosowan) != 1)
  27.     {
  28.         printf("Nieprawidlowa liczba!\n");
  29.         return 1;
  30.     }
  31.  
  32.  
  33.     printf("%.10f\n", PoliczPI(liczbaLosowan));
  34.  
  35.     return 0;
  36. }
  37.  
  38. double Losowa()
  39. {
  40.     return (double)rand() / (double)RAND_MAX ;
  41. }
  42.  
  43. double PoliczPI(int liczbaLosowan)
  44. {
  45.     int wKole=0;
  46.  
  47.     for (int i=0; i<liczbaLosowan; i++)
  48.     {
  49.         double x, y;
  50.         x = Losowa();
  51.         y = Losowa();
  52.  
  53.         if(x*x + y*y <= 1)
  54.             wKole++;
  55.     }
  56.  
  57.     return 4*((double)wKole/liczbaLosowan);
  58. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement