{"id":2590,"date":"2017-07-08T00:01:01","date_gmt":"2017-07-08T07:01:01","guid":{"rendered":"http:\/\/c-for-dummies.com\/blog\/?p=2590"},"modified":"2017-07-01T09:50:31","modified_gmt":"2017-07-01T16:50:31","slug":"change-due-solution","status":"publish","type":"post","link":"https:\/\/c-for-dummies.com\/blog\/?p=2590","title":{"rendered":"Change Due &#8211; Solution"},"content":{"rendered":"<p>I confess, the solution to <a href=\"http:\/\/c-for-dummies.com\/blog\/?p=2577\">this month&#8217;s Exercise<\/a> was frustrating. Several times, I thought to abandon the premise and focus on the main problem, which is how to create a random price, such as $5.32, without that floating-point value really being 5.3187321.<br \/>\n<!--more--><br \/>\nNormally, you can code prices using float variables straight-up. Financial institutions calculate fractions of cents when they work out interest and such. So a real number works fine, and the <code>%.2f<\/code> placeholder in a <em>printf()<\/em> statement shows the proper result. For a rounding exercise, however, the values must be limited to only two digits of precision &mdash; cents.<\/p>\n<p>My first thought was to use the modulus operator (<code>%<\/code>) to hack off a real number, but the modulus operator works only with <em>int<\/em> values. Instead, I crafted floating point values using two random number calls, one for the dollars and the other for the cents:<\/p>\n<pre class=\"screen\">\r\n    <span class=\"comments\">\/* calculate item's random cost *\/<\/span>\r\n        <span class=\"comments\">\/* pennies *\/<\/span>\r\n    item_cents = rand()%100;\r\n        <span class=\"comments\">\/* dollar *\/<\/span>\r\n    item_dollars = rand()%MAX_COST;\r\n        <span class=\"comments\">\/* build original price *\/<\/span>\r\n    original_price = (float)item_dollars + (float)item_cents\/100.0;<\/pre>\n<p>Integer variable <code>item_cents<\/code> contains a random number between 0 and 99. Integer variable <code>item_dollars<\/code> contains a value from 0 to <code>MAX_COST<\/code>, which in my code is defined as 10. The <code>original_price<\/code> variable is a <em>float<\/em>. Typecasting recajiggers <code>item_dollars<\/code> and <code>item_cents<\/code> to a <em>float<\/em> value, <code>item_cents<\/code>. That value is divided by 100, which results in a real number with only two decimal places guaranteed.<\/p>\n<p>After the item&#8217;s original price is obtained, the code determines the rounded price using the original <em>int<\/em> value for cents:<\/p>\n<pre class=\"screen\">\r\n    <span class=\"comments\">\/* round off to the nearest nickel *\/<\/span>\r\n    diff = item_cents % 5;\r\n    round_price = original_price - (float)diff\/100.0;<\/pre>\n<p>Above, I use the original <em>int<\/em> variable <code>item_cents<\/code> with the modulus operator to round down to the next nickel. Then the value <code>round_price<\/code> is calculated by subtracting the rounded-down value (the pennies) from the original price.<\/p>\n<p>The rest of the code displays the two values and calculates a running total for each. You can view the entire code <a href=\"http:\/\/c-for-dummies.com\/blog\/wp-content\/uploads\/2017\/06\/07exercise-solution.c\">here<\/a>. And, yes, as you expect, you save more when each price is rounded down, but only a small amount.<\/p>\n<p>Another way to calculate the solution is to use the <em>fmod()<\/em> function. <em>fmod()<\/em> is the floating-point remainder function, which requires two <em>double<\/em> arguments:<\/p>\n<p><code>fmod(a,b)<\/code><\/p>\n<p>The <em>fmod()<\/em> function, defined in <code>math.h<\/code>, returns the remainder of <code>a\/b<\/code>. So if <code>a<\/code> is some random price and <code>b<\/code> is 0.05, the result is similar to <code>a % 0.05<\/code>. You could truncate any floating point value to the second decimal position by using this equation:<\/p>\n<p><code>round_price = fmod(original_price,0.01);<\/code><\/p>\n<p>If you used <em>fmod()<\/em> in your solution instead of the cobbled-together-integers solution I attempted, then I&#8217;ll give you extra <em>For Dummies<\/em> bonus points.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I confess, the solution to this month&#8217;s Exercise was frustrating. Several times, I thought to abandon the premise and focus on the main problem, which is how to create a random price, such as $5.32, without that floating-point value really &hellip; <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=2590\">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":[5],"tags":[],"class_list":["post-2590","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\/2590","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=2590"}],"version-history":[{"count":2,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/2590\/revisions"}],"predecessor-version":[{"id":2603,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/2590\/revisions\/2603"}],"wp:attachment":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=2590"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=2590"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=2590"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}