| Deutsch English Français Italiano |
|
<v4d4hm$1rjc5$1@dont-email.me> View for Bookmarking (what is this?) Look up another Usenet article |
Path: ...!weretis.net!feeder8.news.weretis.net!eternal-september.org!feeder3.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail From: Janis Papanagnou <janis_papanagnou+ng@hotmail.com> Newsgroups: comp.lang.c Subject: Re: "undefined behavior"? Date: Wed, 12 Jun 2024 23:38:55 +0200 Organization: A noiseless patient Spider Lines: 44 Message-ID: <v4d4hm$1rjc5$1@dont-email.me> References: <666a095a$0$952$882e4bbb@reader.netnews.com> MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 7bit Injection-Date: Wed, 12 Jun 2024 23:39:02 +0200 (CEST) Injection-Info: dont-email.me; posting-host="35d424470b21418f2e04583a4fa0825a"; logging-data="1953157"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX18H2Po7ah+6FmC0m2PYWy+u" User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:45.0) Gecko/20100101 Thunderbird/45.8.0 Cancel-Lock: sha1:KD17A0teHyzt762NeP1AfUO3aDE= X-Enigmail-Draft-Status: N1110 In-Reply-To: <666a095a$0$952$882e4bbb@reader.netnews.com> Bytes: 2765 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? Yeah, I had a similar problem like you; I had a declaration char answer[100]; and was surprised that it wasn't initialized with "42". Seriously; why do you expect [in C] a declaration to initialize that stack object? (There are other languages that do initializations as the language defines it, but C doesn't; it may help to learn before programming in any language?) And why do you think that "" would be an appropriate initialization (i.e. a single '\0' character) and not all 100 elements set to '\0'? (Someone else might want to access the element 'answer[99]'.) And should we pay for initializing 1000000000 characters in case one declares an appropriate huge array? Janis