{"id":6949,"date":"2025-05-08T00:01:20","date_gmt":"2025-05-08T07:01:20","guid":{"rendered":"https:\/\/c-for-dummies.com\/blog\/?p=6949"},"modified":"2025-04-26T10:04:32","modified_gmt":"2025-04-26T17:04:32","slug":"formatting-a-series-for-output-solution","status":"publish","type":"post","link":"https:\/\/c-for-dummies.com\/blog\/?p=6949","title":{"rendered":"Formatting a Series for Output &#8211; Solution"},"content":{"rendered":"<p>The challenge for <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=6937\">this month&#8217;s Exercise<\/a> is to output a series of numbers, 1 through 10, and to ensure that the final number doesn&#8217;t look dorky. I&#8217;m certain that&#8217;s what I asked for.<br \/>\n<!--more--><br \/>\nSpecifically, my point is to present a series like this:<\/p>\n<p><code>1, 2, 3, 4, 5, 6, 7, 8, 9, 10<\/code><\/p>\n<p>The goal is to ensure that a comma postfixes each of the other values, but not the last. Several solutions are possible. Here is the first one I came up with:<\/p>\n<h3><a href=\"https:\/\/github.com\/dangookin\/C-For-Dummies-Blog\/blob\/master\/2025_05-Exercise-a.c\" rel=\"noopener\" target=\"_blank\">2025_05-Exercise-a.c<\/a><\/h3>\n<pre class=\"screen\">\r\n#include &lt;stdio.h&gt;\r\n\r\nint main()\r\n{\r\n    int x;\r\n\r\n    for( x=1; x&lt;11; x++ )\r\n    {\r\n        if( x&lt;10 )\r\n            printf(\"%d, \",x);\r\n        else\r\n            printf(\"%d\\n\",x);\r\n    }\r\n\r\n    return 0;\r\n}<\/pre>\n<p>An <em>if<\/em> decision determines when the value of <code>x<\/code> is less than 10. If so, the trailing comma and space are output. Otherwise, at the end of the list, only a new line is output after the final value.<\/p>\n<p>One way to make this code more flexible is to set the looping value as a constant. So the <em>for<\/em> loop&#8217;s exit condition would be <code>maxval<\/code>, which means the <em>if<\/em> statement uses <code>maxval-1<\/code> as its test.<\/p>\n<p>I stole the following code from the Interwebs, which does the same thing as my solution but in a more elegant and potentially cryptic manner:<\/p>\n<h3><a href=\"https:\/\/github.com\/dangookin\/C-For-Dummies-Blog\/blob\/master\/2025_05-Exercise-b.c\" rel=\"noopener\" target=\"_blank\">2025_05-Exercise-b.c<\/a><\/h3>\n<pre class=\"screen\">\r\n<span class=\"comments\">\/* from RosettaCode *\/<\/span>\r\n#include &lt;stdio.h&gt;\r\n\r\nint main()\r\n{\r\n    int x;\r\n\r\n    for( x=1; x&lt;11; x++ )\r\n    {\r\n        printf(\"%d\",x);\r\n        printf( x==10 ? \"\\n\" : \", \");\r\n    }\r\n\r\n    return 0;\r\n}<\/pre>\n<p>Here, the <em>for<\/em> loop&#8217;s second <em>printf()<\/em> statement contains the ternary operator, which sets the text to output after each value. The two <em>printf()<\/em> statements could be combined to make a single looping statement:<\/p>\n<p><code>printf( (x==10 ? \"%d\\n\" : \"%d, \"),x);<\/code><\/p>\n<p>The ternary operator sets the <em>printf()<\/em> function&#8217;s format string, which is enclosed in parentheses. The presentation is wonderfully cryptic, plus it works.<\/p>\n<p>In all cases, the proper solution is output:<\/p>\n<p><code>1, 2, 3, 4, 5, 6, 7, 8, 9, 10<\/code><\/p>\n<p>No extra spaces are prefixed or suffixed to the output string, which makes it neat and tidy.<\/p>\n<p>I&#8217;m certain other solutions are available, each of which must be delightful in some way. It&#8217;s this type of programming exercise that I find more engaging than some of the head-scratcher problems.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>The challenge for this month&#8217;s Exercise is to output a series of numbers, 1 through 10, and to ensure that the final number doesn&#8217;t look dorky. I&#8217;m certain that&#8217;s what I asked for.<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[5],"tags":[],"class_list":["post-6949","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\/6949","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=6949"}],"version-history":[{"count":4,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/6949\/revisions"}],"predecessor-version":[{"id":6972,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/6949\/revisions\/6972"}],"wp:attachment":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=6949"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=6949"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=6949"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}