SHARE
TWEET

4.1 (about needls)

allekco Oct 15th, 2017 21 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <time.h>
  4. #include <math.h>
  5. #include <stddef.h>
  6.  
  7. float randomazer (float max, float min){ //random numbers from min to max
  8.     float random;
  9.     int i; //because time og program near 1 sec random number isnt differnt
  10.     i=rand(); //i always be same random numbers if dont add srand
  11.     srand(time(NULL)+i);
  12.     random = rand();
  13.     random = (random / RAND_MAX)*(max-min)+min;
  14.     return(random);
  15. }
  16.  
  17. int crossed (float y, float j, float si, float t, float l){
  18.     j= (int)j;
  19.     l= (int)l;
  20.     t= (int)t;
  21.     if (y<(t+t*j) && y>=j*t && ((si * l/2 + y >= t+t*j) || (-si * l/2 + y <= j*t)))
  22.         return 1;  
  23.     else
  24.         return 0;
  25. }
  26.  
  27. int main (void){
  28.     float max, min, ratio, pi, p;
  29.     int width, i=0, j, g, high, t, l, n, count_crossed=0; //count_crossed - quantity of crossed needles
  30.     scanf ("%d" "%d" "%d" "%d" "%d", &width, &high, &t, &l, &n);
  31.     float x [n+1]; //location of center of each needl
  32.     float y [n+1];
  33.     float alpha [n+1];
  34.     float si [n+1]; //array of sin of alpha
  35.    
  36.     for (i=0 ; i<n ; i++){
  37.         max=width;
  38.         x [i] = randomazer (max, 0);
  39.     //  printf ("%f  ", x [i]);
  40.        
  41.         max=high;
  42.         y [i] = randomazer (max, 0);
  43.     //  printf ("%f  ", y [i]);
  44.        
  45.         alpha [i] = randomazer (3.14, 0);
  46.     //  printf ("%f\n", alpha [i]);
  47.         si [i] = sin (alpha [i]);
  48.        
  49.         for (j=0; j<high/t; j++){  //for every section 
  50.         count_crossed+=crossed (y[i], j, si [i], t, l);
  51.         }
  52.     //  printf ("%d\n", count_crossed);          
  53.     }
  54.    
  55.     printf("Crossed lines : ");
  56.     printf("%d\n", count_crossed);
  57.    
  58.     printf("Between lines : ");
  59.     printf("%d\n", n-count_crossed);
  60.    
  61.     ratio=(float)count_crossed/(n-count_crossed);
  62.     printf("Ratio : ");
  63.     printf("%f\n", ratio);
  64.    
  65.     p= (float)count_crossed/n;
  66.     pi= 2 * (float)l / (t*p);
  67.     printf("PI : ");
  68.     printf("%f\n", pi);
  69.    
  70.     return 0;
  71. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top