{"id":2516,"date":"2017-05-27T00:01:14","date_gmt":"2017-05-27T07:01:14","guid":{"rendered":"http:\/\/c-for-dummies.com\/blog\/?p=2516"},"modified":"2017-06-03T08:00:43","modified_gmt":"2017-06-03T15:00:43","slug":"the-square-is-really-magic","status":"publish","type":"post","link":"https:\/\/c-for-dummies.com\/blog\/?p=2516","title":{"rendered":"The Square is Really Magic"},"content":{"rendered":"<p>Before moving off the topic of arrays and their bogus dimensions, I want to play further with a magic square. Specifically, it intrigues me that you can shift rows or columns within the square and it doesn&#8217;t affect the magical properties.<br \/>\n<!--more--><br \/>\nFor example, if you move the first column to the fifth column&#8217;s position, illustrated in Figure 1, you still have a magic square. The same effect happens for the rows as well. It&#8217;s as if the square is a frame onto a pattern of numbers that can shift about without losing its charm.<\/p>\n<div id=\"attachment_2517\" style=\"width: 577px\" class=\"wp-caption aligncenter\"><img loading=\"lazy\" decoding=\"async\" aria-describedby=\"caption-attachment-2517\" src=\"http:\/\/c-for-dummies.com\/blog\/wp-content\/uploads\/2017\/05\/0527-figure1.png\" alt=\"\" width=\"567\" height=\"270\" class=\"size-full wp-image-2517\" srcset=\"https:\/\/c-for-dummies.com\/blog\/wp-content\/uploads\/2017\/05\/0527-figure1.png 567w, https:\/\/c-for-dummies.com\/blog\/wp-content\/uploads\/2017\/05\/0527-figure1-300x143.png 300w, https:\/\/c-for-dummies.com\/blog\/wp-content\/uploads\/2017\/05\/0527-figure1-500x238.png 500w\" sizes=\"auto, (max-width: 567px) 100vw, 567px\" \/><p id=\"caption-attachment-2517\" class=\"wp-caption-text\">Figure 1. Shifting column one in the magic square doesn&#8217;t remove the magic, nor does it change the totals of rows, columns, and diagonals.<\/p><\/div>\n<p>Can you prove that rotating rows and columns works? Sure! Just add up the rows, columns, and diagonals in your head. Or &mdash; better &mdash; you can write code that does the work for you.<\/p>\n<p>The first part of the code is shown below. The magic square is defined as a two-dimensional array. From previous Lessons (<a href=\"http:\/\/c-for-dummies.com\/blog\/?p=2484\">here<\/a> and <a href=\"http:\/\/c-for-dummies.com\/blog\/?p=2498\">here<\/a>), I explain that a 2D array is bogus, but the notation is useful when coding.<\/p>\n<pre class=\"screen\">\r\n#include &lt;stdio.h&gt;\r\n\r\nint main()\r\n{\r\n    int grid[5][5] = {\r\n        { 25, 13,  1, 19,  7},\r\n        { 16,  9, 22, 15,  3},\r\n        { 12,  5, 18,  6, 24},\r\n        {  8, 21, 14,  2, 20},\r\n        {  4, 17, 10, 23, 11}\r\n    };\r\n    int temp[5];\r\n    int row,col;\r\n\r\n<span class=\"comments\">\/* display grid *\/<\/span>\r\n    puts(\"Original Magic Square\");\r\n    for(row=0;row&lt;5;row++)\r\n    {\r\n        for(col=0;col&lt;5;col++)\r\n            printf(\"  %2d\",grid[row][col]);\r\n        putchar('\\n');\r\n    }\r\n\r\n<span class=\"comments\">\/* move column 0 to column 4 *\/<\/span>\r\n    <span class=\"comments\">\/* save column 0 *\/<\/span>\r\n    for(row=0;row<5;row++)\r\n        temp[row] = grid[row][0];\r\n    <span class=\"comments\">\/* move columns 1 through 4 *\/<\/span>\r\n    for(col=0;col<4;col++)\r\n        for(row=0;row<5;row++)\r\n            grid[row][col] = grid[row][col+1];\r\n    <span class=\"comments\">\/* restore column 0 to column 4 *\/<\/span>\r\n    for(row=0;row<5;row++)\r\n        grid[row][4] = temp[row];\r\n\r\n<span class=\"comments\">\/* display grid *\/<\/span>\r\n    puts(\"New Magic Square\");\r\n    for(row=0;row&lt;5;row++)\r\n    {\r\n        for(col=0;col&lt;5;col++)\r\n            printf(\"  %2d\",grid[row][col]);\r\n        putchar('\\n');\r\n    }\r\n\r\n    return(0);\r\n}<\/pre>\n<p>Starting at Line 24, the code moves column 0 to the column 4 position. First, column zero is saved in the <code>temp[]<\/code> array. Then the remaining columns, 1 through 4, are shifted to the right. Finally, column zero, stored in the <code>temp[]<\/code> array, is placed at the column 4 position in the <code>grid[][]<\/code> array. I hope you agree that the 2D array notation helps make the code readable.<\/p>\n<p>Here&#8217;s a sample run:<\/p>\n<pre><code>Original Magic Square\r\n  25  13   1  19   7\r\n  16   9  22  15   3\r\n  12   5  18   6  24\r\n   8  21  14   2  20\r\n   4  17  10  23  11\r\nNew Magic Square\r\n  13   1  19   7  25\r\n   9  22  15   3  16\r\n   5  18   6  24  12\r\n  21  14   2  20   8\r\n  17  10  23  11   4<\/code><\/pre>\n<p>Does the square still work? I added the numbers in my head, and it does, but of course that&#8217;s not programming. The preferred method, for this blog, is to run the <em>confirm_magic()<\/em> function on the array. This function was presented in <a href=\"http:\/\/c-for-dummies.com\/blog\/?p=2505\">last week&#8217;s Lesson<\/a>, but it works only with single-dimension arrays.<\/p>\n<p>This post is a Lesson, not an Exercise, yet I&#8217;m going to challenge you to modify the code presented to allow the <em>confirm_magic()<\/em> function to verify the new magic square. You can code the solution on your own, or just wait until <a href=\"http:\/\/c-for-dummies.com\/blog\/?p=2527\">next week&#8217;s Lesson<\/a> to see what I did. And, no, I&#8217;m not using pointers, nor am I folding everything into the  <em>main()<\/em> function.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>What happens when you move a row or column in a magic square? <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=2516\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2],"tags":[],"class_list":["post-2516","post","type-post","status-publish","format-standard","hentry","category-main"],"_links":{"self":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/2516","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=2516"}],"version-history":[{"count":4,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/2516\/revisions"}],"predecessor-version":[{"id":2551,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/2516\/revisions\/2551"}],"wp:attachment":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=2516"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=2516"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=2516"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}