{"id":25763,"date":"2017-10-25T20:19:35","date_gmt":"2017-10-25T14:49:35","guid":{"rendered":"https:\/\/www.wikitechy.com\/technology\/?p=25763"},"modified":"2017-10-25T20:19:35","modified_gmt":"2017-10-25T14:49:35","slug":"c-programming-magic-square","status":"publish","type":"post","link":"https:\/\/www.wikitechy.com\/technology\/c-programming-magic-square\/","title":{"rendered":"C Programming &#8211; Magic Square"},"content":{"rendered":"<p>A magic square of order n is an arrangement of n^2 numbers, usually distinct integers, in a square, such that the n numbers in all rows, all columns, and both diagonals sum to the same constant. A magic square contains the integers from 1 to n^2.<\/p>\n<p>The constant sum in every row, column and diagonal is called the magic constant or magic sum, M. The magic constant of a normal magic square depends only on n and has the following value:<br \/>\nM = n(n^2+1)\/2<\/p>\n<p>For normal magic squares of order n = 3, 4, 5, \u2026, the magic constants are: 15, 34, 65, 111, 175, 260, \u2026<\/p>\n<p>In this post, we will discuss how programmatically we can generate a magic square of size n. Before we go further, consider the below examples:<\/p>\n<pre>Magic Square of size 3\r\n-----------------------\r\n  2   7   6\r\n  9   5   1\r\n  4   3   8\r\nSum in each row & each column = 3*(3^2+1)\/2 = 15\r\n\r\n\r\nMagic Square of size 5\r\n----------------------\r\n  9   3  22  16  15\r\n  2  21  20  14   8\r\n 25  19  13   7   1\r\n 18  12   6   5  24\r\n 11  10   4  23  17\r\nSum in each row & each column = 5*(5^2+1)\/2 = 65\r\n\r\n\r\nMagic Square of size 7\r\n----------------------\r\n 20  12   4  45  37  29  28\r\n 11   3  44  36  35  27  19\r\n  2  43  42  34  26  18  10\r\n 49  41  33  25  17   9   1\r\n 40  32  24  16   8   7  48\r\n 31  23  15  14   6  47  39\r\n 22  21  13   5  46  38  30\r\nSum in each row & each column = 7*(7^2+1)\/2 = 175<\/pre>\n<p>Did you find any pattern in which the numbers are stored?<br \/>\nIn any magic square, the first number i.e. 1 is stored at position (n\/2, n-1). Let this position be (i,j). The next number is stored at position (i-1, j+1) where we can consider each row & column as circular array i.e. they wrap around.<\/p>\n[ad type=\u201dbanner\u201d]\n<p>Three conditions hold:<\/p>\n<p>1. The position of next number is calculated by decrementing row number of previous number by 1, and incrementing the column number of previous number by 1. At any time, if the calculated row position becomes -1, it will wrap around to n-1. Similarly, if the calculated column position becomes n, it will wrap around to 0.<\/p>\n<p>2. If the magic square already contains a number at the calculated position, calculated column position will be decremented by 2, and calculated row position will be incremented by 1.<\/p>\n<p>3. If the calculated row position is -1 & calculated column position is n, the new position would be: (0, n-2).<\/p>\n<pre>Example:\r\nMagic Square of size 3\r\n----------------------\r\n 2  7  6\r\n 9  5  1\r\n 4  3  8 \r\n\r\nSteps:\r\n1. position of number 1 = (3\/2, 3-1) = (1, 2)\r\n2. position of number 2 = (1-1, 2+1) = (0, 0)\r\n3. position of number 3 = (0-1, 0+1) = (3-1, 1) = (2, 1)\r\n4. position of number 4 = (2-1, 1+1) = (1, 2)\r\n   Since, at this position, 1 is there. So, apply condition 2.\r\n   new position=(1+1,2-2)=(2,0)\r\n5. position of number 5=(2-1,0+1)=(1,1)\r\n6. position of number 6=(1-1,1+1)=(0,2)\r\n7. position of number 7 = (0-1, 2+1) = (-1,3) \/\/ this is tricky, see condition 3 \r\n   new position = (0, 3-2) = (0,1)\r\n8. position of number 8=(0-1,1+1)=(-1,2)=(2,2) \/\/wrap around\r\n9. position of number 9=(2-1,2+1)=(1,3)=(1,0) \/\/wrap around\r\n<\/pre>\n<p>Based on the above approach, following is the working code:<\/p>\n[pastacode lang=\u201dc\u201d manual=\u201d%23include%3Cstdio.h%3E%0A%23include%3Cstring.h%3E%0A%20%0A%2F%2F%20A%20function%20to%20generate%20odd%20sized%20magic%20squares%0Avoid%20generateSquare(int%20n)%0A%7B%0A%20%20%20%20int%20magicSquare%5Bn%5D%5Bn%5D%3B%0A%20%0A%20%20%20%20%2F%2F%20set%20all%20slots%20as%200%0A%20%20%20%20memset(magicSquare%2C%200%2C%20sizeof(magicSquare))%3B%0A%20%0A%20%20%20%20%2F%2F%20Initialize%20position%20for%201%0A%20%20%20%20int%20i%20%3D%20n%2F2%3B%0A%20%20%20%20int%20j%20%3D%20n-1%3B%0A%20%0A%20%20%20%20%2F%2F%20One%20by%20one%20put%20all%20values%20in%20magic%20square%0A%20%20%20%20for%20(int%20num%3D1%3B%20num%20%3C%3D%20n*n%3B%20)%0A%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20if%20(i%3D%3D-1%20%26%26%20j%3D%3Dn)%20%2F%2F3rd%20condition%0A%20%20%20%20%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20j%20%3D%20n-2%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20i%20%3D%200%3B%0A%20%20%20%20%20%20%20%20%7D%0A%20%20%20%20%20%20%20%20else%0A%20%20%20%20%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20%2F%2F1st%20condition%20helper%20if%20next%20number%20goes%20to%20out%20of%20square\u2019s%20right%20side%0A%20%20%20%20%20%20%20%20%20%20%20%20if%20(j%20%3D%3D%20n)%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20j%20%3D%200%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20%2F%2F1st%20condition%20helper%20if%20next%20number%20is%20goes%20to%20out%20of%20square\u2019s%20upper%20side%0A%20%20%20%20%20%20%20%20%20%20%20%20if%20(i%20%3C%200)%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20i%3Dn-1%3B%0A%20%20%20%20%20%20%20%20%7D%0A%20%20%20%20%20%20%20%20if%20(magicSquare%5Bi%5D%5Bj%5D)%20%2F%2F2nd%20condition%0A%20%20%20%20%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20j%20-%3D%202%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20i%2B%2B%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20continue%3B%0A%20%20%20%20%20%20%20%20%7D%0A%20%20%20%20%20%20%20%20else%0A%20%20%20%20%20%20%20%20%20%20%20%20magicSquare%5Bi%5D%5Bj%5D%20%3D%20num%2B%2B%3B%20%2F%2Fset%20number%0A%20%0A%20%20%20%20%20%20%20%20j%2B%2B%3B%20%20i\u2013%3B%20%2F%2F1st%20condition%0A%20%20%20%20%7D%0A%20%0A%20%0A%20%20%20%20%2F%2F%20print%20magic%20square%0A%20%20%20%20printf(%22The%20Magic%20Square%20for%20n%3D%25d%3A%5CnSum%20of%20each%20row%20or%20column%20%25d%3A%5Cn%5Cn%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20n%2C%20n*(n*n%2B1)%2F2)%3B%0A%20%20%20%20for(i%3D0%3B%20i%3Cn%3B%20i%2B%2B)%0A%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20for(j%3D0%3B%20j%3Cn%3B%20j%2B%2B)%0A%20%20%20%20%20%20%20%20%20%20%20%20printf(%22%253d%20%22%2C%20magicSquare%5Bi%5D%5Bj%5D)%3B%0A%20%20%20%20%20%20%20%20printf(%22%5Cn%22)%3B%0A%20%20%20%20%7D%0A%7D%0A%20%0A%2F%2F%20Driver%20program%20to%20test%20above%20function%0Aint%20main()%0A%7B%0A%20%20%20%20int%20n%20%3D%207%3B%20%2F%2F%20Works%20only%20when%20n%20is%20odd%0A%20%20%20%20generateSquare%20(n)%3B%0A%20%20%20%20return%200%3B%0A%7D\u201d message=\u201dC Program\u201d highlight=\u201d\u201d provider=\u201dmanual\u201d\/]\n<p><strong>Output:<\/strong><\/p>\n<pre>The Magic Square for n=7:\r\nSum of each row or column 175:\r\n\r\n 20  12   4  45  37  29  28\r\n 11   3  44  36  35  27  19\r\n  2  43  42  34  26  18  10\r\n 49  41  33  25  17   9   1\r\n 40  32  24  16   8   7  48\r\n 31  23  15  14   6  47  39\r\n 22  21  13   5  46  38  30<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>C Programming &#8211; Magic Square &#8211; Mathematical Algorithms &#8211; A magic square of order n is an arrangement of n^2 numbers, usually distinct integers, in a square.<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[69866,1,83564,74058],"tags":[75140,75165,75148,75144,75131,75168,75150,75138,75121,75129,75155,75169,75124,75134,75130,75160,75135,75156,75143,75149,75125,75133,75132,75159,75167,75163,75146,75170,75136,75123,75126,75153,75141,75162,75152,75158,75154,75142,75139,75128,75127,75137,75157,75151,75147,75164,75166,75145,75122,75161],"class_list":["post-25763","post","type-post","status-publish","format-standard","hentry","category-c-programming","category-coding","category-magic-square","category-mathematical-algorithms","tag-3x3-magic-square-solver","tag-4x4-magic-square-generator","tag-4x4-magic-square-solver","tag-5x5-magic-square-solution","tag-5x5-magic-square-solver","tag-how-to-solve-magic-square-puzzle","tag-how-to-solve-magic-squares","tag-how-to-solve-magic-squares-3x3","tag-integer-magic-squares","tag-magic-box","tag-magic-box-math-problem","tag-magic-matrix","tag-magic-program","tag-magic-square","tag-magic-square-1-to-9","tag-magic-square-16","tag-magic-square-3x3","tag-magic-square-3x3-java-source-code","tag-magic-square-4x4","tag-magic-square-4x4-trick","tag-magic-square-code-in-c","tag-magic-square-examples","tag-magic-square-formula","tag-magic-square-generator-3x3","tag-magic-square-generator-4x4","tag-magic-square-how-to-solve","tag-magic-square-in-java","tag-magic-square-math","tag-magic-square-of-integers","tag-magic-square-problems","tag-magic-square-puzzle","tag-magic-square-puzzle-4x4","tag-magic-square-puzzle-solver","tag-magic-square-puzzle-with-answers","tag-magic-square-solver","tag-magic-square-solver-3x3-answer","tag-magic-square-trick","tag-magic-square-trick-3x3","tag-magic-square-with-solution","tag-magic-squares-worksheet","tag-magic-sum","tag-math-magic-tricks-and-methods","tag-mathematical-magic-square","tag-maths-magic-squares","tag-maths-magic-squares-with-solution","tag-matrix-magic","tag-program-magic","tag-solve-magic-square","tag-square-matrix-program-in-c","tag-www-magic-square"],"_links":{"self":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts\/25763","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/comments?post=25763"}],"version-history":[{"count":0,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts\/25763\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/media?parent=25763"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/categories?post=25763"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/tags?post=25763"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}