{"id":5525,"date":"2022-09-17T00:01:42","date_gmt":"2022-09-17T07:01:42","guid":{"rendered":"https:\/\/c-for-dummies.com\/blog\/?p=5525"},"modified":"2022-09-10T10:33:05","modified_gmt":"2022-09-10T17:33:05","slug":"vertical-table-output","status":"publish","type":"post","link":"https:\/\/c-for-dummies.com\/blog\/?p=5525","title":{"rendered":"Vertical Table Output"},"content":{"rendered":"<p>Generating a table where the items march left-to-right and then down the page is uncomplicated with stream output. What&#8217;s more difficult is outputting a table where the data drops in columns top-down before going left-to-right. Performing this feat requires no screen\/cursor manipulation, only clever coding.<br \/>\n<!--more--><br \/>\nThe key to getting the output correct is to know two tidbits about the data: the total number of items and either the rows or columns desired. You must calculate the grid size, then output values in the stream to fill the table.<\/p>\n<p>When given the number of values to output and columns desired, I calculate the rows as: <code>rows = values\/columns+1<\/code> The <code>+1<\/code> ensures that the final row is output given that these are integer values.<\/p>\n<p>In <a href=\"https:\/\/c-for-dummies.com\/blog\/wp-admin\/post.php?post=5511\">last week&#8217;s Lesson<\/a>, the calculation made to generate a left-to-right table is: <code>(x*columns)+y)<\/code> For the top-to-bottom table, the calculation is <code>x+(y*rows)<\/code> &mdash; almost the opposite construction. The code from the previous Lesson must be modified further because the blank items appear at the end of a row, not at the bottom of a column.<\/p>\n<h3><a href=\"https:\/\/github.com\/dangookin\/C-For-Dummies-Blog\/blob\/master\/2022_09_17-Lesson.c\" rel=\"noopener\" target=\"_blank\">2022_09_17-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,rows,count,v;\r\n\r\n    x = 0;                <span class=\"comments\">\/* track the rows *\/<\/span>\r\n    count = values;\r\n    rows = values\/columns+1;    <span class=\"comments\">\/* how many rows are needed, max? *\/<\/span>\r\n    while( x&lt;rows )\r\n    {\r\n        <span class=\"comments\">\/* in stream output, columns alway appear first *\/<\/span>\r\n        for( y=0; y&lt;columns; y++ )\r\n        {\r\n            <span class=\"comments\">\/* calculate the value to output *\/<\/span>\r\n            v = x+(y*rows);            <span class=\"comments\">\/* this value could also be\r\n                                       an array element number *\/<\/span>\r\n            if( v&lt;values )            <span class=\"comments\">\/* output only numbers in range *\/<\/span>\r\n            {\r\n                printf(\"%3d\",v);\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            }\r\n            if( y&lt;columns-1)            <span class=\"comments\">\/* set tabs between numbers *\/<\/span>\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>Constants for the <code>columns<\/code> and number of <code>values<\/code> are set as shown on Lines 5 and 6. Play with these numbers to generate different size table arrangements.<\/p>\n<p>As with last week&#8217;s example, rows are output first as stream output works left-to-right regardless of how the table&#8217;s contents lay out. The <em>while<\/em> loop handles rows with the <em>for<\/em> loop going across with column values.<\/p>\n<p>The value output is within the range of zero through constant <code>values<\/code>. It&#8217;s calculated at Line 18 as: <code>v = x+(y*rows)<\/code>. The current row is added to the column value multiplied by the total number of rows. This expression works out how the column values jump across the table, as shown in the output in Figure 1.<\/p>\n<div id=\"attachment_5526\" style=\"width: 594px\" class=\"wp-caption aligncenter\"><img loading=\"lazy\" decoding=\"async\" aria-describedby=\"caption-attachment-5526\" src=\"https:\/\/c-for-dummies.com\/blog\/wp-content\/uploads\/2022\/09\/0917-figure1-1024x410.png\" alt=\"screen output\" width=\"584\" height=\"234\" class=\"size-large wp-image-5526\" srcset=\"https:\/\/c-for-dummies.com\/blog\/wp-content\/uploads\/2022\/09\/0917-figure1-1024x410.png 1024w, https:\/\/c-for-dummies.com\/blog\/wp-content\/uploads\/2022\/09\/0917-figure1-300x120.png 300w, https:\/\/c-for-dummies.com\/blog\/wp-content\/uploads\/2022\/09\/0917-figure1-768x308.png 768w, https:\/\/c-for-dummies.com\/blog\/wp-content\/uploads\/2022\/09\/0917-figure1-500x200.png 500w, https:\/\/c-for-dummies.com\/blog\/wp-content\/uploads\/2022\/09\/0917-figure1.png 1098w\" sizes=\"auto, (max-width: 584px) 100vw, 584px\" \/><p id=\"caption-attachment-5526\" class=\"wp-caption-text\">Figure 1. The table&#8217;s values march down rows first, then across columns.<\/p><\/div>\n<p>The <em>if<\/em> test at Line 20 ensures that the final column doesn&#8217;t overflow. If the value calculated <code>v<\/code> is less than the total <code>values<\/code>, the value is output and the total <code>count<\/code> is decremented. You can see when this condition is trigged in Figure 1, no value to the right of 88 appears in the fifth row, and on down the final column.<\/p>\n<p>Remember that <code>value<\/code> can be an index into a buffer or the element number in an array. This is how data collected can be output in a top-down column table without offending stream output&#8217;s left-to-right way of doing things.<\/p>\n<p>I find this type of table arrangement to be more impressive than last week&#8217;s example and the code presented for <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=5499\">this month&#8217;s Exercise<\/a>. Face it, left-to-right tables aren&#8217;t difficult to generate with stream output. This top-to-bottom table raises the bar, but it&#8217;s possible only when you know the total number of items and the column or row count.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>It&#8217;s amazing what tricks you can pull with stream output when you&#8217;re willing to put effort into your code. <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=5525\">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-5525","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\/5525","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=5525"}],"version-history":[{"count":4,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/5525\/revisions"}],"predecessor-version":[{"id":5537,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/5525\/revisions\/5537"}],"wp:attachment":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=5525"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=5525"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=5525"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}