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: Tue, 03 Mar 2026 16:23:54 -0800
Organization: A noiseless patient Spider
Lines: 72
Message-ID: <86y0k8cuhx.fsf@linuxsc.com>
References: <10n80sc$3soe4$1@dont-email.me> <86v7feei2e.fsf@linuxsc.com> <10o6vdo$23hp3$3@dont-email.me>
MIME-Version: 1.0
Content-Type: text/plain; charset=us-ascii
Injection-Date: Wed, 04 Mar 2026 00:24:01 +0000 (UTC)
Injection-Info: dont-email.me; posting-host="de0045d7c780f08ea604ccdb8e034471"; logging-data="2621457"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX18Z9M8OLNxVM4XcuqlvlSw0fQLgQ3M7pxs="
User-Agent: Gnus/5.11 (Gnus v5.11) Emacs/22.4 (gnu/linux)
Cancel-Lock: sha1:n4+SGd1uogFzCFtROJ7QB55ZCNY= sha1:xkKxfELh461s87QxQhHq3xmYVRM=
Xref: csiph.com comp.lang.c:396746
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.
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.)