Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <time.h>
- #include <stdbool.h>
- double Losowa();
- double PoliczPI(int liczbaLosowan);
- int main()
- {
- printf("Krzysztof Dabrowski gr. 1I1\nProjekt obliczajacy liczbe PI metoda \"orzel-reszka\"\n-----------------------------------\n\n");
- srand(time(0)); //Ustawienie zarodka losowania od aktualnego czasu
- // int liczba = 200;
- // int czesc = liczba/10;
- //
- // for (int i=1; i<=liczba; i++)
- // {
- // if(i % czesc == 0)
- // printf("%d\n", i);
- // }
- printf("Podaj liczbe losowan\n");
- int liczbaLosowan;
- if (scanf("%d", &liczbaLosowan) != 1)
- {
- printf("Nieprawidlowa liczba!\n");
- return 1;
- }
- printf("%.10f\n", PoliczPI(liczbaLosowan));
- return 0;
- }
- double Losowa()
- {
- return (double)rand() / (double)RAND_MAX ;
- }
- double PoliczPI(int liczbaLosowan)
- {
- int wKole=0;
- for (int i=0; i<liczbaLosowan; i++)
- {
- double x, y;
- x = Losowa();
- y = Losowa();
- if(x*x + y*y <= 1)
- wKole++;
- }
- return 4*((double)wKole/liczbaLosowan);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement