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

From: Uwe Sydow <sydow AT uni-bremen DOT de>
Newsgroups: comp.os.msdos.djgpp
Subject: Problem with pointer(?)
Date: Tue, 27 Jun 2000 15:01:23 +0200
Organization: =?iso-8859-1?Q?Universit=E4t?= Bremen
Lines: 51
Message-ID: <3958A5A3.D470EC39@uni-bremen.de>
NNTP-Posting-Host: caesar.chemie.uni-bremen.de
Mime-Version: 1.0
X-Mailer: Mozilla 4.7 [en] (Win95; I)
X-Accept-Language: en
To: djgpp AT delorie DOT com
DJ-Gateway: from newsgroup comp.os.msdos.djgpp
Reply-To: djgpp AT delorie DOT com

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