Path: csiph.com!eternal-september.org!feeder.eternal-september.org!nntp.eternal-september.org!.POSTED!not-for-mail From: Tim Rentsch Newsgroups: comp.lang.c Subject: Re: Sort of trivial code challenge - may be interesting to you anyway Date: Wed, 04 Mar 2026 09:38:16 -0800 Organization: A noiseless patient Spider Lines: 104 Message-ID: <86h5qvcx6f.fsf@linuxsc.com> References: <10n80sc$3soe4$1@dont-email.me> <86v7feei2e.fsf@linuxsc.com> <10o6vdo$23hp3$3@dont-email.me> <86y0k8cuhx.fsf@linuxsc.com> <10o9j90$313fd$1@dont-email.me> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Injection-Date: Wed, 04 Mar 2026 17:38:23 +0000 (UTC) Injection-Info: dont-email.me; posting-host="de0045d7c780f08ea604ccdb8e034471"; logging-data="3285793"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+9JYRsYRJpiYNf5kFd/dxbaLqlmRXIkjA=" User-Agent: Gnus/5.11 (Gnus v5.11) Emacs/22.4 (gnu/linux) Cancel-Lock: sha1:i0GwU00uMxFy4fVgqCkNhGxKm8s= sha1:83CpIt2hOccPYJuKH3lbIQdTfFM= Xref: csiph.com comp.lang.c:396765 Lew Pitcher writes: > On Tue, 03 Mar 2026 16:23:54 -0800, Tim Rentsch wrote: > >> Lew Pitcher writes: >> >>> On Mon, 02 Mar 2026 00:44:57 -0800, Tim Rentsch wrote: >>> >>>> DFS writes: >>>> >>>>> Challenge is to output sequential numbers by column then row: >>>>> >>>>> 1 6 11 16 21 >>>>> 2 7 12 17 22 >>>>> 3 8 13 18 23 >>>>> 4 9 14 19 24 >>>>> 5 10 15 20 25 >>>> >>>> [...] >>>> >>>>> Simple enough. But the following 2 requirements take it from trivial >>>>> to less trivial! >>>>> >>>>> -------------------------------------------------------------------- >>>>> 1) must be able to cut the output off at any arbitrary value >>>>> lower than rows x columns >>>>> -------------------------------------------------------------------- >>>> >>>> [...] >>>> >>>>> -------------------------------------------------------------------- >>>>> 2) if you don't specify rows and columns, your solution must try >>>>> to calculate them to form a square (same # of rows and columns) >>>>> that includes only 1 to N. >>>>> >>>>> If rows=columns can't be calculated, return message 'not possible' >>>>> -------------------------------------------------------------------- >>>> >>>> A straightfoward exercise. Here is a counter challenge to make >>>> things more interesting: as above, but don't use nested loops >>>> (or goto's, etc). >>> >>> So, instead of my >>> for (unsigned int row = 0; row < n_rows; ++row) >>> { >>> for (unsigned int col = 0; col < n_cols; ++col) >>> { >>> unsigned int valu = 1 + row + (col * n_rows); >>> if (valu <= cut_off) >>> printf(" %*u",field_width,valu); >>> } >>> putchar('\n'); >>> } >>> use... >>> int max = n_rows * n_cols; >>> >>> for (unsigned int count = 0; count < max; ++count) >>> { >>> unsigned int row, col, valu; >>> >>> col = count % n_cols; >>> row = count / n_cols; >>> valu = 1 + row + (col * n_rows); >>> >>> if (valu <= cut_off) printf(" %*u",field_width,valu); >>> >>> if (col == n_cols-1) putchar('\n'); >>> } >> >> Right. Except for minor differences in spacing, this code >> gives the same output as your original. > > True enough. I've been fiddling with the column alignment; > the code I originally posted kept DFS' example left alignment, > but I prefer right alignment of numbers in columnar format. Yeah, me too. Incidentally, I used a different way to determine the field width to use, something like this: int field_width = snprintf( 0, 0, "%u", cutoff ); > The minor differences in spacing come from me not remembering > which version I posted. Mea culpa. The change in spacing didn't bother me, I mentioned it only out of a penchant for accuracy in my writing. >> One glitch: when the cutoff is less than the number of >> rows, this code behaves differently than what DFS prescribes, >> as can be seen by an example in his original posting. (The >> original code also.) > > Right. I noticed that as well, but too late. I've modified my > code to conform to the "no blank lines" rule. However, I have > mutated the code from the original that I presented here, > what with conditional compilation and all, and will post it later. > My current code does not satisfy the later counter-challenge of > avoiding for()/do/do while()/goto/ and recursion. That's a > challenge that I have to think on ;-) That isn't hard once you see the basic technique, but it has its own set of challenges that I think make it interesting. I'm looking forward to seeing your solution. My first version is kind of ugly, probably I will rewrite it before posting.