Mail Archives: djgpp/2000/06/27/13:57:08
I have compiled at looked at your program, and it was an interesting case.
It appeared to be failing at the first call to printf() !
The problem turns out to be in a place that I was intuitively uncomfortable
with: your use of pow(MAX,2) instead of MAX * MAX. You really should rely
on floating point function to determine the size of a block of memory!
pow(), once int'ized, returned the equivilent of MAX * MAX - 1, so that
explains it. You were allocating one less item than you needed to.
Let that be a lesson to all of us!
Edmund.
"Uwe Sydow" <sydow AT uni-bremen DOT de> wrote in message
news:3958A5A3 DOT D470EC39 AT uni-bremen DOT de...
> Hello,
> I like to calculate a gaussian distribution of numbers
> defined by mean and variance on a MAX x MAX grid.
> What I don't understand is, for some values of MAX (eg 100) the
> programm works, for eg MAX =10 it doesen't.
> What did I make wrong?
> Thanks a lot for help
>
> Uwe
>
> /* prog */
> #include<stdio.h>
> #include<math.h>
> #include<stdlib.h>
> #include<time.h>
> #define MAX 100 /* 100 is ok, but 10 or 11 crashes ?? */
> int main(void)
>
> double dd=4.0; /* mean */
> double da=1.0; /* variance */
> short Zh;
> int i,j;
> double (*tg)[MAX] = calloc(pow(MAX,2), sizeof(double));
> double Z0,Z1,Z2,Z3;
> srand(time(0));
> for (i=0;i<MAX;i++) {
> for (j=0;j<MAX;j++) {
> Zh=0;
> while(Zh==0) {
> Z0=(double)rand()/RAND_MAX;
> Z1= 2 * dd * Z0;
> Z2=(1/sqrt(2*PI*pow(da,2)))*exp(-pow((Z1-dd),2))/(2*pow(da,2));
> Z3=(double)rand()/RAND_MAX;
> if( Z2>=Z3 )
> tg[i][j]=Z1;
> Zh=1;
> } /* end if */
> } /* while */
> } /* for j */
> } /* for i */
>
> for (i=0;i<MAX;i++) { /* print result */
> printf("\n");
> for (j=0;j<MAX;j++) {
> printf("%.4f\t",tg[i][j]);
> } /* for j */
> } /* for i */
> cfree(tg);
> exit(0);
> }
> /* end of prog */
- Raw text -