home bbs files messages ]

Forums before death by AOL, social media and spammers... "We can't have nice things"

   comp.lang.c      Meh, in C you gotta define EVERYTHING      243,370 messages   

[   << oldest   |   < older   |   list   |   newer >   |   newest >>   ]

   Message 243,369 of 243,370   
   Lew Pitcher to Lew Pitcher   
   Re: Sort of trivial code challenge - may   
   06 Mar 26 17:48:51   
   
   From: lew.pitcher@digitalfreehold.ca   
      
   On Fri, 06 Mar 2026 17:38:04 +0000, Lew Pitcher wrote:   
      
   > On Fri, 06 Mar 2026 10:36:19 -0500, DFS wrote:   
   >   
   >> On 2/26/2026 2:34 PM, Lew Pitcher wrote:   
   >>   
   >>   
   >>> 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("");   
   >>>    }   
   >>> }   
   >>   
   >>   
   >> I found a nit:   
   >>   
   >> with 1-column inputs like 10 1 3  or 100 1 25, your program prints (row   
   >> - cutoff) blank lines after the cutoff.   
   > [snip]   
   >   
   > Yes, I'm aware :-)   
   >   
   > I fixed it in a later version, which I have not posted yet.   
      
   And, here it is...   
      
   /*   
   ** From: DFS    
   ** 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   
   ** --------------------------------------------------------------------   
   ** 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'   
   ** --------------------------------------------------------------------   
   **   
   ** * 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.   
   ** -----------------------------------------------------------------------   
   **   
   ** From: Tim Rentsch    
   ** Newsgroups: comp.lang.c   
   ** Subject: Re: Sort of trivial code challenge - may be interesting to you   
   anyway   
   ** Date: Mon, 02 Mar 2026 00:44:57 -0800   
   ** Message-ID: <86v7feei2e.fsf@linuxsc.com>   
   **   
   ** Here is a counter challenge to make things more interesting:  as above,   
   ** but don't use nested loops (or goto's, etc).   
   **   
   ** -----------------------------------------------------------------------   
   **   
   ** From: Michael S    
   ** Newsgroups: comp.lang.c   
   ** Subject: Re: Sort of trivial code challenge - may be interesting to you   
   anyway   
   ** Date: Mon, 2 Mar 2026 11:07:20 +0200   
   ** Message-ID: <20260302110720.00007698@yahoo.com>   
   **   
   ** Another counter challenge could have been to commpletely avoid loops/goto.   
   **   
   ** ==========================================================================   
   **   
   ** DFS challenge; solution by Lew Pitcher 2026-02-26   
   ** cc -o rowcol_CLC -mtune=native -Wall -std=c99 -pedantic -lm rowcol_CLC.c   
   **   
   ** Tim Rentsch counter-challenge; solution by Lew Pitcher 2026-03-03   
   ** cc -o rowcol_CLC -mtune=native -Wall -std=c99 -pedantic -lm -   
   NO_NESTED_LOOPS rowcol_CLC.c   
   **   
   ** Michael S counter-challenge; solution by Lew Pitcher 2026-03-03   
   ** cc -o rowcol_CLC -mtune=native -Wall -std=c99 -pedantic -lm -DNO_LOOPS   
   rowcol_CLC.c   
   **   
   ** NB: solutions use ceil(), sqrt(), and log10() calls from math lib   
   **   
   ** This code released into the public domain. Lew Pitcher, 2026-03-04   
   */   
      
   #include    
   #include    
   #include    
   #include    
      
      
   /*   
   ** define RIGHT_ALIGN to right-align all numeric results   
   ** otherwise, numeric results will align to the left   
   */   
   #ifdef RIGHT_ALIGN   
   #define FORMAT "%*u"   
   #else   
   #define FORMAT "%-*u"   
   #endif   
      
   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;   
         }   
      
         /* cut-off value must occur within table bounds */   
         if (cut_off)   
         {   
   	if (cut_off > n_rows * n_cols)   
   	{   
   	  fprintf(stderr,"cut off value %u not found in table\n",cut_off);   
   	  args_ok = 0;   
   	}   
         }   
         else cut_off = n_rows * n_cols;   
      
         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   
         {   
   	/*   
   	** Compute n_rows and n_cols, and validate that cut_off   
   	** value occurs as last entry in final column   
   	**   
   	** ----- Narrative -----   
   	** As problem definition states that, for this configuration,   
   	** n_rows must equal n_cols, and that n_rows * n_cols must   
   	** include the cut_off value, we first approximate the value   
   	** of n_rows and n_cols by taking the square root of cut_off   
   	** and rounding that value up to the nearest integer. This   
   	** guarantees that n_rows * n_cols is the smallest square   
   	** that will contain the cut_off value.   
   	**   
   	** But, this rounded value might be too big to place cut_off   
   	** in the last column. So, we compute the first entry in   
   	** the last column, and make sure that cut_off is not less   
   	** than that value (if it is, then cut_off occurs in a column   
   	** prior to the last column, and violates the constraints   
   	** of this configuration).   
   	**   
   	** FWIW, because of the way we compute n_rows and n_cols,   
   	** we know that cut_off will never /exceed/ n_rows * n_cols.   
   	*/   
   	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 */   
      
   [continued in next message]   
      
   --- SoupGate-Win32 v1.05   
    * Origin: you cannot sedate... all the things you hate (1:229/2)   

[   << oldest   |   < older   |   list   |   newer >   |   newest >>   ]


(c) 1994,  bbs@darkrealms.ca