Kind of goes without saying the solution should handle any row x$ cc /version
column input:
input 1 1
input 2 20$ mcr []rowcol 2 20
1 3 5 7 9 11 13 15 17 19 21 23 25 27 29 31 33 35 37 39
2 4 6 8 10 12 14 16 18 20 22 24 26 28 30 32 34 36 38 40
Simple enough. But the following 2 requirements take it from trivialSure, sure... if you're a professional C programmer with a BS and 5+
to less trivial!
input = 5 5 23 (cutoff at 23)
1 6 11 16 21
2 7 12 17 22
3 8 13 18 23
4 9 14 19
5 10 15 20
input = 1 10 (no cutoff)$ mcr []rowcol 1 10
1 2 3 4 5 6 7 8 9 10
input = 1 10 3 (cutoff at 3)$ mcr []rowcol 1 10 3
1 2 3
--------------------------------------------------------------------@compile rowcol.c
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' --------------------------------------------------------------------
input = 1 (1 row and 1 column includes 1)
1
input = 2@rowcol 2
not possible to output 1-2 where rows=columns
input = 4 (2 rows and 2 columns includes 4)@rowcol 4
1 3
2 4
input = 5@rowcol 5
not possible to output 1-5 where rows=columns
input = 6$ cc /define=DEBUG=1 rowcol.c
not possible to output 1-6 where rows=columns
input = 9@rowcol 9
1 4 7
2 5 8
3 6 9
input = 10 11 or 12@rowcol 11
not possible to output 1-10/11/12 where rows=columns
input = 25@rowcol 25
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
* Extra Credit if you determine a formula for this requirement. I kindI can't see any. There's probably a better way to do this by putting the col/rows counter stuff in a function but now that I have it working I
of brute-forced it with 2 loops. As you can see, N = prime isn't
enough to know if it can be done.
My code is below.I have not looked until I posted this. Obviously very different. I have
DFS <nospam@dfs.com> writes:
Kind of goes without saying the solution should handle any row x
column input:
Simple enough. But the following 2 requirements take it from trivialSure, sure... if you're a professional C programmer with a BS and 5+
to less trivial!
years experience.
This was a bit above where I am but once I started it
I had to finish it so here I am.
* Extra Credit if you determine a formula for this requirement. I kindI can't see any. There's probably a better way to do this by putting the col/rows counter stuff in a function but now that I have it working I
of brute-forced it with 2 loops. As you can see, N = prime isn't
enough to know if it can be done.
don't want to mess with it.
Glad to be done with this!
@logout
Good afternoon, JAYJWA ...
End of TOPS20:<SYSTEM>LOGOUT.CMD.1
Killed Job 12, User JAYJWA, TTY121 ATR2:/dev/pts/14, at 25-Feb-2026 15:46:17
Used 0:00:00 in 0:52:08
Challenge is to output sequential numbers by column then row:[snip]
Kind of goes without saying the solution should handle any row x column input:[snip]
input rows columns
--------------------------------------------------------------------[snip]
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' --------------------------------------------------------------------
* Extra Credit if you determine a formula for this requirement. I kind
of brute-forced it with 2 loops. As you can see, N = prime isn't
enough to know if it can be done. -----------------------------------------------------------------------
On Thu, 19 Feb 2026 16:55:25 -0500, DFS wrote:
Challenge is to output sequential numbers by column then row:[snip]
Kind of goes without saying the solution should handle any row x column[snip]
input:
input rows columns
--------------------------------------------------------------------[snip]
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'
--------------------------------------------------------------------
* Extra Credit if you determine a formula for this requirement. I kind
of brute-forced it with 2 loops. As you can see, N = prime isn't
enough to know if it can be done.
-----------------------------------------------------------------------
Reasonably trivial. Took me about 20 minutes to get it working.
Hint for the "Extra Credit" part: given the requirement that, when
only "cut-off" specified, rows must equal columns, then the
number of rows and columns are related to the square root of the
cut-off value. And, the partial column can only have between 1
and n_rows values in it. So, invalid cut-off values are easy enough
to compute by using some simple math.
I write more Python than anything (then duplicate the .py programs inPython's great. I did some Java then got distracted by Python then got distracted by C. Since (almost) all of the systems I use have C, C it is
C for exercise).
FYI: your code compiled cleanly 1st time on gccAfter I tested it on Linux, I see it did. Before, when I was building
I notice you stopped at 30x30, I guess because of your monitor?I wanted it to run on TOPS-20, which is headless. That terminal is set
Your name is close to my home state of Jawja.It was my login name in computer class in highschool. It just stuck over
Hint for the "Extra Credit" part: given the requirement that, whenI'm not seeing it. You can cleanly take the square root of 1, 4, and 9
only "cut-off" specified, rows must equal columns, then the
number of rows and columns are related to the square root of the
cut-off value. And, the partial column can only have between 1
and n_rows values in it. So, invalid cut-off values are easy enough
to compute by using some simple math.
Lew Pitcher <lew.pitcher@digitalfreehold.ca> writes:
Hint for the "Extra Credit" part: given the requirement that, whenI'm not seeing it. You can cleanly take the square root of 1, 4, and 9
only "cut-off" specified, rows must equal columns, then the
number of rows and columns are related to the square root of the
cut-off value. And, the partial column can only have between 1
and n_rows values in it. So, invalid cut-off values are easy enough
to compute by using some simple math.
but 6 and 7 yield floats around 2.x. 7 works for the puzzle but 6 does
not.
@rowcol 4
1 3
2 4
@rowcol 5
Not possible to output 1-5 where rows=columns.
@rowcol 6
Not possible to output 1-6 where rows=columns.
@rowcol 7
1 4 7
2 5
3 6
@rowcol 9
1 4 7
2 5 8
3 6 9
@rowcol 1
1
@
On Thu, 26 Feb 2026 13:33:40 -0500, jayjwa wrote:
Lew Pitcher <lew.pitcher@digitalfreehold.ca> writes:
Hint for the "Extra Credit" part: given the requirement that, whenI'm not seeing it. You can cleanly take the square root of 1, 4, and 9
only "cut-off" specified, rows must equal columns, then the
number of rows and columns are related to the square root of the
cut-off value. And, the partial column can only have between 1
and n_rows values in it. So, invalid cut-off values are easy enough
to compute by using some simple math.
but 6 and 7 yield floats around 2.x. 7 works for the puzzle but 6 does
not.
@rowcol 4
1 3
2 4
@rowcol 5
Not possible to output 1-5 where rows=columns.
@rowcol 6
Not possible to output 1-6 where rows=columns.
@rowcol 7
1 4 7
2 5
3 6
@rowcol 9
1 4 7
2 5 8
3 6 9
@rowcol 1
1
@
unsigned int n_rows, n_cols, cut_off;
/* ... */
/* handle standalone "cut_off" value */
n_rows = n_cols = ceil(sqrt(cut_off));
if ( (cut_off < (n_rows * (n_cols-1)) + 1) || (cut_off > (n_rows * n_cols)) )
{
fprintf(stderr,"Cut off value %u not possible where rows=cols\n",cut_off);
/* error handling as required */
}
On Thu, 26 Feb 2026 18:49:41 +0000, Lew Pitcher wrote:[snip]
On Thu, 26 Feb 2026 13:33:40 -0500, jayjwa wrote:
Lew Pitcher <lew.pitcher@digitalfreehold.ca> writes:
Hint for the "Extra Credit" part: given the requirement that, whenI'm not seeing it. You can cleanly take the square root of 1, 4, and 9
only "cut-off" specified, rows must equal columns, then the
number of rows and columns are related to the square root of the
cut-off value. And, the partial column can only have between 1
and n_rows values in it. So, invalid cut-off values are easy enough
to compute by using some simple math.
but 6 and 7 yield floats around 2.x. 7 works for the puzzle but 6 does
not.
@rowcol 4
1 3
2 4
@rowcol 5
Not possible to output 1-5 where rows=columns.
@rowcol 6
Not possible to output 1-6 where rows=columns.
@rowcol 7
1 4 7
2 5
3 6
@rowcol 9
1 4 7
2 5 8
3 6 9
@rowcol 1
1
@
unsigned int n_rows, n_cols, cut_off;
/* ... */
/* handle standalone "cut_off" value */
n_rows = n_cols = ceil(sqrt(cut_off));
if ( (cut_off < (n_rows * (n_cols-1)) + 1) || (cut_off > (n_rows * n_cols)) )
{
fprintf(stderr,"Cut off value %u not possible where rows=cols\n",cut_off);
/* error handling as required */
}
13:53:54 $ cat cutoff.c
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
int main(void)
{
unsigned int n_rows, n_cols, cut_off;
for (cut_off = 1 ; cut_off < 32; ++cut_off)
{
n_rows = n_cols = ceil(sqrt(cut_off));
if ( (cut_off < (n_rows * (n_cols-1)) + 1) || (cut_off > (n_rows * n_cols)) )
printf("Cut off value %u not possible where rows=cols\n",cut_off);
else
printf("Cut off = %u, Rows = %u, Cols = %u\n",cut_off,n_rows,n_cols);
}
return 0;
}
On Thu, 26 Feb 2026 17:06:53 +0000, Lew Pitcher wrote:
On Thu, 19 Feb 2026 16:55:25 -0500, DFS wrote:
Challenge is to output sequential numbers by column then row:[snip]
Kind of goes without saying the solution should handle any row x column[snip]
input:
input rows columns
--------------------------------------------------------------------[snip]
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'
--------------------------------------------------------------------
* Extra Credit if you determine a formula for this requirement. I kind
of brute-forced it with 2 loops. As you can see, N = prime isn't
enough to know if it can be done.
-----------------------------------------------------------------------
Reasonably trivial. Took me about 20 minutes to get it working.
Hint for the "Extra Credit" part: given the requirement that, when
only "cut-off" specified, rows must equal columns, then the
number of rows and columns are related to the square root of the
cut-off value.
And, the partial column can only have between 1
and n_rows values in it. So, invalid cut-off values are easy enough
to compute by using some simple math.
Script done on Thu 26 Feb 2026 12:25:33 PM EST
On Thu, 19 Feb 2026 16:55:25 -0500, DFS wrote:
Challenge is to output sequential numbers by column then row:[snip]
Kind of goes without saying the solution should handle any row x column[snip]
input:
input rows columns
--------------------------------------------------------------------[snip]
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'
--------------------------------------------------------------------
* Extra Credit if you determine a formula for this requirement. I kind
of brute-forced it with 2 loops. As you can see, N = prime isn't
enough to know if it can be done.
-----------------------------------------------------------------------
Reasonably trivial. Took me about 20 minutes to get it working.
Hint for the "Extra Credit" part: given the requirement that, when
only "cut-off" specified, rows must equal columns, then the
number of rows and columns are related to the square root of the
cut-off value. And, the partial column can only have between 1
and n_rows values in it. So, invalid cut-off values are easy enough
to compute by using some simple math.
On Thu, 26 Feb 2026 17:06:53 +0000, Lew Pitcher wrote:
On Thu, 19 Feb 2026 16:55:25 -0500, DFS wrote:
Challenge is to output sequential numbers by column then row:[snip]
Kind of goes without saying the solution should handle any row x column >>> input:[snip]
input rows columns
--------------------------------------------------------------------[snip]
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'
--------------------------------------------------------------------
* Extra Credit if you determine a formula for this requirement. I kind
of brute-forced it with 2 loops. As you can see, N = prime isn't
enough to know if it can be done.
-----------------------------------------------------------------------
Reasonably trivial. Took me about 20 minutes to get it working.
Hint for the "Extra Credit" part: given the requirement that, when
only "cut-off" specified, rows must equal columns, then the
number of rows and columns are related to the square root of the
cut-off value. And, the partial column can only have between 1
and n_rows values in it. So, invalid cut-off values are easy enough
to compute by using some simple math.
/*
** From: DFS <nospam@dfs.com>
** Newsgroups: comp.lang.c
** Subject: Sort of trivial code challenge - may be interesting to you anyway ** Date: Thu, 19 Feb 2026 16:55:25 -0500
** Message-ID: <10n80sc$3soe4$1@dont-email.me>
**
** Challenge is to output sequential numbers by column then row:
** Kind of goes without saying the solution should handle any row x column ** input:
**
** input rows 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'
** --------------------------------------------------------------------
**
** * Extra Credit if you determine a formula for this requirement. I kind
** of brute-forced it with 2 loops. As you can see, N = prime isn't
** enough to know if it can be done.
** -----------------------------------------------------------------------
**
** Solution by Lew Pitcher 2026-02-26
** cc -o rowcol -mtune=native -Wall -std=c99 -pedantic -lm rowcol.c
**
** NB: uses ceil(), sqrt(), and log10() calls from math lib
*/
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <limits.h>
static int StrUint(const char *string, unsigned int *valu);
static void dosquare(unsigned int n_rows, unsigned int n_cols, unsigned int cut_off);
int main(int argc, char *argv[])
{
int status = EXIT_FAILURE,
args_ok = 1;
unsigned int n_rows = 0,
n_cols = 0,
cut_off = 0;
switch (argc)
{
case 4: /* rowcol #rows #cols cutoff */
if ((StrUint(argv[3],&cut_off) == 0) || (cut_off == 0))
{
fprintf(stderr,"Invalid value for cut-off (\"%s\")\n",argv[3]);
args_ok = 0;
}
case 3: /* rowcol #rows #cols */
if ((StrUint(argv[1],&n_rows) == 0) || (n_rows == 0))
{
fprintf(stderr,"Invalid value for # rows (\"%s\")\n",argv[1]);
args_ok = 0;
}
if ((StrUint(argv[2],&n_cols) == 0) || (n_cols == 0))
{
fprintf(stderr,"Invalid value for # columns (\"%s\")\n",argv[2]);
args_ok = 0;
}
break;
case 2: /* rowcol cutoff */
if ((StrUint(argv[1],&cut_off) == 0) || (cut_off == 0))
{
fprintf(stderr,"Invalid value for cut off (\"%s\")\n",argv[1]);
args_ok = 0;
}
else
{
n_rows = n_cols = ceil(sqrt(cut_off));
if ( (cut_off < (n_rows * (n_cols-1)) + 1) )
{--
fprintf(stderr,"Cut off value %u not possible where rows=cols\n",cut_off);
args_ok = 0;
}
}
break;
default:
args_ok = 0; /* default error msg is usage message */
break;
}
if (args_ok)
{
dosquare(n_rows,n_cols,cut_off);
status = EXIT_SUCCESS;
}
else fprintf(stderr,"Usage:\t%s #_rows #_cols [ cut-off ]\nor\t%s cut-off\n",argv[0],argv[0]);
return status;
}
/*
** StrUint() parse string for numeric decimal integer value
** returns 0 on failure,
** 1, valu updated on success
*/
static int StrUint(const char *string, unsigned int *valu)
{
int status = 0;
char *eptr;
unsigned long int result;
result = strtoul(string,&eptr,10);
if ((eptr !=string) && (*eptr == 0) && (result <= UINT_MAX))
{
*valu = result;
status = 1;
}
return status;
}
/*
** dosquare() prints out the specified square, with numbers
** ascending in columns
** Notes:
** 1) square printed with equal field_width, so that all cols
** have same width
** 2) function does not return a value
*/
static void dosquare(unsigned int n_rows, unsigned int n_cols, unsigned int cut_off)
{
unsigned int field_width; /* for equal spacing of output data */
if (cut_off == 0) cut_off = n_rows * n_cols;
field_width = ceil(log10(cut_off)) + 1;
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);
}
puts("");
}
}
| Sysop: | DaiTengu |
|---|---|
| Location: | Appleton, WI |
| Users: | 1,097 |
| Nodes: | 10 (0 / 10) |
| Uptime: | 21:14:06 |
| Calls: | 14,089 |
| Files: | 187,111 |
| D/L today: |
1,311 files (437M bytes) |
| Messages: | 2,490,428 |