{"id":4279,"date":"2020-08-08T00:01:05","date_gmt":"2020-08-08T07:01:05","guid":{"rendered":"https:\/\/c-for-dummies.com\/blog\/?p=4279"},"modified":"2020-08-15T08:19:50","modified_gmt":"2020-08-15T15:19:50","slug":"multiples-of-four-solution","status":"publish","type":"post","link":"https:\/\/c-for-dummies.com\/blog\/?p=4279","title":{"rendered":"Multiples of Four &#8211; Solution"},"content":{"rendered":"<p>The solution <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=4267\">this month&#8217;s Exercise<\/a> isn&#8217;t that complex, but it was handy in disproving a theory.<br \/>\n<!--more--><br \/>\nAs a recap, the code simulates two opponents in a number game, a player and the computer.<\/p>\n<p>The player goes first, guessing a value, 1 to 3, which is added to a running total. The total starts at zero.<\/p>\n<p>The computer&#8217;s job is to also guess values 1 to 3, but with the running total result always a multiple of 4.<\/p>\n<p>The guessing continues until the total surpasses 30.<\/p>\n<p>Here is my solution:<\/p>\n<h3>2020_08-Exercise.c<\/h3>\n<pre class=\"screen\">\r\n#include &lt;stdio.h&gt;\r\n#include &lt;stdlib.h&gt;\r\n#include &lt;time.h&gt;\r\n\r\nint main()\r\n{\r\n    int total,four,player;\r\n\r\n    <span class=\"comments\">\/* initialize randomizer *\/<\/span>\r\n    srand((unsigned)time(NULL));\r\n\r\n    total = 0;\r\n    while( total&lt;=30 )\r\n    {\r\n        <span class=\"comments\">\/* get a value from 1 to 3 *\/<\/span>\r\n        player=rand() % 3 + 1;\r\n        total+=player;\r\n        <span class=\"comments\">\/* output player's guess and total *\/<\/span>\r\n        printf(\"Player adds %d, total is %d\\n\",\r\n                player,\r\n                total\r\n              );\r\n        <span class=\"comments\">\/* make the computer add a value to\r\n           equal a multiple of 4 *\/<\/span>\r\n        four = 4 - total % 4;\r\n        total += four;\r\n        printf(\"Computer adds %d to make %d\\n\",\r\n                four,\r\n                total\r\n              );\r\n    }\r\n\r\n    return(0);\r\n}<\/pre>\n<p>Variable <code>total<\/code> represents the game&#8217;s running total. It&#8217;s initialized to zero at Line 12. The <em>while<\/em> loop spins as long as the <code>total<\/code> value is less than or equal to 30.<\/p>\n<p>Variable <code>player<\/code> holds the player&#8217;s guess, which is obtained at Line 16: <code>player=rand() % 3 + 1;<\/code> This value is added to the running total at Line 17: <code>total+=player;<\/code><\/p>\n<p>After the player&#8217;s guess, the computer&#8217;s job is easy. Its guess is stored in variable <code>four<\/code>, which is calculated at Line 25: <code>four = 4 - total % 4;<\/code> The value in <code>total<\/code> is divided by 4 to obtain the remainder, which will be values 1, 2, or 3. It can&#8217;t ever be zero because the player went first; only the computer can reach a multiple of 4. The result of the modulo operation is subtracted from 4, which means the computer always guesses a multiple of 4.<\/p>\n<p>Here&#8217;s a sample run:<\/p>\n<p><code>Player adds 3, total is 3<br \/>\nComputer adds 1 to make 4<br \/>\nPlayer adds 2, total is 6<br \/>\nComputer adds 2 to make 8<br \/>\nPlayer adds 3, total is 11<br \/>\nComputer adds 1 to make 12<br \/>\nPlayer adds 2, total is 14<br \/>\nComputer adds 2 to make 16<br \/>\nPlayer adds 3, total is 19<br \/>\nComputer adds 1 to make 20<br \/>\nPlayer adds 2, total is 22<br \/>\nComputer adds 2 to make 24<br \/>\nPlayer adds 3, total is 27<br \/>\nComputer adds 1 to make 28<br \/>\nPlayer adds 3, total is 31<br \/>\nComputer adds 1 to make 32<\/code><\/p>\n<p>If your solution yields similar results, great! Remember that your code need not match mine to successfully complete the Exercise.<\/p>\n<p>I wrote this simulation to test the theory that going second and always guessing am multiple of 4 helps you to win the 21 number game. It failed. Regardless, in <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=4297\">future Lesson<\/a> I present my version of the 21 number game.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>The solution this month&#8217;s Exercise isn&#8217;t that complex, but it was handy in disproving a theory.<\/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-4279","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\/4279","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=4279"}],"version-history":[{"count":5,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/4279\/revisions"}],"predecessor-version":[{"id":4315,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/4279\/revisions\/4315"}],"wp:attachment":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=4279"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=4279"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=4279"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}