| gallu (1) | |
|
i tried to creat a project and i have the main function: ---------------------------------------------------------------------------------------------------------------------------------------------------------------- #include <cmath> #include <iostream> #include "lib.h" using namespace std; // Here we define various functions called by the main program // this function defines the function to integrate double func(double x); // Main function begins here int main() { int i, n; long idum; double crude_mc, x, sum_sigma, fx, variance, exact; printf("Read in the number of Monte-Carlo samples\n"); scanf("%d", &n); crude_mc = sum_sigma=0. ; idum=-1 ; exact=acos(-1.); // evaluate the integral with a crude Monte-Carlo method for ( i = 1; i <= n; i++){ x=ran0(&idum); fx=func(x); crude_mc += fx; sum_sigma += fx*fx; } crude_mc = crude_mc/((double) n ); sum_sigma = sum_sigma/((double) n ); variance=sum_sigma-crude_mc*crude_mc; // final output printf("%d variance= %12.5E Inum= %12.5E pi= %12.5E\n", n, variance, crude_mc, exact); return 0; } // end of main program // this function defines the function to integrate double func(double x) { double value; value = 4/(1.+x*x); return value; } // end of function to evaluate ---------------------------------------------------------------------------------------------------------------------------------------------------------------- and the function ran0() which must be called in the main function: ---------------------------------------------------------------------------------------------------------------------------------------------------------------- /* ** The function ** ran0() ** is an "Minimal" random number generator of Park and Miller ** Set or reset the input value ** idum to any integer value (except the unlikely value MASK) ** to initialize the sequence; idum must not be altered between ** calls for sucessive deviates in a sequence. ** The function returns a uniform deviate between 0.0 and 1.0. */ double ran0(long &idum) { const int a = 16807, m = 2147483647, q = 127773; const int r = 2836, MASK = 123459876; const double am = 1./m; long k; double ans; idum ^= MASK; k = (*idum)/q; idum = a*(idum - k*q) - r*k; // add m if negative difference if(idum < 0) idum += m; ans=am*(idum); idum ^= MASK; return ans; } // End: function ran0() ---------------------------------------------------------------------------------------------------------------------------------------------------------------- when i try to compiille my code i gott error so if some can try that or give me an ideas? | |
|
|
|
| soranz (472) | |
|
Hi, use code tags please :D You won't actually need a pointer in ran0()... http://cplusplus.com/doc/tutorial/pointers/ | |
|
|
|