delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/2000/06/27/13:57:08

From: "Edmund Horner" <edmund1 AT geocities DOT com>
Newsgroups: comp.os.msdos.djgpp
References: <3958A5A3 DOT D470EC39 AT uni-bremen DOT de>
Subject: Re: Problem with pointer(?)
Lines: 69
X-Priority: 3
X-MSMail-Priority: Normal
X-Newsreader: Microsoft Outlook Express 5.00.2919.6600
X-MimeOLE: Produced By Microsoft MimeOLE V5.00.2919.6600
Organization: Paradise Net Ltd. Customer
Message-ID: <962115507.394420@shelley.paradise.net.nz>
Cache-Post-Path: shelley.paradise.net.nz!unknown AT 203-79-65-99 DOT tnt8 DOT paradise DOT net DOT nz
X-Cache: nntpcache 2.4.0b2 (see http://www.nntpcache.org/)
Date: Wed, 28 Jun 2000 02:18:49 +1200
NNTP-Posting-Host: 203.96.152.26
X-Complaints-To: newsadmin AT xtra DOT co DOT nz
X-Trace: news.xtra.co.nz 962115525 203.96.152.26 (Wed, 28 Jun 2000 02:18:45 NZST)
NNTP-Posting-Date: Wed, 28 Jun 2000 02:18:45 NZST
To: djgpp AT delorie DOT com
DJ-Gateway: from newsgroup comp.os.msdos.djgpp
Reply-To: djgpp AT delorie DOT com

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 -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019