{"id":4651,"date":"2021-03-08T00:01:45","date_gmt":"2021-03-08T08:01:45","guid":{"rendered":"https:\/\/c-for-dummies.com\/blog\/?p=4651"},"modified":"2021-03-13T09:33:14","modified_gmt":"2021-03-13T17:33:14","slug":"positive-negative-or-zero-solution","status":"publish","type":"post","link":"https:\/\/c-for-dummies.com\/blog\/?p=4651","title":{"rendered":"Positive, Negative, or Zero &#8211; Solution"},"content":{"rendered":"<p>Your challenge for <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=4640\">this month&#8217;s Exercise<\/a> is to write a <em>sign()<\/em> function, which returns 1, 0, or -1 depending on the sign of an integer. A relatively simple thing to code &mdash; I hope!<br \/>\n<!--more--><br \/>\nFor my solution, the <em>sign()<\/em> function uses two comparisons. Assuming <code>a<\/code> is the value passed:<\/p>\n<p><code>if( a&lt;0 ) return(-1);<\/code><\/p>\n<p>If <code>a<\/code> is less than zero, -1 is returned. Otherwise:<\/p>\n<p><code>if( a&gt;0 ) return(1);<\/code><\/p>\n<p>When <code>a<\/code> is greater than zero, 1 is returned.<\/p>\n<p>The only option left is zero, which is handled by a single <em>return<\/em> statement:<\/p>\n<p><code>return(0);<\/code><\/p>\n<p>Here is the full code:<\/p>\n<h3><a href=\"https:\/\/github.com\/dangookin\/C-For-Dummies-Blog\/blob\/master\/2021_03-Exercise.c\" rel=\"noopener\" target=\"_blank\">2021_03-Exercise.c<\/a><\/h3>\n<pre class=\"screen\">\r\n#include &lt;stdio.h&gt;\r\n\r\nint sign(int a)\r\n{\r\n    <span class=\"comments\">\/* value is negative *\/<\/span>\r\n    if( a&lt;0 ) return(-1);\r\n\r\n    <span class=\"comments\">\/* value is positive *\/<\/span>\r\n    if( a&gt;0 ) return(1);\r\n\r\n    <span class=\"comments\">\/* value is zero *\/<\/span>\r\n    return(0);\r\n}\r\n\r\nint main()\r\n{\r\n    int values[20] = { -34, 27, 0, 48, -25, 28, -55,\r\n        66, -17, -78, 0, -20, 40, -98, 63, -44, 59,\r\n        6, 65, 90\r\n    };\r\n    int x;\r\n\r\n    for( x=0; x&lt;20; x++ )\r\n    {\r\n        printf(\"Sign of %3d is \", values[x] );\r\n        switch( sign( values[x] ))\r\n        {\r\n            case -1:\r\n                puts(\"negative\");\r\n                break;\r\n            case 1:\r\n                puts(\"positive\");\r\n                break;\r\n            case 0:\r\n                puts(\"zero\");\r\n                break;\r\n            default:\r\n                puts(\"unknown\");\r\n        }\r\n    }\r\n\r\n    return(0);\r\n}<\/pre>\n<p>The output reflects the value returned by the <em>sign()<\/em> function; text is appended to the value reflecting the proper sign: negative, positive, or zero:<\/p>\n<pre><code>Sign of -34 is negative\r\nSign of  27 is positive\r\nSign of   0 is zero\r\nSign of  48 is positive\r\nSign of -25 is negative\r\nSign of  28 is positive\r\nSign of -55 is negative\r\nSign of  66 is positive\r\nSign of -17 is negative\r\nSign of -78 is negative\r\nSign of   0 is zero\r\nSign of -20 is negative\r\nSign of  40 is positive\r\nSign of -98 is negative\r\nSign of  63 is positive\r\nSign of -44 is negative\r\nSign of  59 is positive\r\nSign of   6 is positive\r\nSign of  65 is positive\r\nSign of  90 is positive<\/code><\/pre>\n<p>I hope you found the solution easy and enjoyed the challenge. Remember, my solution represents only one approach; as long as your <em>sign()<\/em> function behaves similarly, it&#8217;s good.<\/p>\n<p>A <em>sign()<\/em> function comes in handy, which is probably why the BASIC language featured the <code>SGN<\/code> command that performs the same feat. This function is used in <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=4657\">an upcoming Lesson<\/a> where knowing the sign of an integer plays an important role.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Your challenge for this month&#8217;s Exercise is to write a sign() function, which returns 1, 0, or -1 depending on the sign of an integer. A relatively simple thing to code &mdash; I hope!<\/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-4651","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\/4651","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=4651"}],"version-history":[{"count":4,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/4651\/revisions"}],"predecessor-version":[{"id":4669,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/4651\/revisions\/4669"}],"wp:attachment":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=4651"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=4651"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=4651"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}