{"id":1006,"date":"2014-10-08T00:01:37","date_gmt":"2014-10-08T07:01:37","guid":{"rendered":"http:\/\/c-for-dummies.com\/blog\/?p=1006"},"modified":"2014-09-27T08:15:27","modified_gmt":"2014-09-27T15:15:27","slug":"the-inverse-pyramid-solution","status":"publish","type":"post","link":"https:\/\/c-for-dummies.com\/blog\/?p=1006","title":{"rendered":"The Inverse Pyramid &#8211; Solution"},"content":{"rendered":"<p>The task for this month&#8217;s <a href=\" http:\/\/c-for-dummies.com\/blog\/?p=993\">Exercise<\/a> was to create code that outputs an inverse number pyramid.<\/p>\n<p>Here&#8217;s what such a thing looks like:<\/p>\n<pre><code>0000000000\r\n999999999\r\n88888888\r\n7777777\r\n666666\r\n55555\r\n4444\r\n333\r\n22\r\n1<\/code><\/pre>\n<p><!--more--><br \/>\nOf the many solutions, I chose to use a nested <em>for<\/em> loop: The outer loop is a simple countdown loop, going from 10 to 1. The inner loop displays a quantity digits based on the value of the outer loop: 10, 9, 8, on down to 1.<\/p>\n<p><a href=\"http:\/\/c-for-dummies.com\/blog\/wp-content\/uploads\/2014\/09\/10exercise.c\">Click here<\/a> to view\/download the code.<\/p>\n<p>Here is the line-by-line description of my solution:<\/p>\n<pre class=\"screen\">\r\n#include &lt;stdio.h&gt;\r\n\r\nint main()\r\n{\r\n    int pyramid,row;<\/pre>\n<p>You need two variables to create a nested <em>for<\/em> loop. <code>pyramid<\/code> is the outer variable, the one that counts down from 10 to 1. <code>row<\/code> is the inner variable, which displays the repeated numbers in the output.<\/p>\n<pre class=\"screen\">\r\n    for(pyramid=10;pyramid&tg;0;pyramid--)\r\n    {<\/pre>\n<p>The outer <em>for<\/em> loop counts from 10 down to 1 by using the <code>pyramid<\/code> variable. The <code>&gt;0<\/code> part keeps the loop spinning until 1 is reached.<\/p>\n<pre class=\"screen\">\r\n        for(row=0;row&lt;pyramid;row++)<\/pre>\n<p>For each repetition of the outer <em>for<\/em> loop, the inner <em>for<\/em> loop repeats. The <code>row<\/code> variable is used to count up from 0 through the value of variable <code>pyramid<\/code>.<\/p>\n<pre class=\"screen\">\r\n            printf(\"%d\",pyramid%10);\r\n        putchar('\\n');\r\n    }<\/pre>\n<p>The inner loop uses a <em>printf()<\/em> function to output each value. The <code>%d<\/code> placeholder displays an <em>int<\/em> value.<\/p>\n<p>The value is modified by using the <code>%<\/code> (modulus) operator. That way, the value 10 displays as 0, not 10. That&#8217;s really the key to the entire operation. The modulus operator gives the remainder of a division calculation, the left-overs. When you perform <code>10 % 10<\/code> you get a result of 0 because 10 evenly goes into 10.<\/p>\n<p>The <code>putchar('\\n')<\/code> statement adds a newline after each row is output.<\/p>\n<p>You could also output the number pyramid by using an <em>if<\/em> statement, such as:<\/p>\n<pre><code>if(pyramid > 10)\r\n    putchar('0');<\/code><\/pre>\n<p>That&#8217;s another solution, although the more elegant solution is to use the modulus operator. In fact, experienced programmers would refer to the <em>if<\/em> solution as a <em>kludge<\/em>, which means a clumsy work-around.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>The task for this month&#8217;s Exercise was to create code that outputs an inverse number pyramid. Here&#8217;s what such a thing looks like: 0000000000 999999999 88888888 7777777 666666 55555 4444 333 22 1<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[5],"tags":[],"class_list":["post-1006","post","type-post","status-publish","format-standard","hentry","category-solution"],"_links":{"self":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/1006","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=1006"}],"version-history":[{"count":5,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/1006\/revisions"}],"predecessor-version":[{"id":1013,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/1006\/revisions\/1013"}],"wp:attachment":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1006"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1006"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1006"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}