{"id":6313,"date":"2024-04-01T00:01:41","date_gmt":"2024-04-01T07:01:41","guid":{"rendered":"https:\/\/c-for-dummies.com\/blog\/?p=6313"},"modified":"2024-04-08T09:03:22","modified_gmt":"2024-04-08T16:03:22","slug":"calculating-the-absolute-value","status":"publish","type":"post","link":"https:\/\/c-for-dummies.com\/blog\/?p=6313","title":{"rendered":"Calculating the Absolute Value"},"content":{"rendered":"<h2>Difficulty: &#9733; &#9734; &#9734; &#9734;<\/h2>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/c-for-dummies.com\/blog\/wp-content\/uploads\/2020\/07\/The-BASIC-Handbook.png\" alt=\"The BASIC Handbook cover\" width=\"175\" height=\"233\" \/><br \/>\nI started my technology writing career at a computer book publishing house, CompuSoft. It&#8217;s no longer around, but I do recall ghost writing books such as the <em>BASIC Handbook<\/em>, which was an encyclopedia of the BASIC programming language. The first command listed in this book was <code>ABS<\/code>.<br \/>\n<!--more--><br \/>\nThe <code>ABS<\/code> command returns the absolute value of an integer, which is its positive value: <code>ABS(5)<\/code> is 5. <code>ABS(-200)<\/code> is 200.<\/p>\n<p>I&#8217;ve seen absolute values used when dealing with exponentiation as some calculations result in both positive and negative values. Whatever. Still, it&#8217;s easier for me to explain how to write a C program to calculate that absolute value than it is for me to understand math.<\/p>\n<p>Before you protest, I know that the C library hosts a slate of absolute value functions. These depend on the integer size:<\/p>\n<p><em>abs()<\/em> for <em>int<\/em> values<br \/>\n<em>labs()<\/em> for <em>long<\/em> values<br \/>\n<em>llabs()<\/em> for <em>long long<\/em> values<\/p>\n<p>Do not use any of these functions for your solution to this exercise! Instead, your challenge is to write a program that prompts the user for an integer (<em>int<\/em>) value and outputs its absolute value.<\/p>\n<p>Here&#8217;s are some sample runs of my solution:<\/p>\n<p><code>Enter an integer: 5<br \/>\nThe absolute value is 5<\/p>\n<p>Enter an integer: -200<br \/>\nThe absolute value is 200<\/code><\/p>\n<p>This task may not seem too difficult (hence only one star), but a quirk exists when returning the value of some negative integers. I share this quirk along with <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=6324\">my solution<\/a>, but please try this exercise on your own.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>The absolute value of a number is its positive value. <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=6313\">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-6313","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\/6313","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=6313"}],"version-history":[{"count":7,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/6313\/revisions"}],"predecessor-version":[{"id":6363,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/6313\/revisions\/6363"}],"wp:attachment":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=6313"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=6313"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=6313"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}