Deutsch English Français Italiano |
<slrnv6ig9v.13pi.candycanearter07@candydeb.host.invalid> 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: candycanearter07 <candycanearter07@candycanearter07.nomail.afraid> Newsgroups: comp.os.linux.advocacy Subject: Re: More Funny Stuff From Joel Date: Wed, 12 Jun 2024 06:35:02 -0000 (UTC) Organization: the-candyden-of-code Lines: 171 Message-ID: <slrnv6ig9v.13pi.candycanearter07@candydeb.host.invalid> References: <17d716103c089ab3$7951$675878$802601b3@news.usenetexpress.com> <ej496jd0tb59u2l0nqtghq3u9ahhmann3s@4ax.com> <lcjnnuF896hU5@mid.individual.net> <kma96j1no1tp926ctejldkdk2c19aeruft@4ax.com> <lcjvk1F9n7aU1@mid.individual.net> <2ej96j1mbvgiok4q5c57vdlo94itpfu5dt@4ax.com> <6664e989$0$2363151$882e4bbb@reader.netnews.com> <slrnv6f9fl.2hg.candycanearter07@candydeb.host.invalid> <66687931$0$3747328$882e4bbb@reader.netnews.com> Injection-Date: Wed, 12 Jun 2024 08:35:03 +0200 (CEST) Injection-Info: dont-email.me; posting-host="a92ee56fe71330095cf9c81c717935bf"; logging-data="1619543"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1++mtzD90V0mxavT/SYs/5hfN4S0FhJ7y1Z9aSd2OHTKA==" User-Agent: slrn/1.0.3 (Linux) Cancel-Lock: sha1:WZB1y01oo57QtMTdwd5uV8W50Lc= X-Face: b{dPmN&%4|lEo,wUO\"KLEOu5N_br(N2Yuc5/qcR5i>9-!^e\.Tw9?/m0}/~:UOM:Zf]% b+ V4R8q|QiU/R8\|G\WpC`-s?=)\fbtNc&=/a3a)r7xbRI]Vl)r<%PTriJ3pGpl_/B6!8pe\btzx `~R! r3.0#lHRE+^Gro0[cjsban'vZ#j7,?I/tHk{s=TFJ:H?~=]`O*~3ZX`qik`b:.gVIc-[$t/e ZrQsWJ >|l^I_[pbsIqwoz.WGA]<D Bytes: 5876 DFS <nospam@dfs.com> wrote at 16:20 this Tuesday (GMT): > On 6/10/2024 10:40 PM, candycanearter07 wrote: >> DFS <nospam@dfs.com> wrote at 23:30 this Saturday (GMT): >>> On 6/8/2024 5:32 PM, Joel wrote: >>> >>>> I can't imagine I'd need anything beyond C. >>> >>> >>> I hope you're not gonna pull a Feeb and claim you're a C guru that can >>> 'move the Earth', but refuse to show ANY decent code, then make lame >>> excuses like "I have other fish to fry". >> >> Seems like a fun challenge.. especially since im kinda rusty with c >> >>> 1) write your own C to sort these 3 names (by last then first): >>> >>> William Thomas >>> Zachary Jones >>> Jim Thomas >> >> I'll be honest, I might be bad but I completely forgot how to sort >> strings.. sorry > > > Usually qsort(). > I knew there was an official implementation! > >>> 2) write your own C to count the words in: >>> >>> "Now is the time for all good men to come to the aid of their country." >> >> #include <stdio.h> >> #include <ctype.h> >> >> const char phrase[] = "Now is the time for all good men to come to the >> aid of their country."; >> >> int main() >> { >> const char *phr = phrase; >> int words = 0; >> int space = 0; // does c have bool? > > Yes. > >> while(*phr) >> { >> if(space && isalpha(*phr)) >> { >> space = 0; >> words++; >> } >> if(isblank(*phr)) space = 1; >> >> phr++; >> } >> >> printf("Words: %d, words); >> } > > > My wordcounter code used: > > if(isspace(myStr[i]) && !isspace(myStr[i+1])) {countWords++;} > > to also count words that might begin with numbers. > > "For the right money cola Linux 'advocates' would work 4MS in 2ms." > Right, I forgot isalnum was a thing. > > >>> 3) write your own C to calc the mean, median and mode of: >>> >>> 1,2,3,4,5,5,6,7,8,9 >> >> #include <stdio.h> >> #define COUNT_CNT 10 >> >> int nums[] = {1, 2, 3, 4, 5, 5, 6, 7, 8, 9, -1}; >> >> int main() >> { >> double mean; >> int median, mode, md_count[COUNT_CNT], len; >> >> for( len = 0; nums[len] != -1; len++ ) >> { >> mean += nums[len]; >> md_count[nums[len]]++; >> } >> mean /= len; >> >> if(len % 2 == 0) >> { >> int mid = len / 2; >> median = nums[mid] + nums[mid + 1]]; >> median /= 2; >> } >> else >> median = nums[len / 2]; >> >> for( int i = 0; i < COUNT_CNT; i++ ) >> { >> if(md_count[mode] < md_count[i]) mode = i; >> } >> >> printf("mean %g, median %d, mode %d", mean, median, mode); >> } >> >> These compiled fine on my machine (gcc 12.2.0) > > > Did you actually run the code? Yes, but I also typed it into slrn by hand bc I was worried that pasting it would mess up word wrapping > I got a segfault (WSL Ubuntu, gcc 11.4). Had to make a few tweaks to > get it to run and provide correct answers. > > Do you want me to tell you what I did, or do you want to figure it out? > > Hint: after the line "mean /= len;" print out the contents of > md_count[]. You should get: > > 1. 1 > 2. 1 > 3. 1 > 4. 1 > 5. 2 > 6. 1 > 7. 1 > 8. 1 > 9. 1 I don't see it sorry ^^: > I like the technique you use for the mode count, but something to > remember is there can be multiple modes, and your code as is will > identify only one. > > And the numbers were already sorted, but if they weren't you would have > to sort them - forward or reverse - before getting the median. Thanks, I'll keep it in mind next time > >>> Why is it I have a VERY strong feeling you and Feeb and C are about to >>> be pwned by python? >> >> >> IMO, both are pretty good languages, with their own strengths..like, C >> is good for dealing with the raw data and it can be really satisfying to >> get everything working, and python is great for throwing something >> together. > > > I like them both, too. But C is too time-consuming for me. I just > don't spend enough time using it exclusively to be productive vs python. Yeah, it can be frustrating sometimes (like how arrays don't really exist and you have to be /very careful/ around them) > Another language I've had my eye on is Julia. Oh, never heard of it. -- user <candycane> is generated from /dev/urandom