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,242 messages   

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

   Message 243,230 of 243,242   
   DFS to All   
   Sort of trivial code challenge - may be    
   19 Feb 26 16:55:25   
   
   From: nospam@dfs.com   
      
   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   
      
   Kind of goes without saying the solution should handle any row x column   
   input:   
      
   input rows columns   
      
   input 1 1   
   1   
      
   input 1 4   
   1 2 3 4   
      
   input 5 2   
   1  6   
   2  7   
   3  8   
   4  9   
   5  10   
      
   input 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 trivial to   
   less trivial!   
      
   --------------------------------------------------------------------   
   1) must be able to cut the output off at any arbitrary value   
       lower than rows x columns   
   --------------------------------------------------------------------   
   input = 5 5  (no cutoff)   
   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   
      
   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 = 5 5 6   
   1 6   
   2   
   3   
   4   
   5   
      
   input = 5 5 3   
   1   
   2   
   3   
      
   input = 1 10 (no cutoff)   
   1 2 3 4 5 6 7 8 9 10   
      
   input = 1 10 3  (cutoff at 3)   
   1 2 3   
      
      
      
   --------------------------------------------------------------------   
   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 = N   
      
   input = 1  (1 row and 1 column includes 1)   
   1   
      
   input = 2   
   not possible to output 1-2 where rows=columns   
      
   input = 3  (2 rows and 2 columns includes 3)   
   1 3   
   2   
      
   input = 4  (2 rows and 2 columns includes 4)   
   1 3   
   2 4   
      
   input = 5   
   not possible to output 1-5 where rows=columns   
      
   input = 6   
   not possible to output 1-6 where rows=columns   
      
   input = 7  (3 rows and 3 columns includes 7)   
   1 4 7   
   2 5   
   3 6   
      
   input = 8   
   1 4 7   
   2 5 8   
   3 6   
      
   input = 9   
   1 4 7   
   2 5 8   
   3 6 9   
      
   input = 10 11 or 12   
   not possible to output 1-10/11/12 where rows=columns   
      
   input = 13   
   1  5   9  13   
   2  6  10   
   3  7  11   
   4  8  12   
      
   input = 14   
   1  5   9  13   
   2  6  10  14   
   3  7  11   
   4  8  12   
      
   input = 15   
     1   5   9  13   
     2   6  10  14   
     3   7  11  15   
     4   8  12   
      
   input = 16   
     1   5   9  13   
     2   6  10  14   
     3   7  11  15   
     4   8  12  16   
      
   input = 17 18 19 or 20   
   not possible to output 1-17/18/19/20 where rows=columns   
      
   input = 21   
     1   6  11  16  21   
     2   7  12  17   
     3   8  13  18   
     4   9  14  19   
     5  10  15  20   
      
   input = 22   
     1   6  11  16  21   
     2   7  12  17  22   
     3   8  13  18   
     4   9  14  19   
     5  10  15  20   
      
   input = 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   
      
   input = 26   
   not possible to output 1-26 where rows=columns   
      
   input = 27   
   not possible to output 1-27 where rows=columns   
      
   etc   
      
      
   * 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.   
   -----------------------------------------------------------------------   
      
      
   My code is below.   
      
   Look for 'core routine' to see the master output algorithm   
   Requirement 1 is part of the core routine   
   Requirement 2 is the function "calc_rows_columns()"   
      
   If you try this, don't feel obligated to output the column headers as I   
   did.  It's a nice-to-have.   
      
   No code spying until you post yours!   
      
   Enjoy!   
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
   --------------------------------------------------------------------------   
   C listing   
   --------------------------------------------------------------------------   
   // this public domain program outputs numbers by column then row   
   // usage examples   
   // $prog rows columns (print a sequential nbr matrix of size rows x columns)   
   // $prog rows columns stop (output discontinued past stop)   
   // $prog N  (try to find rows=columns that will include only 1-N in output   
      
   #include    
   #include    
   #include    
      
   //print a separator line   
   void printline(int linewidth, char *linechar) {   
   	for(int i = 0; i < linewidth; i++) {	   
   		printf(linechar);	   
   	}   
   	printf("\n");   
   }   
      
   //print column count headers   
   void printcolheader(int cols, int charwidth) {   
   	printline(cols * charwidth,"-");   
   	for (int p = 1; p <= cols; p++) {   
   		printf("%*d",charwidth,p);   
   	}   
   	printf("\n");   
   	printline(cols * charwidth,"-");   
   }	   
   	   
   //twist: try to find rows=columns that will include the input number   
   void calc_rows_columns(int *rows, int *cols, int nbr) {   
   	int m = 10000;   
   	   
   	for (int i = 1; i < m; i++) {   
   		if (i*i == nbr) {   
   			*rows = i;   
   			*cols = i;   
   			return;   
   		}	   
   	}   
   	   
   	for (int i = 2; i < m; i++) {   
   		if ((i*(i-1) < nbr) && ((i*i) >= nbr)) {   
   			*rows = i;   
   			*cols = i;   
   			return;   
   		}	   
   	}   
   	   
   	printf("%s%d%s\n","Not possible to output 1-", nbr ," where rows =   
   columns");   
   	exit(0);   
   }	   
      
   	   
   //core routine to write row x column data to screen   
   void output(int rows, int cols, int max)   
   {   
   	   
   	//calc horizontal spacing of columns based on largest number (minimum 3)   
   	char strmax[10];   
   	sprintf(strmax, "%d", max);   
   	int colwidth = 1 + strlen(strmax);   
   	if (colwidth == 2) {colwidth = 3;}   
   	   
   	//print column header lines   
   	//TODO: maybe print no header columns beyond the column containing the   
   max value   
   	printcolheader(cols, colwidth);   
   	   
   	//nbr matrix   
   	for (int r = 1; r <= rows; r++) {   
   		if (r <= max) {   
   			int nbr = r;   
   			printf("%*d",colwidth,nbr);   
   			for (int i = 0; i < cols-1; i++) {   
   				nbr += rows;   
   				if (nbr <= max) {   
   					printf("%*d",colwidth,nbr);   
   				}   
   			}	   
   			printf("\n");   
   		}   
   		else   
   		{   
   		  break;   
   		}	   
   	}		   
   	   
   	//repeat the headers for readability   
   	if (rows >= 40 && max >= 40) {printcolheader(cols, colwidth);}   
   	   
   }   
      
   int main(int argc, char *argv[]) {   
   	   
   	//validation   
   	if (argc < 2 || argc > 4) {   
   		printf("Use one of these input formats:\n");   
   		printf(" $prog rows columns\n");   
   		printf(" $prog rows columns max (program will halt output past max)\n");   
   		printf(" $prog N  (program will try to determine a square matrix to   
   incl N)\n");   
   		return 0;   
   	}	   
   	   
   	//vars   
   	int rows, cols, max;   
   	   
   	//only 1 argument = try to calc the rows = columns values   
   	// that will include the input number   
   	if (argc == 2) {   
   		max = atoi(argv[1]);   
   		calc_rows_columns(&rows, &cols, max);   
   	}   
   	   
   	if (argc == 3) {   
   		rows = atoi(argv[1]);   
   		cols = atoi(argv[2]);   
   		max  = rows * cols;   
   	}   
   	   
   	   
   	if (argc == 4) {   
   		rows = atoi(argv[1]);   
   		cols = atoi(argv[2]);   
   		max  = atoi(argv[3]);   
   	}	   
   	   
   	//write data to screen   
   	output(rows, cols, max);   
   	   
   	return 0;   
   }	   
   --------------------------------------------------------------------------   
      
   --- 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