Advertisement
dmilicev

print_numbers_in_rows_and_columns_v1.c

Oct 25th, 2019
257
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 6.34 KB | None | 0 0
  1.  /*
  2.  
  3.     print_numbers_in_rows_and_columns_v1.c
  4.  
  5.  
  6.     Task is to print numbers from first to last
  7.  
  8.     in desired numbers of columns
  9.  
  10.     by rows and by columns.
  11.  
  12.  
  13.     Can anyone write a function
  14.  
  15.         print_numbers_in_columns()
  16.  
  17.     which works well ?
  18.  
  19.     My function sometimes works well, but sometimes doesn't
  20.     (has wrong number of columns).
  21.  
  22.     Thanks in advance for your help.
  23.  
  24. -------------------------------------------------
  25.  
  26. These days I'm occupied with rectangles.
  27.  
  28. The idea is that n numbers should be placed in a rectangle of minimal area.
  29.  
  30. We know the number of numbers n and the required number of columns.
  31.  
  32. The area of the rectangle must be equal to or greater than the number n
  33.  
  34. rows * columns >= n.
  35.  
  36. From there,
  37.  
  38. rows >= n / columns             ( integer division ! )
  39.  
  40. (If rows has decimal places... no)
  41.  
  42. If calculated rows
  43.  
  44. rows * column < n
  45.  
  46. we have to choose the first bigger integer.
  47.  
  48. But this is not the end. :(
  49.  
  50. A further problem is that the function (which I found on the Internet, two for loops)
  51.  
  52. is quite insensitive to changing the number n and the number of columns.
  53.  
  54. Probably we need to write a new function whose algorithm will be close to human thinking.
  55.  
  56. When done properly, only then should it be optimized ...
  57.  
  58. -------------------------------------------------
  59.  
  60. Upon further reflection, I finally realized that
  61. what I was looking for was in fact impossible !
  62.  
  63. So, for example, we want to print numbers 1 to 22 in 7 columns.
  64.  
  65. Row numbers must be calculated.
  66.  
  67. n = last - first + 1 = 22 - 1 + 1 = 22
  68.  
  69. area of rectangle >= n
  70.  
  71. rows * columns >= n
  72.  
  73. rows >= n / columns             ( integer division )
  74.  
  75. rows >= 22 / 7
  76.  
  77. rows >= 3 ( and 1 left )
  78.  
  79. Because
  80.  
  81. rows * columns = 3 * 7 = 21 < 22
  82.  
  83. we choose the first bigger integer,
  84.  
  85. so definitely rows = 4
  86.  
  87. Let's print the numbers 1 to 22 in 7 columns and 4 rows by columns in pencil on paper :
  88.  
  89. 1 5  9 13 17 21
  90. 2 6 10 14 18 22
  91. 3 7 11 15 19
  92. 4 8 12 16 20
  93.  
  94. !!!
  95.  
  96. Unsurprisingly, our numbers are in 4 rows and (only !!!) 6 colons.
  97.  
  98. We have consumed all the numbers and do not have them for the required seventh column.
  99.  
  100. Then let's write the numbers in 7 columns and 3 rows ( let rows = 3 ) :
  101.  
  102. 1 4 7 10 13 16 19 22
  103. 2 5 8 11 14 17 20
  104. 3 6 9 12 15 18 21
  105.  
  106. As we can see, we filled 7 columns in 3 rows, but we still have numbers left!
  107.  
  108. Number 22 we failed to insert, it requires an 8th column !
  109.  
  110. That's why I finally realized that what I was looking for was in fact impossible
  111.  
  112. and the function actually works fine.
  113.  
  114. I hope you have at least learned from all my confusion how to think when doing a task.
  115.  
  116. Pen and paper are the law of thought !
  117.  
  118. Sorry for your time spent. :(
  119.  
  120.  
  121. */
  122.  
  123. #include<stdio.h>
  124.  
  125.  
  126. // Prints numbers by rows, from first to last, in the required number of columns
  127. int print_numbers_in_rows(int first_number, int last_number, int number_of_columns)
  128. {
  129.     int i, j, number = first_number, number_of_rows;
  130.  
  131.     // calculate the number_of_rows required
  132.     number_of_rows = (last_number - first_number + 1) / number_of_columns;
  133.  
  134. printf("\n 1) number_of_rows = %d \n", number_of_rows);     // delete this line
  135.  
  136.     if ( number_of_rows * number_of_columns < last_number - first_number + 1 )
  137.         number_of_rows++;
  138.  
  139. printf("\n 2) number_of_rows = %d \n\n", number_of_rows);   // delete this line
  140.  
  141.     for(i=0; i<number_of_rows; i++)             // loop for rows
  142.     {
  143.         for(j=0; j<number_of_columns; j++)      // loop for columns
  144.         {
  145.             if ( number<=last_number)
  146.                 printf("%5d", number++);        // print current number
  147.             else
  148.                 return number;
  149.         }
  150.  
  151.         printf("\n");                           // new row
  152.     }
  153. }
  154.  
  155.  
  156. // Prints numbers by columns, from first to last, in the required number of columns
  157. void print_numbers_in_columns(int first_number, int last_number, int number_of_columns)
  158. {
  159.     int i, j, count, first, number_of_rows;
  160.  
  161.     // calculate the number of numbers to be printed
  162.     count = last_number - first_number + 1;
  163.  
  164.     // calculate the number_of_rows required
  165.     // The idea is that n numbers should be placed in a rectangle of minimal area.
  166.     // We know the number of numbers n and the required number of columns.
  167.     // The area of the rectangle must be equal to or greater than the number n
  168.     //      rows * columns >= n.
  169.     // From there,
  170.     //      rows >= n / columns     ( integer division ! )
  171.     // (If rows has decimal places... no)
  172.     // If calculated rows
  173.     //      rows * column < n
  174.     //  we have to choose the first bigger integer, rows++ .
  175.  
  176.     number_of_rows = (last_number - first_number + 1) / number_of_columns;
  177.  
  178. printf("\n 1) number_of_rows = %d \n", number_of_rows);             // delete this line
  179.  
  180.     if ( number_of_rows * number_of_columns < count )
  181.         number_of_rows++;
  182.  
  183. printf("\n 2) number_of_rows = %d \n\n", number_of_rows);           // delete this line
  184.  
  185.     for(i=0, first=first_number; i<number_of_rows; i++, first++ )   // loop for rows
  186.     {
  187.         for( j=first; j<=last_number; j+=number_of_rows )           // loop for columns
  188.         {
  189.             printf("%5d", j);                                       // print current number
  190.         }
  191.  
  192.         printf("\n");   // new row
  193.     }
  194.  
  195.  
  196.     /*
  197.     // calculate the number of numbers to be printed
  198.     count = last_number - first_number + 1;
  199.  
  200.     // calculate the number_of_rows required
  201.     //number_of_rows = ( count / number_of_columns ) + ( (count % number_of_columns)>0 );
  202.     // or:
  203.     number_of_rows = (last_number - first_number ) / number_of_columns + 1;
  204.  
  205.     for(i=0, first=first_number; i<number_of_rows; i++, first++ )   // loop for rows
  206.     {
  207.         for( j=first; j<=last_number; j+=number_of_rows )           // loop for columns
  208.         {
  209.             printf("%5d", j);                                       // print current number
  210.         }
  211.  
  212.         printf("\n");   // new row
  213.     }
  214. */
  215. }
  216.  
  217.  
  218. int main()
  219. {           // change number_of_columns from 1 to 15 to see how functions works.
  220. //    int first_number=1, last_number=100, number_of_columns=15;
  221. //    int first_number=1, last_number=106, number_of_columns=13;
  222.     int first_number=1, last_number=22, number_of_columns=7;
  223.  
  224.  
  225.     printf("\n Numbers from %d to %d by rows in %d columns : \n\n",
  226.             first_number, last_number, number_of_columns );
  227.  
  228.     print_numbers_in_rows(first_number, last_number, number_of_columns);
  229.  
  230.  
  231.     printf("\n\n Numbers from %d to %d by columns in %d columns : \n\n",
  232.             first_number, last_number, number_of_columns );
  233.  
  234.     print_numbers_in_columns(first_number, last_number, number_of_columns);
  235.  
  236.  
  237.     printf("\n");
  238.  
  239.     return 0;
  240. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement