{"id":5511,"date":"2022-09-10T00:01:45","date_gmt":"2022-09-10T07:01:45","guid":{"rendered":"https:\/\/c-for-dummies.com\/blog\/?p=5511"},"modified":"2022-09-17T07:45:30","modified_gmt":"2022-09-17T14:45:30","slug":"generating-a-table-from-stream-output","status":"publish","type":"post","link":"https:\/\/c-for-dummies.com\/blog\/?p=5511","title":{"rendered":"Generating a Table from Stream Output"},"content":{"rendered":"<p><a href=\"https:\/\/c-for-dummies.com\/blog\/?p=5499&#038;\">This month&#8217;s Exercise<\/a> searched for and output the first 100 cyclops numbers. But instead of spewing them out in a long column, my solution set them in a table. The values marched across the screen in neat rows. This trick is rather easy to accomplish, but in my solution I wanted some flexibility with the column number.<br \/>\n<!--more--><br \/>\nI took code from the relevant part of my Exercise solution and rebuilt it here to output sequential values:<\/p>\n<h3><a href=\"https:\/\/github.com\/dangookin\/C-For-Dummies-Blog\/blob\/master\/2022_09_10-Lesson.c\" rel=\"noopener\" target=\"_blank\">2022_09_10-Lesson.c<\/a><\/h3>\n<pre class=\"screen\">\r\n#include &lt;stdio.h&gt;\r\n\r\nint main()\r\n{\r\n    const int columns = 9;\r\n    const int values = 100;\r\n    int x,y,count;\r\n\r\n    x = 0;\r\n    count = values;\r\n    while( count )\r\n    {\r\n        for( y=0; y&lt;columns; y++ )\r\n        {\r\n            printf(\"%3d\",(x*columns)+y );\r\n            count--;                <span class=\"comments\">\/* decrement the count *\/<\/span>\r\n            if( !count )            <span class=\"comments\">\/* if count is zero, stop *\/<\/span>\r\n                break;\r\n            if( y&lt;columns-1)\r\n                putchar('\\t');\r\n        }\r\n        x++;\r\n        putchar('\\n');\r\n    }\r\n\r\n\r\n    return(0);\r\n}<\/pre>\n<p>The number of <code>columns<\/code> is set as a constant, as is the <code>values<\/code> count. I wanted the table output to handle a variable number of columns, but also to deal with a situation when the last row doesn&#8217;t have a value for each column.<\/p>\n<p>The nested <em>for<\/em> loop handles the values across a row. The key to outputting the values sequentially is found in the <em>printf()<\/em> statement: <code>(x*columns)+y)<\/code> This expression bases each row&#8217;s values on variable <code>x<\/code>, which is the row count, times the total number of columns. Adding the <code>y<\/code> value completes the task, as each value is output sequentially, left-to-right, top-down, as shown in Figure 1.<\/p>\n<div id=\"attachment_5512\" style=\"width: 594px\" class=\"wp-caption aligncenter\"><img loading=\"lazy\" decoding=\"async\" aria-describedby=\"caption-attachment-5512\" src=\"https:\/\/c-for-dummies.com\/blog\/wp-content\/uploads\/2022\/08\/0910-figure1-1024x400.png\" alt=\"screen output\" width=\"584\" height=\"228\" class=\"size-large wp-image-5512\" srcset=\"https:\/\/c-for-dummies.com\/blog\/wp-content\/uploads\/2022\/08\/0910-figure1-1024x400.png 1024w, https:\/\/c-for-dummies.com\/blog\/wp-content\/uploads\/2022\/08\/0910-figure1-300x117.png 300w, https:\/\/c-for-dummies.com\/blog\/wp-content\/uploads\/2022\/08\/0910-figure1-768x300.png 768w, https:\/\/c-for-dummies.com\/blog\/wp-content\/uploads\/2022\/08\/0910-figure1-500x195.png 500w, https:\/\/c-for-dummies.com\/blog\/wp-content\/uploads\/2022\/08\/0910-figure1.png 1126w\" sizes=\"auto, (max-width: 584px) 100vw, 584px\" \/><p id=\"caption-attachment-5512\" class=\"wp-caption-text\">Figure 1. The output showing 100 values in nine columns.<\/p><\/div>\n<p>The expression <code>x*columns<\/code> increments the starting (first column) value by the value of columns: 0, 9, 18, and so on. The rest of the row then falls into place based on the value of variable <code>y<\/code>.<\/p>\n<p>This code is constructed so that the column number can be adjusted without requiring the loops to be rebuilt. If I set the value of constant <code>columns<\/code> to five, the output changes as shown in Figure 2.<\/p>\n<div id=\"attachment_5513\" style=\"width: 268px\" class=\"wp-caption aligncenter\"><img loading=\"lazy\" decoding=\"async\" aria-describedby=\"caption-attachment-5513\" src=\"https:\/\/c-for-dummies.com\/blog\/wp-content\/uploads\/2022\/08\/0910-figure2-258x300.png\" alt=\"screen output\" width=\"258\" height=\"300\" class=\"size-medium wp-image-5513\" srcset=\"https:\/\/c-for-dummies.com\/blog\/wp-content\/uploads\/2022\/08\/0910-figure2-258x300.png 258w, https:\/\/c-for-dummies.com\/blog\/wp-content\/uploads\/2022\/08\/0910-figure2.png 620w\" sizes=\"auto, (max-width: 258px) 100vw, 258px\" \/><p id=\"caption-attachment-5513\" class=\"wp-caption-text\">Figure 2. Table output when the <code>columns<\/code> constant is set to five.<\/p><\/div>\n<p>When the rows and columns are even, as shown in Figure 2, the last row is full. But in Figure 1, you see only one item on the final row. The code that handles this is an <em>if<\/em> decision after the <em>printf()<\/em> statement outputs the value:<\/p>\n<p><code>if( !count )<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;break;<\/code><\/p>\n<p>Variable <code>count<\/code> is decremented after output. If its value is zero, values are exhausted and the row can end: The <em>for<\/em> loop stops and a newline is output. Because <code>count<\/code> is zero, the outer <em>while<\/em> loop stops as well.<\/p>\n<p>Table output raises the difficulty level over just spewing out each number on a line by itself. This method, rows across, is easier than coding a table where the values are set in columns marching left-to-right. I cover this type of table output in <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=5525\">next week&#8217;s Lesson<\/a>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>It&#8217;s much neater to output a long array of values in a table than as a tall list. <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=5511\">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-5511","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\/5511","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=5511"}],"version-history":[{"count":6,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/5511\/revisions"}],"predecessor-version":[{"id":5554,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/5511\/revisions\/5554"}],"wp:attachment":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=5511"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=5511"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=5511"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}