{"id":4678,"date":"2021-04-01T00:01:29","date_gmt":"2021-04-01T07:01:29","guid":{"rendered":"https:\/\/c-for-dummies.com\/blog\/?p=4678"},"modified":"2021-04-08T17:00:53","modified_gmt":"2021-04-09T00:00:53","slug":"balancing-accounts","status":"publish","type":"post","link":"https:\/\/c-for-dummies.com\/blog\/?p=4678","title":{"rendered":"Balancing Accounts"},"content":{"rendered":"<h2>Difficulty: Medium<\/h2>\n<p>Your bank receives two deposits or credits and three bills or debits. The credits cover the debits, meaning that your balance should never go below zero, but the bank has clever software: The bills are calculated first, which thrusts your account into negative territory complete with fees &mdash; even though the deposits cover the bills. Is this obnoxious behavior the result of greedy bankers or clever programming?<br \/>\n<!--more--><br \/>\nIt&#8217;s both!<\/p>\n<p>Bankers have long been accused of rushing through debits before credits to rake in the fines for low or zero balances. I view this approach as evil, though as a programmer I know ways exist to avoid the problem. Do you?<\/p>\n<p>The following code simulates deposits and withdrawals to a bank account. Each amount in the <code>deposit[]<\/code> array is added or subtracted from the current balance.<\/p>\n<h3><a href=\"https:\/\/github.com\/dangookin\/C-For-Dummies-Blog\/blob\/master\/2021_04_01-Lesson.c\" rel=\"noopener\" target=\"_blank\">2021_04_01-Lesson.c<\/a><\/h3>\n<pre class=\"screen\">\r\n#include &lt;stdio.h&gt;\r\n\r\nint main()\r\n{\r\n    float deposit[20] = {\r\n        10.0, -5.0, -10.0, 5.0, 15.0, -10.0, 5.0, -10.0, 5.0, -15.0,\r\n        -5.0, 10.0, 15.0, -15.0, 5.0, 15.0, -5.0, 10.0, -10.0, -5.0\r\n    };\r\n    float a;\r\n\r\n    a = 0.0;\r\n    for(int x=0; x&lt;20; x++)\r\n    {\r\n        printf(\"Bal: $%4.2f\\t\",a);\r\n        if( deposit[x] &gt; 0 )\r\n            printf(\"credit %.2f\",deposit[x]);\r\n        else\r\n            printf(\"debit %.2f\",deposit[x]);\r\n        <span class=\"comments\">\/* adjust balance *\/<\/span>\r\n        a += deposit[x];\r\n        <span class=\"comments\">\/* output new balance *\/<\/span>\r\n        printf(\"\\t= $%4.2f\",a);\r\n        <span class=\"comments\">\/* flag overdrafts *\/<\/span>\r\n        if( a &lt; 0 )\r\n            printf(\"\\t** Overdraft\\n\");\r\n        else\r\n            putchar('\\n');\r\n    }\r\n\r\n    return(0);\r\n}<\/pre>\n<p>The program outputs a running total, where you can see how the balance occasionally dips below zero:<\/p>\n<pre class=\"screen\">\r\nBal: $0.00\tcredit 10.00\t= $10.00\r\nBal: $10.00\tdebit -5.00\t= $5.00\r\nBal: $5.00\tdebit -10.00\t= $-5.00\t** Overdraft\r\nBal: $-5.00\tcredit 5.00\t= $0.00\r\nBal: $0.00\tcredit 15.00\t= $15.00\r\nBal: $15.00\tdebit -10.00\t= $5.00\r\nBal: $5.00\tcredit 5.00\t= $10.00\r\nBal: $10.00\tdebit -10.00\t= $0.00\r\nBal: $0.00\tcredit 5.00\t= $5.00\r\nBal: $5.00\tdebit -15.00\t= $-10.00\t** Overdraft\r\nBal: $-10.00\tdebit -5.00\t= $-15.00\t** Overdraft\r\nBal: $-15.00\tcredit 10.00\t= $-5.00\t** Overdraft\r\nBal: $-5.00\tcredit 15.00\t= $10.00\r\nBal: $10.00\tdebit -15.00\t= $-5.00\t** Overdraft\r\nBal: $-5.00\tcredit 5.00\t= $0.00\r\nBal: $0.00\tcredit 15.00\t= $15.00\r\nBal: $15.00\tdebit -5.00\t= $10.00\r\nBal: $10.00\tcredit 10.00\t= $20.00\r\nBal: $20.00\tdebit -10.00\t= $10.00\r\nBal: $10.00\tdebit -5.00\t= $5.00<\/pre>\n<p>Your challenge for this month&#8217;s Exercise is to modify the code so that the balance never goes below zero. All values in the <code>deposit[]<\/code> array are still be processed, but you must devise a way to ensure that the debits aren&#8217;t subtracted until sufficient funds are available in the account to cover them.<\/p>\n<p>Multiple solutions are possible for this puzzle. Frankly, anything goes, though all the values must be processed.<\/p>\n<p>Please try this Exercise on your own before you check my <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=4693\">my solution<\/a>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Organize values so that a bank account never dips below zero. (Difficulty: Medium) <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=4678\">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-4678","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\/4678","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=4678"}],"version-history":[{"count":5,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/4678\/revisions"}],"predecessor-version":[{"id":4717,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/4678\/revisions\/4717"}],"wp:attachment":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=4678"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=4678"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=4678"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}