delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/2002/03/15/09:00:04

X-Authentication-Warning: delorie.com: mailnull set sender to djgpp-bounces using -f
From: "james watmuff" <ammojamo AT hotmail DOT com>
Newsgroups: comp.os.msdos.djgpp
Subject: help please! =)
Lines: 73
X-Priority: 3
X-MSMail-Priority: Normal
X-Newsreader: Microsoft Outlook Express 5.00.2615.200
X-MimeOLE: Produced By Microsoft MimeOLE V5.00.2615.200
Message-ID: <T0nk8.3138$523.247408@ozemail.com.au>
NNTP-Posting-Host: 210-80-141-205.adam.com.au
X-Trace: ozemail.com.au 1016200371 210.80.141.205 (Sat, 16 Mar 2002 00:52:51 EST)
NNTP-Posting-Date: Sat, 16 Mar 2002 00:52:51 EST
Organization: OzEmail Ltd, Australia
Distribution: world
Date: Sat, 16 Mar 2002 00:21:36 +1030
To: djgpp AT delorie DOT com
DJ-Gateway: from newsgroup comp.os.msdos.djgpp
Reply-To: djgpp AT delorie DOT com

hi people,

im having some problems with a fairly simple function (shown below) that is
generating the right value and assigning it to the variable 'expected',
however when the function returns 'expected' to the function that called it
(in my program, main() ) it does not seem to be returning the value... and
to be even more bizarre, when i comment out the 'free(weight_of_x)' line,
the function returns 11.0, but otherwise it returns 0.0

any ideas as to what is going on???

thanks in advance,

James


here's the problem code:
================

double expected_of_x(int *x, int *freq, int size)
{
    int i,sum_freq;
    double *weight_of_x;
    double expected;

    weight_of_x=(double *)malloc(sizeof(double)*size);
    sum_freq=sum_array(freq,size);
//    printf("\nsumfreq %i",sum_freq);

    for(i=0;i<size;i++)
    {
        weight_of_x[i]=((double)freq[i]/(double)sum_freq)*(double)x[i];
//        printf("\nwox[%i]:%f",i,weight_of_x[i]);
    }

    expected=sum_array_double(weight_of_x,size);
    printf("\n%f",expected);     // this prints the CORRECT value
    free(weight_of_x);
    printf("\n%f\n",expected); // this prints the CORRECT value

    return expected;
}


and the main() function that calls expected_of_x():
===========================
#define SIZE 256

main(int argc, char **argv)
{
   int *diff_freq,diff[SIZE],i,j;
   double expected_diff;
   FILE *fp;

   for(i=0;i<SIZE;i++) diff[i]=i;

   for(j=1;j<argc;j++)
   {
       fp=fopen(argv[j],"rb");

       diff_freq=difference_histogram(fp);
       expected_diff=expected_of_x(diff,diff_freq,SIZE);
       printf("%lf\n",expected_diff);     // this prints some WIERD value

       free(diff_freq); // memory is allocated with malloc() to diff_freq in
the difference_histogram() function
       fclose(fp);
    }
}




- Raw text -


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