{"id":26192,"date":"2016-06-16T08:37:15","date_gmt":"2016-06-16T06:37:15","guid":{"rendered":"https:\/\/mamchenkov.net\/wordpress\/?p=26192"},"modified":"2016-06-16T08:37:15","modified_gmt":"2016-06-16T06:37:15","slug":"how-would-you-write-a-c-program-to-print-1-to-100-without-loop-recursion-and-goto","status":"publish","type":"post","link":"https:\/\/mamchenkov.net\/wordpress\/2016\/06\/16\/how-would-you-write-a-c-program-to-print-1-to-100-without-loop-recursion-and-goto\/","title":{"rendered":"How would you write a C program to print 1 to 100 without loop, recursion and goto?"},"content":{"rendered":"<!-- google_ad_section_start -->\n<p>I came across this Quora question today &#8211; &#8220;<a href=\"https:\/\/www.quora.com\/How-would-you-write-a-C-program-to-print-1-to-100-without-loop-recursion-and-goto\">How would you write a C program to print 1 to 100 without loop, recursion and goto?<\/a>&#8221; \u00a0As always, there is a plethora of answers, of which I liked the following two.<\/p>\n<p><a href=\"https:\/\/www.quora.com\/How-would-you-write-a-C-program-to-print-1-to-100-without-loop-recursion-and-goto\/answer\/Jonathan-Day-10?srid=X7zK\">Jonathan Day<\/a> goes into how the specification can be misinterpreted in a variety of ways, and how garbage in (specification) means garbage out (implementation). \u00a0I like it so much, I&#8217;m going to quote the whole thing here, so that I can easily find it in the future:<\/p>\n<blockquote>\n<pre class=\"brush: cpp; light: true; title: ; notranslate\" title=\"\">\r\n{\r\n  printf(\u201c1 to 100\\n\u201d);\r\n}\r\n<\/pre>\n<p>You actually want the numbers, not just that particular string? That\u2019s the problem with poor specifications. Managers have no consideration for us poor programmers\u2026<\/p>\n<p>No loops of any kind, no recursion of any kind, no goto (or comefrom) of any kind\u2026 This is the opposite extreme, taking the question absolutely literally as it is given rather than as it might be intended. This would have to include loops, recursion and gotos in all libraries, external programs and even the kernel itself, if taken to this degree.<\/p>\n<p>Which creates a problem. You have to treat all such things as black boxes. You do not know if the kernel parses system calls by looping through the operands. That would be a loop that any printing function must call. The question says nothing about YOU not using loops, it says no loops.<\/p>\n<p>Again, this is why specifications must be exact in programming. Poor specifications will get you what you asked for, but not what you want, just as that code snippet above did. No, I don\u2019t care what was in someone\u2019s mind, nor does the computer. GIGO is the law to which all in IT must obey whether they like it or not. That is why you MUST say what you mean and mean what you say, in positives and negatives.<\/p>\n<p>It\u2019s also why a lot of highly autistic people are highly successful. Literalism is, very literally, in their genes.<\/p>\n<p>Ok, let\u2019s use a third possible interpretation, that you want the numbers 1 through to 100, without the code using loops, recursion or gotos, but where calls may.<\/p>\n<p>Most of the other answers are very acceptable, although some were implicit loops rather than explicit loops. Again, are implicit loops allowed or not allowed, under the specification? The specification simply doesn\u2019t provide enough information to say.<\/p>\n<p>Nor does the specification say if I can use loops, recursion or gotos in generating the data, versus printing it. I could generate the string very easily, just malloc enough space, convert the number into text, then place the characters into the string, with a space or control-linefeed as separator. The print itself is then a single command. Nothing else is needed.<\/p>\n<p>Nothing says if this is a permissible option or not, you have to interpret. And interpretation is not how you should ever program. GIGO.<\/p>\n<p>By loops, I am interpreting this as meaning any sort of iterative process, regardless of formulation. That is the standard meaning. But it occurs to me that repeat\/until (while) and until\/repeat (do) loops aren\u2019t always seen as loops as they\u2019re conditional on an event. Well, in C, so is a for loop. In many languages, for is fixed-length and so is regarded as a different sort of beast.<\/p>\n<p>Also, the \u201cgoto\u201d should be regarded with suspicion. Some languages have deliberately replaced \u201cgoto\u201d with \u201ccomefrom\u201d to ensure that the language complies with the requirement of not having gotos. I don\u2019t think many SE\u2019s regard that as anything other than slightly snarky humour, but there is no indication in the question as to whether a comefrom command would be disallowed if someone could code it into a header file. This is why sane programmers would interpret the question as prohibiting any form of condition\/jump operation, regardless of the nature of the condition or the jump.<\/p>\n<p>This leaves grammars. If the sequence from 1 to 100 can be specified in a grammar, then an external library performs the loop and you can print the result. There\u2019s still iteration, but it\u2019s both implicit and indirect. (When abstraction and black-box design is used to hide things from a boss, it\u2019s time to ask if the boss is suitable for you. These should only ever be used to clarify.)<\/p>\n<p>Ok, nothing in the question specifies a sequence, only the presence of the numbers. Nor does anything specify that numbers can\u2019t appear more than once. So if I use grammars to generate absolutely every permutation of the digits 0 to 9 and a space, then I am guaranteed every number from 0 to 100. There are other regexp functions that let us remove specific patterns, so if I remove all patterns in which one, two or three 0s have a space either side, I can eliminate 0. This just leaves 1 to 100, albeit with a great many single digit and double digit repeats. Repeats aren\u2019t talked about and we\u2019ve already screwed with the interpretation badly enough to make the question pointless, so we assume they\u2019re allowed.<\/p>\n<p>Questions of this kind are usually asked to analyze the methodology of thinking rather than the conclusion itself. They have to be, because the question means so many possible things that it is meaningless. The result you get is, thus, equally meaningless.<\/p><\/blockquote>\n<p>And then, this obvious Vim solution by <a href=\"https:\/\/www.quora.com\/How-would-you-write-a-C-program-to-print-1-to-100-without-loop-recursion-and-goto\/answer\/Gilles-Castel-1?srid=X7zK\">Gilles Castel<\/a>:<\/p>\n<pre class=\"brush: plain; light: true; title: ; notranslate\" title=\"\">\r\ni#include &lt;stdio.h&gt;&lt;CR&gt;int main(){&lt;CR&gt;\r\nprintf(&quot;%d &quot;,1);&lt;ESC&gt;qq0yyp&lt;C-A&gt;q98@qoreturn 0;\r\n<\/pre>\n<p>Not only he provided the explanation of what&#8217;s going on, but also \u00a0an animated GIF of how it looks:<\/p>\n<p><a href=\"https:\/\/i0.wp.com\/mamchenkov.net\/wordpress\/wp-content\/uploads\/2016\/06\/vim-macro.gif?ssl=1\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" data-attachment-id=\"26193\" data-permalink=\"https:\/\/mamchenkov.net\/wordpress\/2016\/06\/16\/how-would-you-write-a-c-program-to-print-1-to-100-without-loop-recursion-and-goto\/vim-macro\/\" data-orig-file=\"https:\/\/i0.wp.com\/mamchenkov.net\/wordpress\/wp-content\/uploads\/2016\/06\/vim-macro.gif?fit=948%2C569&amp;ssl=1\" data-orig-size=\"948,569\" data-comments-opened=\"1\" data-image-meta=\"{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}\" data-image-title=\"vim-macro\" data-image-description=\"\" data-image-caption=\"\" data-large-file=\"https:\/\/i0.wp.com\/mamchenkov.net\/wordpress\/wp-content\/uploads\/2016\/06\/vim-macro.gif?fit=660%2C396&amp;ssl=1\" class=\"aligncenter size-medium wp-image-26193\" src=\"https:\/\/i0.wp.com\/mamchenkov.net\/wordpress\/wp-content\/uploads\/2016\/06\/vim-macro-500x300.gif?resize=500%2C300&#038;ssl=1\" alt=\"vim-macro\" width=\"500\" height=\"300\" srcset=\"https:\/\/i0.wp.com\/mamchenkov.net\/wordpress\/wp-content\/uploads\/2016\/06\/vim-macro.gif?resize=500%2C300&amp;ssl=1 500w, https:\/\/i0.wp.com\/mamchenkov.net\/wordpress\/wp-content\/uploads\/2016\/06\/vim-macro.gif?resize=768%2C461&amp;ssl=1 768w\" sizes=\"auto, (max-width: 500px) 100vw, 500px\" \/><\/a><\/p>\n<p>And, yes, I&#8217;m not really interested in the actual C solutions to this problem.<\/p>\n<!-- google_ad_section_end -->\n","protected":false},"excerpt":{"rendered":"<!-- google_ad_section_start -->\n<p>I came across this Quora question today &#8211; &#8220;How would you write a C program to print 1 to 100 without loop, recursion and goto?&#8221; \u00a0As always, there is a plethora of answers, of which I liked the following two. Jonathan Day goes into how the specification can be misinterpreted in a variety of ways, &hellip; <a href=\"https:\/\/mamchenkov.net\/wordpress\/2016\/06\/16\/how-would-you-write-a-c-program-to-print-1-to-100-without-loop-recursion-and-goto\/\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">How would you write a C program to print 1 to 100 without loop, recursion and goto?<\/span><\/a><\/p>\n<!-- google_ad_section_end -->\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"How would you write a C program to print 1 to 100 without loop, recursion and goto? #programming","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":true,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2},"_links_to":"","_links_to_target":""},"categories":[1,18,62],"tags":[1494,3094],"keyring_services":[],"class_list":["post-26192","post","type-post","status-publish","format-standard","hentry","category-general","category-programming","category-technology","tag-c","tag-vim"],"aioseo_notices":[],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack-related-posts":[{"id":22377,"url":"https:\/\/mamchenkov.net\/wordpress\/2014\/08\/12\/what-is-the-shortest-and-most-effective-code-ever-written\/","url_meta":{"origin":26192,"position":0},"title":"What is the shortest and most effective code ever written?","author":"Leonid Mamchenkov","date":"August 12, 2014","format":false,"excerpt":"Quora runs the question, that by now has plenty of awesome answers. \u00a0But this one is my favorite so far: The 'true' program in Unix from the 1970s was an empty file. The shell interpreted that as a shell script which ran and resulted in no error status, so the\u2026","rel":"","context":"In &quot;All&quot;","block_context":{"text":"All","link":"https:\/\/mamchenkov.net\/wordpress\/category\/general\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":28121,"url":"https:\/\/mamchenkov.net\/wordpress\/2017\/10\/19\/what-is-the-hardest-part-about-learning-to-program\/","url_meta":{"origin":26192,"position":1},"title":"What is the hardest part about learning to program?","author":"Leonid Mamchenkov","date":"October 19, 2017","format":false,"excerpt":"Quora runs an interesting question - \"What is the hardest part about learning to program?\".\u00a0 As always, there are plenty of insightful answers with suggestions, tips, shared stories, research and statistical data, and more. For me personally, this answer in particular was useful, as I'm very well familiar with the\u2026","rel":"","context":"In &quot;All&quot;","block_context":{"text":"All","link":"https:\/\/mamchenkov.net\/wordpress\/category\/general\/"},"img":{"alt_text":"","src":"https:\/\/i0.wp.com\/mamchenkov.net\/wordpress\/wp-content\/uploads\/2017\/10\/dunning-kruger-effect-500x421.png?resize=350%2C200&ssl=1","width":350,"height":200},"classes":[]},{"id":7633,"url":"https:\/\/mamchenkov.net\/wordpress\/2004\/07\/02\/dictionary-of-algorithms-and-data-structures\/","url_meta":{"origin":26192,"position":2},"title":"Dictionary of Algorithms and Data Structures","author":"Leonid Mamchenkov","date":"July 2, 2004","format":false,"excerpt":"Dictionary of Algorithms and Data Structures is an excellent place to learn and refresh your memory. It is a huge resource that covers a whole bunch of algorithms and data structures, compiled by National Institute of Standards and Technology. Searching and sorting, trees and graphs, arrays and hashes, big O\u2026","rel":"","context":"In &quot;All&quot;","block_context":{"text":"All","link":"https:\/\/mamchenkov.net\/wordpress\/category\/general\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":27162,"url":"https:\/\/mamchenkov.net\/wordpress\/2016\/12\/22\/signs-that-youre-a-bad-programmer\/","url_meta":{"origin":26192,"position":3},"title":"Signs that you&#8217;re a bad programmer","author":"Leonid Mamchenkov","date":"December 22, 2016","format":false,"excerpt":"Software Engineering Tips shares some tips on how to figure out if you are a bad programmer, and how to remedy that. Signs that you're a bad programmer Inability to reason about code Poor understanding of the language's programming model Deficient research skills \/ Chronically poor knowledge of the platform's\u2026","rel":"","context":"In &quot;All&quot;","block_context":{"text":"All","link":"https:\/\/mamchenkov.net\/wordpress\/category\/general\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":11461,"url":"https:\/\/mamchenkov.net\/wordpress\/2008\/12\/12\/perl-vs-php-variable-scoping\/","url_meta":{"origin":26192,"position":4},"title":"Perl vs. PHP : variable scoping","author":"Leonid Mamchenkov","date":"December 12, 2008","format":false,"excerpt":"I've mentioned quite a few times that I am a big fan of Perl programming languge.\u00a0 However, most of my programming time these days is spent in PHP.\u00a0 The languages are often similar, with PHP having its roots in Perl, and Perl being such a influence in the world of\u2026","rel":"","context":"In &quot;All&quot;","block_context":{"text":"All","link":"https:\/\/mamchenkov.net\/wordpress\/category\/general\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":10397,"url":"https:\/\/mamchenkov.net\/wordpress\/2006\/08\/21\/the-truth-about-interviewing\/","url_meta":{"origin":26192,"position":5},"title":"The Truth About Interviewing","author":"Leonid Mamchenkov","date":"August 21, 2006","format":false,"excerpt":"Steve Yegge tells \"The Truth About Interviewing\". If you want a job at a company like Microsoft, Yahoo!, Apple, or Amazon.com, they're going to have high standards. It doesn't matter if you \"know how to program\". They're going to test you on algorithmic complexity analysis, advanced data structures, algorithm design,\u2026","rel":"","context":"In &quot;All&quot;","block_context":{"text":"All","link":"https:\/\/mamchenkov.net\/wordpress\/category\/general\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]}],"jetpack_sharing_enabled":true,"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/mamchenkov.net\/wordpress\/wp-json\/wp\/v2\/posts\/26192","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/mamchenkov.net\/wordpress\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/mamchenkov.net\/wordpress\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/mamchenkov.net\/wordpress\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/mamchenkov.net\/wordpress\/wp-json\/wp\/v2\/comments?post=26192"}],"version-history":[{"count":0,"href":"https:\/\/mamchenkov.net\/wordpress\/wp-json\/wp\/v2\/posts\/26192\/revisions"}],"wp:attachment":[{"href":"https:\/\/mamchenkov.net\/wordpress\/wp-json\/wp\/v2\/media?parent=26192"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mamchenkov.net\/wordpress\/wp-json\/wp\/v2\/categories?post=26192"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mamchenkov.net\/wordpress\/wp-json\/wp\/v2\/tags?post=26192"},{"taxonomy":"keyring_services","embeddable":true,"href":"https:\/\/mamchenkov.net\/wordpress\/wp-json\/wp\/v2\/keyring_services?post=26192"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}