{"id":6052,"date":"2023-10-08T00:01:06","date_gmt":"2023-10-08T07:01:06","guid":{"rendered":"https:\/\/c-for-dummies.com\/blog\/?p=6052"},"modified":"2023-10-07T11:29:14","modified_gmt":"2023-10-07T18:29:14","slug":"coin-flipping-madness-solution","status":"publish","type":"post","link":"https:\/\/c-for-dummies.com\/blog\/?p=6052","title":{"rendered":"Coin Flipping Madness &#8211; Solution"},"content":{"rendered":"<p>I hope you didn&#8217;t &#8220;flip out&#8221; over <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=6043\">this month&#8217;s Exercise<\/a>. Flipping a coin can be done by hand or by using a computer. Using a computer is easier because it can be programmed to record each flip and it saves you hand muscle molecules for flipping and the chore of writing down the results.<br \/>\n<!--more--><br \/>\nFlipping requires a simple expression in C: <code>rand()%2<\/code><\/p>\n<p>But first, use the <em>srand()<\/em> function to seed the randomizer: <code>srand( (unsigned)time(NULL) )<\/code> This step ensures that the numbers aren&#8217;t identical each time you run the program.<\/p>\n<p>One hundred sets of 100 flips requires nested loops. Further, the flip totals for each run must be recorded. To meet this end, I use two arrays in my solution, <code>heads[]<\/code> and <code>tails[]<\/code>. Flips are tallied within the nested loop by using variables <code>t<\/code> and <code>h<\/code> for heads and tails. Here is the nested loop:<\/p>\n<pre class=\"screen\">\r\n    <span class=\"comments\">\/* 100 sets of 100 flips *\/<\/span>\r\n    for( x=0; x&lt;iterations; x++ )\r\n    {\r\n        <span class=\"comments\">\/* process 100 flips *\/<\/span>\r\n        for( y=t=h=0; y&lt;iterations; y++ )\r\n        {\r\n            if( rand()%2 )\r\n                t++;    <span class=\"comments\">\/* odd is \"tails\" *\/<\/span>\r\n            else\r\n                h++;    <span class=\"comments\">\/* even is \"heads\" *\/<\/span>\r\n        }\r\n        <span class=\"comments\">\/* output this round's results *\/<\/span>\r\n        printf(\"Set %3d: %2d Heads, %2d Tails\\n\",x+1,h,t);\r\n        <span class=\"comments\">\/* store the data *\/<\/span>\r\n        heads[x] = h;\r\n        tails[x] = t;\r\n    }<\/pre>\n<p>I set constant <em>int<\/em> value <code>iterations<\/code> equal to 100. This constant allows me to use the value 100 and potentially change it later without having to hunt and sift through the code.<\/p>\n<p>The outer <em>for<\/em> loop spins 100 times. It outputs the results for each set and stores the results in arrays <code>heads[]<\/code> and <code>tails[]<\/code>.<\/p>\n<p>The inner <em>for<\/em> loop flips the coins. Variable <code>y<\/code> is the looping variable, but variables <code>h<\/code> and <code>t<\/code> are also initialized in the <em>for<\/em> loop statement. An <em>if-else<\/em> structure tallies the heads (zero) and tails (one) flips.<\/p>\n<p>The second part of the challenge is to interpret and output the results. Originally I wrote separate <em>for<\/em> loops for each item requested:<\/p>\n<ul>\n<li>Average of all heads\/tails<\/li>\n<li>Number of times it was 50\/50 heads tails<\/li>\n<li>The greatest number of heads in a set<\/li>\n<li>The greatest number of tails in a set<\/li>\n<\/ul>\n<p>Then I realized I could do all the math in one <em>for<\/em> loop, shown here:<\/p>\n<pre class=\"screen\">\r\n    <span class=\"comments\">\/* gather data *\/<\/span>\r\n    h_total = t_total = even = h_max = t_max = 0;\r\n    for( x=0; x&lt;iterations; x++ )\r\n    {\r\n        <span class=\"comments\">\/* obtain totals for averages *\/<\/span>\r\n        h_total += heads[x];\r\n        t_total += tails[x];\r\n        <span class=\"comments\">\/* count even splits *\/<\/span>\r\n        if( heads[x]==50 )\r\n            even++;\r\n        <span class=\"comments\">\/* store maximums *\/<\/span>\r\n        if( heads[x] &gt; h_max )\r\n            h_max = heads[x];\r\n        if( tails[x] &gt; t_max )\r\n            t_max = tails[x];\r\n    }<\/pre>\n<p>Variables <code>h_total<\/code> and <code>t_total<\/code> accumulate the total heads and tails flips for all 10,000 tries.<\/p>\n<p>Variable <code>even<\/code> counts the number of times the split is 50:50.<\/p>\n<p>Variables <code>h_max<\/code> and <code>t_max<\/code> set the highest number of heads and tails for each set.<\/p>\n<p>All these variables are first initialized to zero. Then a <em>for<\/em> loop gathers the data. The only additional statements calculate the averages:<\/p>\n<p><code>h_avg = (float)h_total\/iterations;<br \/>\nt_avg = (float)t_total\/iterations;<\/code><\/p>\n<p>The <em>float<\/em> cast ensures a real number result, and matches the data type of <code>h_avg<\/code> and <code>t_avg<\/code>.<\/p>\n<p>Finally, a series of <em>printf()<\/em> statements output the results.<\/p>\n<p>You can <a href=\"https:\/\/github.com\/dangookin\/C-For-Dummies-Blog\/blob\/master\/2023_10-Exercise.c\" rel=\"noopener\" target=\"_blank\">click here<\/a> to view the full code on GitHub.<\/p>\n<p>I hope your solution met with success. I had fun coding this puzzle, which are originally just going to be a coin flipping simulation. Then I went nuts. Hopefully you went nuts as well.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I hope you didn&#8217;t &#8220;flip out&#8221; over this month&#8217;s Exercise. Flipping a coin can be done by hand or by using a computer. Using a computer is easier because it can be programmed to record each flip and it saves &hellip; <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=6052\">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-6052","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\/6052","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=6052"}],"version-history":[{"count":2,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/6052\/revisions"}],"predecessor-version":[{"id":6064,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/6052\/revisions\/6064"}],"wp:attachment":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=6052"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=6052"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=6052"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}