{"id":6979,"date":"2025-05-24T00:01:09","date_gmt":"2025-05-24T07:01:09","guid":{"rendered":"https:\/\/c-for-dummies.com\/blog\/?p=6979"},"modified":"2025-05-24T08:32:10","modified_gmt":"2025-05-24T15:32:10","slug":"outputting-a-table-of-math-stuff","status":"publish","type":"post","link":"https:\/\/c-for-dummies.com\/blog\/?p=6979","title":{"rendered":"Outputting a Table of Math Stuff"},"content":{"rendered":"<p>Though I&#8217;m admittedly not good at math (at least according to my grades in school), I enjoy playing with math stuff on the computer. Programming allows me to mess with numbers and values without the risk of that ugly red mark and the need to wonder how a D or an F on my transcript affects my overall GPA.<br \/>\n<!--more--><br \/>\nOf all the math I suffered in school, I enjoyed geometry the best. I know, for example, that the sum of the internal angles of a triangle is 180&deg;. For a square, the sum in 360&deg;. The formula for the number of sides on a polygon to yield the total of all the internal angles is: <code>(n - 2) * 180<\/code>.<\/p>\n<p>This formula makes sense. A two-sided object would have no internal angles. A one-sided object would have seriously messed-up mathematical results as the sum of internal angles.<\/p>\n<p>To obtain the number of degrees for each angle, you divide the result of <code>(n - 2) * 180<\/code> by <em>n<\/em>, which is what I did in the following code:<\/p>\n<h3><a href=\"https:\/\/github.com\/dangookin\/C-For-Dummies-Blog\/blob\/master\/2025_05_24-Lesson.c\" rel=\"noopener\" target=\"_blank\">2025_05_24-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 sides,degrees;\r\n\r\n    printf(\"Sides\\tAngles\\tTotal (degrees)\\n\");\r\n    for(sides = 3; sides&lt;=12; sides++ )\r\n    {\r\n        degrees = (sides - 2) * 180;\r\n        printf(\"%5d\\t%6d\\t%5d\\n\",\r\n                sides,\r\n                degrees\/sides,\r\n                degrees\r\n              );\r\n    }\r\n\r\n    return 0;\r\n}<\/pre>\n<p>The statement <code>degrees = (sides - 2) * 180;<\/code> stores the total number of angles for a <code>sides<\/code>-sided object. This value, along with variable <code>sides<\/code>, is output in the <em>printf()<\/em> statement, along with the size of each internal angle:<\/p>\n<p><code>Sides&nbsp;&nbsp;&nbsp;Angles&nbsp;&nbsp;Total&nbsp;(degrees)<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;3&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;60&nbsp;&nbsp;&nbsp;&nbsp;180<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;4&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;90&nbsp;&nbsp;&nbsp;&nbsp;360<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;5&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;108&nbsp;&nbsp;&nbsp;&nbsp;540<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;6&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;120&nbsp;&nbsp;&nbsp;&nbsp;720<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;7&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;128&nbsp;&nbsp;&nbsp;&nbsp;900<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;8&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;135&nbsp;&nbsp;&nbsp;1080<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;9&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;140&nbsp;&nbsp;&nbsp;1260<br \/>\n&nbsp;&nbsp;&nbsp;10&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;144&nbsp;&nbsp;&nbsp;1440<br \/>\n&nbsp;&nbsp;&nbsp;11&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;147&nbsp;&nbsp;&nbsp;1620<br \/>\n&nbsp;&nbsp;&nbsp;12&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;150&nbsp;&nbsp;&nbsp;1800<\/code><\/p>\n<p>The point here isn&#8217;t this simple program, but how to craft the table, shown above. Often I struggle as a programmer to present such information in an orderly fashion, to ensure that all the columns line up without having to resort to fancy math or a library like Ncurses to precisely set the cursor and present the details.<\/p>\n<p>Here&#8217;s the trick I use:<\/p>\n<p>Write the table header first:<\/p>\n<p><code>printf(\"Sides\\tAngles\\tTotal (degrees)\\n\");<\/code><\/p>\n<p>Each of the column headings is separated by a tab, <code>\\t<\/code>. When outputting the columns in the table, separate each by tab, but use width values in the placeholders that match the character length from the column heading:<\/p>\n<p>Sides has 5 letters<br \/>\nAngles has 6 letters<br \/>\nTotal has 5 letters<\/p>\n<p>Here is the <em>printf()<\/em> formatting string to output each row in the table:<\/p>\n<p><code>\"%5d\\t%6d\\t%5d\\n\"<\/code><\/p>\n<p>Each <code>%d<\/code> placeholder has the same width value as its corresponding header: 5, 6, and 5. The tab escape character is sandwiched between each placeholder. The output worked for me the first time without the need to go back and adjust things &mdash; which I&#8217;ve often suffered doing in the past.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Angles and tables and math and stuff, but mostly a table formatting tip. <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=6979\">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-6979","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\/6979","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=6979"}],"version-history":[{"count":5,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/6979\/revisions"}],"predecessor-version":[{"id":7001,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/6979\/revisions\/7001"}],"wp:attachment":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=6979"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=6979"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=6979"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}