{"id":4640,"date":"2021-03-01T00:01:10","date_gmt":"2021-03-01T08:01:10","guid":{"rendered":"https:\/\/c-for-dummies.com\/blog\/?p=4640"},"modified":"2021-03-10T08:04:21","modified_gmt":"2021-03-10T16:04:21","slug":"positive-negative-or-zero","status":"publish","type":"post","link":"https:\/\/c-for-dummies.com\/blog\/?p=4640","title":{"rendered":"Positive, Negative, or Zero"},"content":{"rendered":"<p>The <em>strcmp()<\/em> function returns a value based on the comparison of two strings. The value is zero when the strings match, otherwise the value is positive or negative depending on how the strings compare. This result makes me wonder which other functions can return positive, negative, or zero values and whether the C library has a <em>sign()<\/em> function or similar that helps make such a determination.<br \/>\n<!--more--><br \/>\nLong story short: It doesn&#8217;t.<\/p>\n<p>Back when I was a BASIC language wizard, I knew the <code>SGN<\/code> command. It returns -1, 0, or 1 based on the sign of a an integer. Such a function, absent from the standard C library seems rather simple to code, which is your task for this month&#8217;s Exercise: Write a <em>sign()<\/em> function.<\/p>\n<p>The <em>sign()<\/em> function accepts an integer value as input. It returns -1, 0, or 1 depending on the value&#8217;s sign: negative, zero, or positive. This functionality is the same as the old BASIC <code>SGN<\/code> command.<\/p>\n<p>To assist you with a solution, here is a code skeleton:<\/p>\n<h3><a href=\"https:\/\/github.com\/dangookin\/C-For-Dummies-Blog\/blob\/master\/2021_03_01-Lesson.c\" rel=\"noopener\" target=\"_blank\">2021_03_01-Lesson.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}\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 code above lacks guts for the <em>sign()<\/em> function. Your task is to code the guts.<\/p>\n<p>In the sample code skeleton, each integer in the <code>values[]<\/code> array is passed to the <em>sign()<\/em> function. The result is used immediately in a <em>switch-case<\/em> structure, where text is output based on the value returned: negative, positive, or zero. A <em>default<\/em> condition is added Just In Case.<\/p>\n<p>Please complete the Exercise by coding the <em>sign()<\/em> function. Upon success, the output looks like this:<\/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>Try this Exercise on your own before you check <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=4651\">my answer<\/a>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>The C language lacks a function that returns the sign of an integer. So why not write one yourself? <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=4640\">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-4640","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\/4640","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=4640"}],"version-history":[{"count":5,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/4640\/revisions"}],"predecessor-version":[{"id":4668,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/4640\/revisions\/4668"}],"wp:attachment":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=4640"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=4640"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=4640"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}