{"id":6937,"date":"2025-05-01T00:01:57","date_gmt":"2025-05-01T07:01:57","guid":{"rendered":"https:\/\/c-for-dummies.com\/blog\/?p=6937"},"modified":"2025-05-08T14:23:08","modified_gmt":"2025-05-08T21:23:08","slug":"formatting-a-series-for-output","status":"publish","type":"post","link":"https:\/\/c-for-dummies.com\/blog\/?p=6937","title":{"rendered":"Formatting a Series for Output"},"content":{"rendered":"<h2>Difficulty: &#9733; &#9733; &#9734; &#9734;<\/h2>\n<p>Here is an issue that crops up often in programming, specifically when outputting data in a series: How do you separate items in the series and not make the last item look dorky? It&#8217;s tricky.<br \/>\n<!--more--><br \/>\nFor example, consider the number sequence:<\/p>\n<p><code>1, 2, 3, 4, 5, 6, 7, 8, 9, 10<\/code><\/p>\n<p>The final value, 10, isn&#8217;t followed by a comma. This arrangement is how data is how humans (well, English-reading humans)  read such a series. Commas separate the items. The final item lacks a comma and is instead followed by a period and a newline or just the newline (above).<\/p>\n<p>Alternatively, you can eschew the commas and just follow each value with a space:<\/p>\n<p><code>1 2 3 4 5 6 7 8 9 10&nbsp;<\/code><\/p>\n<p>The problem here is that the final value is also followed by a space, plus you must add in the newline.<\/p>\n<p>A trick I use is to prefix the values with a space, which kinda solves the problem but not entirely:<\/p>\n<p><code>&nbsp;1 2 3 4 5 6 7 8 9 10<\/code><\/p>\n<p>Therefore, a better and more rational solution should be available, which is the topic for this month&#8217;s Exercise.<\/p>\n<p>Your challenge is to write a loop that outputs values 1 through 10. After the first nine values are output, add a comma and a space. The tenth value (10) is output followed by a newline.<\/p>\n<p>This exercise may not seem very deep as far as C challenges go, but it does present multiple solutions. I found two. How many can you come up with?<\/p>\n<p>Here&#8217;s a code skeleton to get you started:<\/p>\n<h3><a href=\"https:\/\/github.com\/dangookin\/C-For-Dummies-Blog\/blob\/master\/2025_05_01-Lesson.c\" rel=\"noopener\" target=\"_blank\">2025_05_01-Lesson.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        printf(\"%d, \",x);\r\n    }\r\n\r\n    return 0;\r\n}<\/pre>\n<p><a href=\"https:\/\/c-for-dummies.com\/blog\/?p=6949\">Click here<\/a> to view my solutions.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>The problem is when to separate and when to terminate? <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=6937\">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":[3],"tags":[],"class_list":["post-6937","post","type-post","status-publish","format-standard","hentry","category-exercise"],"_links":{"self":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/6937","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=6937"}],"version-history":[{"count":5,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/6937\/revisions"}],"predecessor-version":[{"id":6984,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/6937\/revisions\/6984"}],"wp:attachment":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=6937"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=6937"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=6937"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}