{"id":1141,"date":"2015-01-08T00:01:52","date_gmt":"2015-01-08T08:01:52","guid":{"rendered":"http:\/\/c-for-dummies.com\/blog\/?p=1141"},"modified":"2015-01-03T11:49:12","modified_gmt":"2015-01-03T19:49:12","slug":"just-average-solution","status":"publish","type":"post","link":"https:\/\/c-for-dummies.com\/blog\/?p=1141","title":{"rendered":"Just Average &#8211; Solution"},"content":{"rendered":"<p>If you attempted to compile the code skeleton for this month&#8217;s <a href=\"http:\/\/c-for-dummies.com\/blog\/?p=1138\">Exercise<\/a>, you most likely stumbled across the first problem to solve: The <em>average()<\/em> function requires a <em>type<\/em>.<br \/>\n<!--more--><br \/>\nBefore I get into my explanation, <a href=\"http:\/\/c-for-dummies.com\/blog\/wp-content\/uploads\/2014\/01\/01exercise.c\">click here<\/a> to view my solution to the &#8220;Just Average&#8221; Exercise.<\/p>\n<p>In the Exercise post, I purposefully omitted the function&#8217;s type to drive home a point about calculating the average of a series of numbers: Numeric values in the C language can be either integers or real (floating point). The <em>average()<\/em> function must deal with floating point values, so I cast it as a <em>float<\/em> in my solution, shown at Line 5:<\/p>\n<pre class=\"screen\">\r\n#include &lt;stdio.h&gt;\r\n\r\n#define COUNT 10\r\n\r\nfloat average(float *a, int size);<\/pre>\n<p>Above, you also see the second required addition, the <em>average()<\/em> function&#8217;s two arguments: The array to be averaged and the size of that array. Arrays are pointers in disguise, which is why pointer notation is used above. And even though the array is a <em>float<\/em>, its size (the number of elements) is an <em>int<\/em>.<\/p>\n<p>Because a program can&#8217;t determine an array&#8217;s size on-the-fly, any <em>average()<\/em> function must somehow know the array&#8217;s size or, specifically, its ending point. In my solution, that value is passed directly to the function.<\/p>\n<p>The rest of the <em>main()<\/em> function was provided in the Exercise. My only addition is the <em>printf()<\/em> function you see below at Line 17, which displays the result and calls the <em>average()<\/em> function:<\/p>\n<pre class=\"screen\">\r\nint main()\r\n{\r\n    float scores[COUNT] = {\r\n        97.2, 86.0, 75.5, 93.2, 87.1,\r\n        68.7, 81.9, 92.4, 84.0, 66.3 };\r\n    int x;\r\n\r\n    for(x=0;x&lt;COUNT;x++)\r\n        printf(\"Student %2d score %3.1f\\n\",x+1,scores[x]);\r\n    <span class=\"comments\">\/* display average score *\/<\/span>\r\n    printf(\"Average score = %.1f\\n\",average(scores,COUNT));\r\n\r\n    return(0);\r\n}<\/pre>\n<p>The average of a series of values is calculated by taking the sum of those values and then dividing it by the number of values.<\/p>\n<pre class=\"screen\">\r\nfloat average(float *a, int size)\r\n{\r\n    int x;\r\n    float total = 0.0;\r\n\r\n    for(x=0;x&lt;size;x++)\r\n        total += a[x];\r\n    return(total\/size);\r\n}<\/pre>\n<p>The <code>total<\/code> variable acts as an accumulator. It&#8217;s initialized to zero (actually 0.0, which is a floating point value), then it keeps adding to itself (in Line 28) each element&#8217;s value from the array. That total is divided by the <code>size<\/code> value, with the result &#8212; the average &#8212; returned from the function.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>If you attempted to compile the code skeleton for this month&#8217;s Exercise, you most likely stumbled across the first problem to solve: The average() function requires a type.<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[5],"tags":[],"class_list":["post-1141","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\/1141","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=1141"}],"version-history":[{"count":3,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/1141\/revisions"}],"predecessor-version":[{"id":1169,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/1141\/revisions\/1169"}],"wp:attachment":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1141"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1141"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1141"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}