{"id":3698,"date":"2019-08-08T00:01:06","date_gmt":"2019-08-08T07:01:06","guid":{"rendered":"https:\/\/c-for-dummies.com\/blog\/?p=3698"},"modified":"2019-08-03T09:52:51","modified_gmt":"2019-08-03T16:52:51","slug":"rotate-the-grid-solution","status":"publish","type":"post","link":"https:\/\/c-for-dummies.com\/blog\/?p=3698","title":{"rendered":"Rotate the Grid &#8211; Solution"},"content":{"rendered":"<p>The challenge for <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=3685\">this month&#8217;s Exercise<\/a> is to fill a grid with random letters, a to z, then rotate that grid on its side, counterclockwise, and display the results. Such fun.<br \/>\n<!--more--><br \/>\nWell, I think it&#8217;s fun because this is the exact type of programming puzzle I&#8217;ve enjoyed since toying with the BASIC language centuries ago. The delightful part is figuring out the code that plucks a value from a position in the original matrix and sets that value in the proper, rotated position in the second. Once you get the equation down, the program runs and data flows to work out the solution. It&#8217;s like magic!<\/p>\n<p>Here&#8217;s the <em>main()<\/em> function for my solution, which breaks down the four steps as presented in the Exercise&#8217;s original post:<\/p>\n<pre class=\"screen\">\r\nint main()\r\n{\r\n    char matrix[SIZE*SIZE];\r\n    char matrix_r[SIZE*SIZE];\r\n\r\n    <span class=\"comments\">\/* seed randomizer *\/<\/span>\r\n    srand( (unsigned)time(NULL) );\r\n    \r\n    fill_matrix(matrix);\r\n    puts(\"Original:\");\r\n    show_matrix(matrix);\r\n    rotate_matrix(matrix,matrix_r);\r\n    puts(\"Rotated:\");\r\n    show_matrix(matrix_r);\r\n\r\n    return(0);\r\n}<\/pre>\n<p>The original grid is <code>matrix[]<\/code>, the rotated grid is <code>matrix_r[]<\/code>.<\/p>\n<p>The <em>fill_matrix()<\/em> function initializes the original <code>matrix<\/code> with random characters, a to z.<\/p>\n<p>The <em>show_matrix()<\/em> function outputs the original <code>matrix<\/code>.<\/p>\n<p>The <em>rotate_matrix()<\/em> function is the workhorse, transferring the characters from one grid to the other, rotating the results.<\/p>\n<p>Finally, the <em>show_matrix()<\/em> function is called again, but with <code>matrix_r<\/code> as its argument.<\/p>\n<p>Because both arrays are expressed in single dimensions, they can be passed to the functions name-only.<\/p>\n<p>Here&#8217;s the <em>rotate_matrix()<\/em> function:<\/p>\n<pre class=\"screen\">\r\nvoid rotate_matrix(char a[], char b[])\r\n{\r\n    int row_a,row_b,col_a,col_b;\r\n    \r\n    for(row_a=0,col_b=SIZE-1; row_a&lt;SIZE; row_a++,col_b--)\r\n    {\r\n        for(col_a=0,row_b=0; col_a&lt;SIZE; col_a++,row_b++)\r\n        {\r\n            b[(row_b*SIZE)+col_b] = a[(row_a*SIZE)+col_a];\r\n        }\r\n    }\r\n}<\/pre>\n<p>The original matrix is processed rows, then columns. The rotated matrix is processed columns, then rows. You see this effect with the multiple arguments in each <em>for<\/em> loop.<\/p>\n<p>The outer loop is initialized <code>row_a=0,col_b=SIZE-1<\/code> and each turn alters the variables by incrementing <code>row_a<\/code> and decrementing <code>col_b<\/code>. This is the rotation method, which effectively copies a row from one matrix into a column for the next.<\/p>\n<p>The inner loop doesn&#8217;t need to rotate anything; it processes columns and rows in sequence, initializing both variables <code>col_a<\/code> and <code>row_b<\/code> to zero and incrementing both each turn. This loop&#8217;s statement copies the values:<\/p>\n<p><code>b[(row_b*SIZE)+col_b] = a[(row_a*SIZE)+col_a];<\/code><\/p>\n<p>The <code>*SIZE<\/code> math is necessary to calculate the proper offset in the grid.<\/p>\n<p>Here&#8217;s sample output:<\/p>\n<p><code>Original:<br \/>\n&nbsp;u  k  b  x  v<br \/>\n&nbsp;a  s  t  y  n<br \/>\n&nbsp;m  i  c  w  q<br \/>\n&nbsp;k  b  l  u  x<br \/>\n&nbsp;v  r  j  m  l<br \/>\nRotated:<br \/>\n&nbsp;v  k  m  a  u<br \/>\n&nbsp;r  b  i  s  k<br \/>\n&nbsp;j  l  c  t  b<br \/>\n&nbsp;m  u  w  y  x<br \/>\n&nbsp;l  x  q  n  v<\/code><\/p>\n<p><a href=\"https:\/\/github.com\/dangookin\/C-For-Dummies-Blog\/blob\/master\/2019_08-Exercise.c\" rel=\"noopener noreferrer\" target=\"_blank\">Click here<\/a> to view my solution on github. Alas, I can&#8217;t upload C code to the blog any longer, which disappoints me, but encourages me to post more stuff on github.<\/p>\n<p>I hope you devised a clever solution and were able to solve the problem. For further skills, consider writing code that rotates the grid 180&deg; or 270&deg;. Think of the fun you&#8217;ll have!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>The challenge for this month&#8217;s Exercise is to fill a grid with random letters, a to z, then rotate that grid on its side, counterclockwise, and display the results. Such fun.<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[5],"tags":[],"class_list":["post-3698","post","type-post","status-publish","format-standard","hentry","category-solution"],"_links":{"self":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/3698","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=3698"}],"version-history":[{"count":3,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/3698\/revisions"}],"predecessor-version":[{"id":3707,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/3698\/revisions\/3707"}],"wp:attachment":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=3698"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=3698"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=3698"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}