{"id":871,"date":"2014-08-02T00:01:33","date_gmt":"2014-08-02T07:01:33","guid":{"rendered":"http:\/\/c-for-dummies.com\/blog\/?p=871"},"modified":"2014-07-26T08:27:22","modified_gmt":"2014-07-26T15:27:22","slug":"number-crunching","status":"publish","type":"post","link":"https:\/\/c-for-dummies.com\/blog\/?p=871","title":{"rendered":"Number Crunching"},"content":{"rendered":"<p>Forget the graphics. Forget the fancy stuff, which amazingly includes text. At the root of all programming, and the foundations upon which modern computer science is erected, lies the ability to quickly and accurately calculate vast seas of numbers. The traditional title for this activity is <em>Number Crunching<\/em>.<br \/>\n<!--more--><br \/>\nThe first computers developed back in the 1940s were used to calculate missile trajectories. Even Babage&#8217;s engine (1820s) was designed to solve complex mathematical problems. This whole obsession with games and Facebook is child&#8217;s play next to what raw computing power can do.<\/p>\n<p>Without getting overly complex &#8212; and by that, I mean writing code that could calculate &pi; down to the millionth digit &#8212; I&#8217;ll present a rather simple example of number crunching.<\/p>\n<p>To demonstrate number crunching, I&#8217;ve written code that simulates tossing two dice and reporting the total. That would be a random value between 2 and 12, inclusive:<\/p>\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\n#define ROLLS 10\r\n\r\nint main()\r\n{\r\n    int t,r1,r2;\r\n\r\n    <span class=\"comments\">\/* Seed randomizer *\/<\/span>\r\n    srand((unsigned int)(time(NULL)));\r\n\r\n    <span class=\"comments\">\/* Roll dice ROLLS times *\/<\/span>\r\n    for(t=0;t&lt;ROLLS;t++)\r\n    {\r\n        r1 = (rand() % 6) + 1;\r\n        r2 = (rand() % 6) + 1;\r\n        printf(\"Roll %d = %d\\n\",t+1,r1+r2);\r\n    }\r\n\r\n    return(0);\r\n}<\/pre>\n<p>Here&#8217;s sample output:<\/p>\n<pre><code>Roll 1 = 8\r\nRoll 2 = 6\r\nRoll 3 = 9\r\nRoll 4 = 5\r\nRoll 5 = 10\r\nRoll 6 = 7\r\nRoll 7 = 7\r\nRoll 8 = 6\r\nRoll 9 = 7\r\nRoll 10 = 3<\/code><\/pre>\n<p>To tally the values, I&#8217;ll add an array. The array keeps track of the numbers 2 through 12 as they are rolled. The following code shows this modification from the original example (above):<\/p>\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\n#define ROLLS 100\r\n\r\nint main()\r\n{\r\n    int r,t,r1,r2;\r\n    int results[13];\r\n\r\n    <span class=\"comments\">\/* Seed randomizer *\/<\/span>\r\n    srand((unsigned int)time(NULL));\r\n\r\n    <span class=\"comments\">\/* Initialize the array *\/<\/span>\r\n    for(r=0;r&lt;13;r++)\r\n        results[r] = 0;\r\n\r\n    <span class=\"comments\">\/* Roll dice ROLLS times *\/<\/span>\r\n    for(t=0;t&lt;ROLLS;t++)\r\n    {\r\n        r1 = (rand() % 6) + 1;\r\n        r2 = (rand() % 6) + 1;\r\n        results[r1+r2]++;   \/* count total *\/\r\n    }\r\n\r\n    <span class=\"comments\">\/* Display results *\/<\/span>\r\n    for(r=2;r&lt;=12;r++)\r\n        printf(\"%2d: %d\\n\",r,results[r]);\r\n\r\n    return(0);\r\n}<\/pre>\n<p>First, I&#8217;ve increased the number of <code>ROLLS<\/code> to 100 in Line 5.<\/p>\n<p>An integer array, <code>results<\/code>, with 13 elements is created at Line 10. Why 13? Because I&#8217;m going to use values 2 through 12 from the dice rolls to set the array&#8217;s elements. For example, a roll of 12 would be tallied at array element 13. Array elements 0 and 1 won&#8217;t be used as you can&#8217;t roll a zero or 1 with two dice.<\/p>\n<p>Lines 16 and 17 initialize the <code>results<\/code> array, setting all the elements to zero.<\/p>\n<p>At Line 24, the array element matching the number rolled is incremented. That effectively keeps track of the values rolled.<\/p>\n<p>Line 29 displays the results, which are shown below for 100 rolls:<\/p>\n<pre><code> 2: 5\r\n 3: 4\r\n 4: 8\r\n 5: 8\r\n 6: 10\r\n 7: 18\r\n 8: 17\r\n 9: 15\r\n10: 10\r\n11: 2\r\n12: 3<\/code><\/pre>\n<p>That looks like a good distribution; the most popular rolls are around 7, which has the highest odds. The values 2 and 2 have the least amount of rolls.<\/p>\n<p>To really crunch the numbers, I&#8217;ve adjusted the value of <code>ROLLS<\/code> to 100,000. Here&#8217;s that output:<\/p>\n<pre><code> 2: 2852\r\n 3: 5538\r\n 4: 8242\r\n 5: 11212\r\n 6: 13847\r\n 7: 16614\r\n 8: 13831\r\n 9: 11094\r\n10: 8451\r\n11: 5562\r\n12: 2757<\/code><\/pre>\n<p>Being a hard core nerd who can&#8217;t leave well enough alone, I made further modifications so that the output finally looks like this:<\/p>\n<pre><code>Results of 100000 rolls:\r\nRoll\tCount\tPercent\r\n 2\t 2715\t2.7%\r\n 3\t 5544\t5.5%\r\n 4\t 8517\t8.5%\r\n 5\t11135\t11.1%\r\n 6\t14039\t14.0%\r\n 7\t16547\t16.5%\r\n 8\t13835\t13.8%\r\n 9\t11145\t11.1%\r\n10\t 8162\t8.2%\r\n11\t 5624\t5.6%\r\n12\t 2737\t2.7%<\/code><\/pre>\n<p><a href=\"http:\/\/c-for-dummies.com\/blog\/wp-content\/uploads\/2014\/08\/0802c.c\">Click here<\/a> to view the code, although I&#8217;d encourage you to try and code this type of output on your own first. (Use this Lesson&#8217;s second example as a starting point.)<\/p>\n<p>Now this is only a mild example of what I&#8217;d call &#8220;number crunching.&#8221; Obviously you&#8217;ll find more complex math problems that provide more of a challenge to the computer. I&#8217;ll cover some of those in a later Lesson.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>One thing computers are excellent at doing is working with numbers. <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=871\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2],"tags":[],"class_list":["post-871","post","type-post","status-publish","format-standard","hentry","category-main"],"_links":{"self":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/871","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=871"}],"version-history":[{"count":5,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/871\/revisions"}],"predecessor-version":[{"id":897,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/871\/revisions\/897"}],"wp:attachment":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=871"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=871"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=871"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}