Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <time.h>
- #include <math.h>
- #include <stddef.h>
- float randomazer (float max, float min){ //random numbers from min to max
- float random;
- int i; //because time og program near 1 sec random number isnt differnt
- i=rand(); //i always be same random numbers if dont add srand
- srand(time(NULL)+i);
- random = rand();
- random = (random / RAND_MAX)*(max-min)+min;
- return(random);
- }
- int crossed (float y, float j, float si, float t, float l){
- j= (int)j;
- l= (int)l;
- t= (int)t;
- if (y<(t+t*j) && y>=j*t && ((si * l/2 + y >= t+t*j) || (-si * l/2 + y <= j*t)))
- return 1;
- else
- return 0;
- }
- int main (void){
- float max, min, ratio, pi, p;
- int width, i=0, j, g, high, t, l, n, count_crossed=0; //count_crossed - quantity of crossed needles
- scanf ("%d" "%d" "%d" "%d" "%d", &width, &high, &t, &l, &n);
- float x [n+1]; //location of center of each needl
- float y [n+1];
- float alpha [n+1];
- float si [n+1]; //array of sin of alpha
- for (i=0 ; i<n ; i++){
- max=width;
- x [i] = randomazer (max, 0);
- // printf ("%f ", x [i]);
- max=high;
- y [i] = randomazer (max, 0);
- // printf ("%f ", y [i]);
- alpha [i] = randomazer (3.14, 0);
- // printf ("%f\n", alpha [i]);
- si [i] = sin (alpha [i]);
- for (j=0; j<high/t; j++){ //for every section
- count_crossed+=crossed (y[i], j, si [i], t, l);
- }
- // printf ("%d\n", count_crossed);
- }
- printf("Crossed lines : ");
- printf("%d\n", count_crossed);
- printf("Between lines : ");
- printf("%d\n", n-count_crossed);
- ratio=(float)count_crossed/(n-count_crossed);
- printf("Ratio : ");
- printf("%f\n", ratio);
- p= (float)count_crossed/n;
- pi= 2 * (float)l / (t*p);
- printf("PI : ");
- printf("%f\n", pi);
- return 0;
- }
Add Comment
Please, Sign In to add comment