Guest User

Untitled

a guest
Jun 24th, 2018
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.51 KB | None | 0 0
  1. #include<stdio.h>
  2. #include<stdlib.h>
  3. #include<math.h>
  4. #include<time.h>
  5. #define MAXTETA M_PI/2
  6. double frominput(double min,double max);
  7. double randreal(double min, double max);
  8.  
  9. int main(){
  10. srand48(time(0));
  11.  
  12. double l,d,n,teta,x,i,S,pi;
  13.  
  14. printf("Dammi l ");
  15. l=frominput(0,5);
  16. printf("Dammi d ");
  17. d=frominput(0,5);
  18. printf("Dammi n ");
  19. n=frominput(0,10000);
  20.  
  21. printf("l %lf,d %lf,n %lf",l,d,n);
  22.  
  23. for(i=0;i<1000;i++){
  24. for(i=0;i<n;i++){
  25. teta=randreal(0,MAXTETA);
  26. x=randreal(0,d/2);
  27. if(x<(l/2)*sin(teta)){
  28. S+=1;
  29. }
  30. }
  31. printf("\nS vale %lf \n",&S);
  32. pi=(2*l*n)/S*d;
  33. printf("pi vale %lf\n",pi);
  34.  
  35. FILE*output;
  36. output=fopen("pi.dat","w");
  37. if(output){
  38. fprintf(output,"vale %lf\n",pi);
  39. }
  40.  
  41. }
  42. return 0;
  43. }
  44.  
  45.  
  46.  
  47.  
  48. double frominput(double min,double max){
  49. double x,l=0,d=0;
  50.  
  51. do{
  52. printf("compreso fra [%lf,%lf]\n",min,max);
  53. scanf("%lf",&x);
  54. }while(x<min || x>max);
  55.  
  56. return x;
  57. }
  58.  
  59. double randreal(double min, double max){
  60. double range = (max - min);
  61. double denom = RAND_MAX / range;
  62.  
  63. return min + (lrand48() / denom);
  64.  
  65. // solo a=min compreso randmax+1.solo b=max compreso randmax+1. e lrand+1. nessuno compreso rand+2. lrand+1.
  66. }
Add Comment
Please, Sign In to add comment