Path: ...!news-out.netnews.com!postmaster.netnews.com!us14.netnews.com!not-for-mail X-Trace: DXC=XDO2S7nchUY2BFZQ=P]QnYHWonT5<]0T]Q;nb^V>PUfV=AnO\FUBY[PnF54O@^\1?TKTo[:hc[gi]Wk5\ZZ8Yg0Pnk9`jfQW@>]dDioXmM8L1QOXeKkS2`?jY X-Complaints-To: support@blocknews.net Date: Wed, 12 Jun 2024 18:29:27 -0400 MIME-Version: 1.0 User-Agent: Betterbird (Windows) Subject: Re: "undefined behavior"? Newsgroups: comp.lang.c References: <666a095a$0$952$882e4bbb@reader.netnews.com> Content-Language: en-US From: DFS In-Reply-To: Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 8bit Lines: 383 Message-ID: <666a2146$0$950$882e4bbb@reader.netnews.com> NNTP-Posting-Host: 127.0.0.1 X-Trace: 1718231366 reader.netnews.com 950 127.0.0.1:59703 Bytes: 13460 On 6/12/2024 5:38 PM, David Brown wrote: > On 12/06/2024 22:47, DFS wrote: >> Wrote a C program to mimic the stats shown on: >> >> https://www.calculatorsoup.com/calculators/statistics/descriptivestatistics.php >> >> My code compiles and works fine - every stat matches - except for one >> anomaly: when using a dataset of consecutive numbers 1 to N, all >> values  > 40 are flagged as outliers.  Up to 40, no problem.  Random >> numbers dataset of any size: no problem. >> >> And values 41+ definitely don't meet the conditions for outliers >> (using the IQR * 1.5 rule). >> >> Very strange. >> >> Edit: I just noticed I didn't initialize a char: >> before: char outliers[100]; >> after : char outliers[100] = ""; >> >> And the problem went away.  Reset it to before and problem came back. >> >> Makes no sense.  What could cause the program to go FUBAR at data >> point 41+ only when the dataset is consecutive numbers? >> >> Also, why doesn't gcc just do you a solid and initialize to "" for you? >> > > It is /really/ difficult to know exactly what your problem is without > seeing your C code!  There may be other problems that you haven't seen yet. The outlier section starts on line 169 ===================================================================================== //this code is hereby released to the public domain #include #include #include #include #include /* this program computes the descriptive statistics of a randomly generated set of N integers 1.0 release Dec 2020 2.0 release Jun 2024 used the population skewness and Kurtosis formulas from: https://www.calculatorsoup.com/calculators/statistics/descriptivestatistics.php also test the results of this code against that site compile: gcc -Wall prog.c -o prog -lm usage : ./prog N -option (where N is 2 or higher, and option is -r or -c or -o) -r generates N random numbers -c generates consecutive numbers 1 to N -o generates random numbers with outliers */ //random ints int randNbr(int low, int high) { return (low + rand() / (RAND_MAX / (high - low + 1) + 1)); } //comparator function used with qsort int compareint (const void * a, const void * b) { if (*(int*)a > *(int*)b) return 1; else if (*(int*)a < *(int*)b) return -1; else return 0; } int main(int argc, char *argv[]) { if(argc < 3) { printf("Missing argument:\n"); printf(" * enter a number greater than 2\n"); printf(" * enter an option -r -c or -o\n"); exit(0); } //vars int i=0, lastmode=0; int N = atoi(argv[1]); int nums[N]; double sumN=0.0, median=0.0, Q1=0.0, Q2=0.0, Q3=0.0, IQR=0.0; double stddev = 0.0, kurtosis = 0.0; double sqrdiffmean = 0.0, cubediffmean = 0.0, quaddiffmean = 0.0; double meanabsdev = 0.0, rootmeansqr = 0.0; char mode[100], tmp[12]; //generate random dataset if(strcmp(argv[2],"-r") == 0) { srand(time(NULL)); for(i=0;iumaxcnt) { umaxcnt=ucnt; memset(mode, '\0', sizeof(mode)); sprintf(tmp, "%d ", nums[i]); strcat(mode, tmp); lastmode = nums[i]; } } // median and quartiles // quartiles divide sorted dataset into four sections // Q1 = median of values less than Q2 // Q2 = median of the data set // Q3 = median of values greater than Q2 if(N % 2 == 0) { Q2 = median = (nums[(N/2)-1] + nums[N/2]) / 2.0; ========== REMAINDER OF ARTICLE TRUNCATED ==========