{"id":1781,"date":"2016-02-27T00:01:32","date_gmt":"2016-02-27T08:01:32","guid":{"rendered":"http:\/\/c-for-dummies.com\/blog\/?p=1781"},"modified":"2016-02-13T10:25:17","modified_gmt":"2016-02-13T18:25:17","slug":"permutations","status":"publish","type":"post","link":"https:\/\/c-for-dummies.com\/blog\/?p=1781","title":{"rendered":"Permutations"},"content":{"rendered":"<p>In his 1953 short story, <em><a href=\"https:\/\/en.wikipedia.org\/wiki\/The_Nine_Billion_Names_of_God\" target=\"_blank\">The Nine Billion Names of God<\/a><\/em>, Arthur C. Clarke writes of Tibetan llamas who seek to know all the names of God. They&#8217;ve been writing down the names for centuries, but upon the advent of the computer, they enlist western science to help them finish the task in days. You can complete the same task with your computer and the C programming language, but in hours instead of days.<br \/>\n<!--more--><br \/>\nIn the story, Clarke cites the algorithm for printing out all the possible name combinations. Rather than just spit out names <code>AAAAAAAAA<\/code> through <code>ZZZZZZZZZ<\/code>, some rules are added to eliminate repetitive letters and other stipulations made by the monks.<\/p>\n<p>The task of generating such output is accomplished easily in any computer language: Use nested loops to generate permutations of <code>AAAAAAAAA<\/code> through <code>ZZZZZZZZZ<\/code>. The greater the number of characters, the long it takes the program to run. If you display the results, the program runs longer. Printing takes longest.<\/p>\n<blockquote><p>A <em>permutation<\/em> includes all possible results of a series such as <code>AAAAAAAAA<\/code> through <code>ZZZZZZZZZ<\/code>. A <em>combination<\/em> is limited to only certain results. So in <em>The Nine Billion Names of God<\/em>, the programmers generate combinations.<\/p><\/blockquote>\n<p>In the following code, three nested <em>for<\/em> loops generate all permutations of <code>aaa<\/code> through <code>zzz<\/code>, what I like to call the TLA list, for Three Letter Acronyms:<\/p>\n<pre class=\"screen\">\r\n#include &lt;stdio.h&gt;\r\n\r\nint main()\r\n{\r\n    char tla[4] = \"\\0\";\r\n\r\n    for(tla[0]='a';tla[0]&lt;='z';tla[0]++)\r\n        for(tla[1]='a';tla[1]&lt;='z';tla[1]++)\r\n            for(tla[2]='a';tla[2]&lt;='z';tla[2]++)\r\n                printf(\"%s \",tla);\r\n    putchar('\\n');\r\n\r\n    return(0);\r\n}<\/pre>\n<p>The <em>char<\/em> array <code>tla[]<\/code> at Line 5 is initialized to a null string, which ensures that a null character (<code>\\0<\/code>) terminates the string.<\/p>\n<p>In addition to holding the output, <code>tla[]<\/code>&#8216;s elements are also used as variables in the <em>for<\/em> loops. That makes the <em>for<\/em> statements look a bit complex.<\/p>\n<p>Each <em>for<\/em> loop spins from the letter <code>'a'<\/code> through <code>'z'<\/code>, inclusive, which is why the <code>&lt;=<\/code> operator is used.<\/p>\n<p>At Line 10, the <em>printf()<\/em> statement displays each result. Then a final newline is displayed at Line 11, then the program quits.<\/p>\n<p>The output is all one line of text, from <code>aaa<\/code> through <code>zzz<\/code>, and on a modern computer the code takes only a few milliseconds to run.<\/p>\n<p>You can increase the number of characters in the permutation by adding another nested <em>for<\/em> loop. That addition increases the time the program takes to run: On my system, the 3-character TLA program ran in 0.011 seconds, but the 4-character TLA program took 0.074 seconds. Adding a fifth character increased the program&#8217;s runtime to 1.941 seconds.<\/p>\n<p>As a challenge, a sort-of mini-Exercise, modify the code so that it doesn&#8217;t generate a single line of text as output; add code that spits out a newline after each 20th TLA is displayed. Please try this Exercise on your own; <a href=\"http:\/\/c-for-dummies.com\/blog\/wp-content\/uploads\/2016\/02\/0227.c\"rel=\"\">click here<\/a> to view my solution, though many solutions are possible.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Use nested <em>for<\/em> loops to generate all possible combinations of letters. <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=1781\">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-1781","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\/1781","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=1781"}],"version-history":[{"count":5,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/1781\/revisions"}],"predecessor-version":[{"id":1791,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/1781\/revisions\/1791"}],"wp:attachment":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1781"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1781"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1781"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}