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 |
[   << oldest   |   < older   |   list   |   newer >   |   newest >>   ]
(c) 1994, bbs@darkrealms.ca