{"id":5045,"date":"2021-11-08T00:01:24","date_gmt":"2021-11-08T08:01:24","guid":{"rendered":"https:\/\/c-for-dummies.com\/blog\/?p=5045"},"modified":"2021-11-09T07:08:09","modified_gmt":"2021-11-09T15:08:09","slug":"the-power-grid-solution","status":"publish","type":"post","link":"https:\/\/c-for-dummies.com\/blog\/?p=5045","title":{"rendered":"The Power Grid &#8211; Solution"},"content":{"rendered":"<p>I hope you didn&#8217;t find <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=5031\">this month&#8217;s Exercise<\/a> too difficult. I do occasionally like to toss in an easy one. But for me, the big task was getting the output just right.<br \/>\n<!--more--><br \/>\nIn C, the <em>pow()<\/em> function outputs exponents. It&#8217;s prototyped in the <code>math.h<\/code> header file. Further, in some environments (hello, Linux), you must link in the math library when building the program: Use the <code>-lm<\/code> switch, where <code>-l<\/code> (little L) specifies a library and <code>m<\/code> is the math library. This switch must be specified last at the command prompt.<\/p>\n<p>To output the table neat and tidy, I use a <em>printf()<\/em> statement inside the nested loop. The goal is to generate the cells in the table:<\/p>\n<p><code>printf(\"%d^%d = %4.0f\\t\",a,b,pow(a,b));<\/code><\/p>\n<p>The <code>%4.0f<\/code> placeholder ensures that each floating point value is output in a width of 4 characters with no characters output after the decimal. This placeholder is followed by the <code>\\t<\/code> (tab) character, which aligns the cells.<\/p>\n<p>Here is the full code for my solution:<\/p>\n<h3><a href=\"https:\/\/github.com\/dangookin\/C-For-Dummies-Blog\/blob\/master\/2021_11-Exercise.c\" rel=\"noopener\" target=\"_blank\">2021_11-Exercise.c<\/a><\/h3>\n<pre class=\"screen\">\r\n#include &lt;stdio.h&gt;\r\n#include &lt;math.h&gt;\r\n\r\nint main()\r\n{\r\n    int a,b;\r\n\r\n    for( a=0; a&lt;7; a++ )\r\n    {\r\n        for( b=1; b&lt;=5; b++ )\r\n        {\r\n            printf(\"%d^%d = %4.0f\\t\",a,b,pow(a,b));\r\n        }\r\n        putchar('\\n');\r\n    }\r\n\r\n    return(0);\r\n}<\/pre>\n<p>See? Not bad &mdash; once you get the <em>printf()<\/em> formatting done properly.<\/p>\n<p>I hope your solution also met with success and that you explored possibilities for different types of table formatting. More power to you if you added a header row, and special C language bonus points are awarded if you labeled the rows as well.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I hope you didn&#8217;t find this month&#8217;s Exercise too difficult. I do occasionally like to toss in an easy one. But for me, the big task was getting the output just right.<\/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-5045","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\/5045","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=5045"}],"version-history":[{"count":4,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/5045\/revisions"}],"predecessor-version":[{"id":5065,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/5045\/revisions\/5065"}],"wp:attachment":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=5045"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=5045"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=5045"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}