Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<stdio.h>
- #include<stdlib.h>
- #include<math.h>
- #include<time.h>
- #define MAXTETA M_PI/2
- double frominput(double min,double max);
- double randreal(double min, double max);
- int main(){
- srand48(time(0));
- double l,d,n,teta,x,i,S,pi;
- printf("Dammi l ");
- l=frominput(0,5);
- printf("Dammi d ");
- d=frominput(0,5);
- printf("Dammi n ");
- n=frominput(0,10000);
- printf("l %lf,d %lf,n %lf",l,d,n);
- for(i=0;i<1000;i++){
- for(i=0;i<n;i++){
- teta=randreal(0,MAXTETA);
- x=randreal(0,d/2);
- if(x<(l/2)*sin(teta)){
- S+=1;
- }
- }
- printf("\nS vale %lf \n",&S);
- pi=(2*l*n)/S*d;
- printf("pi vale %lf\n",pi);
- FILE*output;
- output=fopen("pi.dat","w");
- if(output){
- fprintf(output,"vale %lf\n",pi);
- }
- }
- return 0;
- }
- double frominput(double min,double max){
- double x,l=0,d=0;
- do{
- printf("compreso fra [%lf,%lf]\n",min,max);
- scanf("%lf",&x);
- }while(x<min || x>max);
- return x;
- }
- double randreal(double min, double max){
- double range = (max - min);
- double denom = RAND_MAX / range;
- return min + (lrand48() / denom);
- // solo a=min compreso randmax+1.solo b=max compreso randmax+1. e lrand+1. nessuno compreso rand+2. lrand+1.
- }
Add Comment
Please, Sign In to add comment