{"id":2527,"date":"2017-06-03T00:01:13","date_gmt":"2017-06-03T07:01:13","guid":{"rendered":"http:\/\/c-for-dummies.com\/blog\/?p=2527"},"modified":"2017-05-27T08:21:21","modified_gmt":"2017-05-27T15:21:21","slug":"shift-and-verify-a-magic-square","status":"publish","type":"post","link":"https:\/\/c-for-dummies.com\/blog\/?p=2527","title":{"rendered":"Shift and Verify a Magic Square"},"content":{"rendered":"<p><a href=\"http:\/\/c-for-dummies.com\/blog\/?p=2516\">Last week&#8217;s Lesson<\/a> showed how to shift a column of numbers in an array grid. The array just happens to be a magic square. So the puzzle is to run the array through the <em>confirm_magic()<\/em> function, which was presented in <a href=\"http:\/\/c-for-dummies.com\/blog\/?p=2505\">a previous week&#8217;s Lesson<\/a>. Seems easy.<br \/>\n<!--more--><br \/>\nThe problem, however, is that the <em>confirm_magic()<\/em> function assumes that you&#8217;re passing a single-dimension int array. In last week&#8217;s Lesson, however, the array is declared in two dimensions:<\/p>\n<pre class=\"screen\">\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    };<\/pre>\n<p>You cannot pass a 2D array to a function unless you use <code>**<\/code> pointer notation, which many C programmers avoid. And, as you can see above, I didn&#8217;t declare the array as a set of pointers.<\/p>\n<p>The <em>confirm_magic()<\/em> function consumes a single-dimension array, so my solution was to create a second array <code>g[]<\/code> and copy the data from <code>grid[][]<\/code> into that array. This process takes place as the original array is displayed after moving column 0:<\/p>\n<pre class=\"screen\">\r\n<span class=\"comments\">\/* display grid *\/<\/span>\r\n    puts(\"New Magic Square\");\r\n    x = 0;\r\n    for(row=0;row&lt;5;row++)\r\n    {   \r\n        for(col=0;col&lt;5;col++)\r\n        {   \r\n            printf(\"  %2d\",grid[row][col]);\r\n            <span class=\"comments\">\/* build array for function *\/<\/span>\r\n            g[x] = grid[row][col];\r\n            x++;\r\n        }\r\n        putchar('\\n');\r\n    }<\/pre>\n<p>As the nested <em>for<\/em> loops plow through the <code>grid[][]<\/code> array, each element is stuffed into array <code>g[]<\/code>:<\/p>\n<p><code>g[x] = grid[row][col];<\/code><\/p>\n<p>After the array is filled, it&#8217;s passed to the <em>confirm_magic()<\/em> function, as it&#8217;s a single-dimension array and the function can deal with it just fine.<\/p>\n<p><a href=\"http:\/\/c-for-dummies.com\/blog\/wp-content\/uploads\/2017\/05\/0603.c\">Click here<\/a> to view the full code.<\/p>\n<p>You could modify the code further, so that it shuffles through all the rows and columns, each time confirming that the square remains magical. That&#8217;s an exercise you can attempt on your own. The point of this Lesson is to demonstrate a quick-and-dirty shortcut to pass a 2D array to a function that gobbles only 1D arrays &mdash; without having to resort to pointer notation.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Use C to shift a column in a magic square and then verify that the square is still magic. <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=2527\">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-2527","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\/2527","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=2527"}],"version-history":[{"count":2,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/2527\/revisions"}],"predecessor-version":[{"id":2541,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/2527\/revisions\/2541"}],"wp:attachment":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=2527"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=2527"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=2527"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}