{"id":4693,"date":"2021-04-08T00:01:28","date_gmt":"2021-04-08T07:01:28","guid":{"rendered":"https:\/\/c-for-dummies.com\/blog\/?p=4693"},"modified":"2021-04-03T10:20:22","modified_gmt":"2021-04-03T17:20:22","slug":"balancing-accounts-solution","status":"publish","type":"post","link":"https:\/\/c-for-dummies.com\/blog\/?p=4693","title":{"rendered":"Balancing Accounts &#8211; Solution"},"content":{"rendered":"<p>I hope you had a wee bit of a struggle with your solution to <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=4678\">this month&#8217;s Exercise<\/a>. It involves a lot of decisions and proper steps in the right order to hold off on debits so that the bank account never dips below zero.<br \/>\n<!--more--><br \/>\nThe pathetically easy way to devise a solution is to tally all the deposits and withdrawals, then add them to get the final balance. Here you go:<\/p>\n<h3><a href=\"https:\/\/github.com\/dangookin\/C-For-Dummies-Blog\/blob\/master\/2021_04-Exercise-a.c\" rel=\"noopener\" target=\"_blank\">2021_04-Exercise-a.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,debit;\r\n\r\n    a = debit = 0.0;\r\n    for(int x=0; x&lt;20; x++)\r\n    {\r\n        if( deposit[x] &lt; 0.0 )\r\n            debit += deposit[x];\r\n        else\r\n            a += deposit[x];\r\n    }\r\n\r\n    <span class=\"comments\">\/* show results *\/<\/span>\r\n    printf(\"A total of $%.2f in deposits made\\n\",a);\r\n    printf(\"A total of %.2f in withdrawals made\\n\",debit);\r\n    printf(\"Final balance: $%.2f\\n\",a+debit);\r\n\r\n    return(0);\r\n}<\/pre>\n<p>The <em>for<\/em> loop at Line 12 tallies values in the <code>deposit[]<\/code> array, accumulating positive values in variable <code>a<\/code> and negative values in variable <code>debit<\/code>. At Line 21, a series of <em>printf()<\/em> functions output the results, boring and ho-hum:<\/p>\n<p><code>A total of $95.00 in deposits made<br \/>\nA total of -90.00 in withdrawals made<br \/>\nFinal balance: $5.00<\/code><\/p>\n<p>I would prefer that you instead approach the problem one array element at a time, which is what I did for my real solution:<\/p>\n<h3><a href=\"https:\/\/github.com\/dangookin\/C-For-Dummies-Blog\/blob\/master\/2021_04-Exercise-b.c\" rel=\"noopener\" target=\"_blank\">2021_04-Exercise-b.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,debit;\r\n\r\n    a = debit = 0.0;\r\n    for(int x=0; x&lt;20; x++)\r\n    {\r\n        printf(\"Bal: $%.2f\\t\",a);\r\n    \r\n        if( deposit[x] &gt; 0.0 )\r\n        {\r\n            printf(\"%+.2f \",deposit[x]);\r\n            a += deposit[x];\r\n            <span class=\"comments\">\/* check to see if debits can be taken *\/<\/span>\r\n            if( a+debit&gt;0.0 &amp;&amp; debit!=0.0 )\r\n            {\r\n                printf(\"add debit total %.2f \",debit);\r\n                a += debit;\r\n                debit = 0.0;\r\n            }\r\n            printf(\"= $%4.2f\",a);\r\n        }\r\n        else\r\n        {\r\n            if( a+deposit[x] &gt; 0 )\r\n            {\r\n                printf(\"%+.2f \",deposit[x]);\r\n                a += deposit[x];\r\n                printf(\"= $%4.2f\",a);\r\n            }\r\n            else\r\n            {\r\n                debit += deposit[x];\r\n                printf(\"%.2f added to debits\",deposit[x]);\r\n            }\r\n        }\r\n        putchar('\\n');\r\n    }\r\n\r\n    return(0);\r\n}<\/pre>\n<p>Within the <em>for<\/em> loop, an <em>if-else<\/em> decision separates the positive and negative elements in the <code>deposit[]<\/code> array.<\/p>\n<p>At Line 16, if the value of <code>deposit[x]<\/code> is positive, it&#8217;s added the cumulative balance in variable <code>a<\/code>. Then at Line 21, a test is made to determine if the current value of <code>debit<\/code> can be subtracted ( <code>a+=debit>0.0<\/code> ). If so, the total is reduced at Line 24, <code>a += debit<\/code>. At Line 25, the value of <code>debit<\/code> is zeroed, allowing for further negative values to accumulate.<\/p>\n<p>The <em>else<\/em> part of the decision at Line 29 handles withdrawals, or negative elements in the <code>deposit[]<\/code> array. At Line 31, if the withdrawal can be subtracted, it is. Otherwise, at Line 39, the value of the withdrawal is added to the cumulative <code>debit<\/code> total, <code>debit += deposit[x]<\/code>.<\/p>\n<p>Here is the output:<\/p>\n<pre class=\"screen\">\r\nBal: $0.00\t+10.00 = $10.00\r\nBal: $10.00\t-5.00 = $5.00\r\nBal: $5.00\t-10.00 added to debits\r\nBal: $5.00\t+5.00 = $10.00\r\nBal: $10.00\t+15.00 add debit total -10.00 = $15.00\r\nBal: $15.00\t-10.00 = $5.00\r\nBal: $5.00\t+5.00 = $10.00\r\nBal: $10.00\t-10.00 added to debits\r\nBal: $10.00\t+5.00 add debit total -10.00 = $5.00\r\nBal: $5.00\t-15.00 added to debits\r\nBal: $5.00\t-5.00 added to debits\r\nBal: $5.00\t+10.00 = $15.00\r\nBal: $15.00\t+15.00 add debit total -20.00 = $10.00\r\nBal: $10.00\t-15.00 added to debits\r\nBal: $10.00\t+5.00 = $15.00\r\nBal: $15.00\t+15.00 add debit total -15.00 = $15.00\r\nBal: $15.00\t-5.00 = $10.00\r\nBal: $10.00\t+10.00 = $20.00\r\nBal: $20.00\t-10.00 = $10.00\r\nBal: $10.00\t-5.00 = $5.00<\/pre>\n<p>I hope you found this Exercise challenging. I decided to amp up the difficulty a tad, though if you opted for the easier solution (presented first), I don&#8217;t blame you. It takes some deft action to determine how negative values are held off until the balance can accept them. I hope your solution was effective and clever.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I hope you had a wee bit of a struggle with your solution to this month&#8217;s Exercise. It involves a lot of decisions and proper steps in the right order to hold off on debits so that the bank account &hellip; <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=4693\">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-4693","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\/4693","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=4693"}],"version-history":[{"count":2,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/4693\/revisions"}],"predecessor-version":[{"id":4708,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/4693\/revisions\/4708"}],"wp:attachment":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=4693"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=4693"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=4693"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}