{"id":6391,"date":"2024-05-08T00:01:40","date_gmt":"2024-05-08T07:01:40","guid":{"rendered":"https:\/\/c-for-dummies.com\/blog\/?p=6391"},"modified":"2024-05-08T08:39:20","modified_gmt":"2024-05-08T15:39:20","slug":"calculating-the-standard-deviation-solution","status":"publish","type":"post","link":"https:\/\/c-for-dummies.com\/blog\/?p=6391","title":{"rendered":"Calculating the Standard Deviation &#8211; Solution"},"content":{"rendered":"<p>The challenge for <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=6368\">this month&#8217;s Exercise<\/a> is to calculate the standard deviation for a given set of data points. It&#8217;s a &#8220;whole population calculation&#8221; because all the data points are present. The trick is to follow the equation, transforming it from cryptic math mumbo-jumbo into C code.<br \/>\n<!--more--><br \/>\nFigure 1 shows the equation I used, which follows these steps:<\/p>\n<div id=\"attachment_6378\" style=\"width: 400px\" class=\"wp-caption aligncenter\"><img loading=\"lazy\" decoding=\"async\" aria-describedby=\"caption-attachment-6378\" src=\"https:\/\/c-for-dummies.com\/blog\/wp-content\/uploads\/2024\/05\/0501-Figure2.png\" alt=\"Standard deviation calculation\" width=\"390\" height=\"222\" class=\"size-full wp-image-6378\" srcset=\"https:\/\/c-for-dummies.com\/blog\/wp-content\/uploads\/2024\/05\/0501-Figure2.png 390w, https:\/\/c-for-dummies.com\/blog\/wp-content\/uploads\/2024\/05\/0501-Figure2-300x171.png 300w\" sizes=\"auto, (max-width: 390px) 100vw, 390px\" \/><p id=\"caption-attachment-6378\" class=\"wp-caption-text\">Figure 1. The mathematical monster used to calculate standard deviation (full set).<\/p><\/div>\n<ol>\n<li>Calculate the mean for the values, what I would normally call the &#8220;average.&#8221; Tally up all the data points and divide by the number of data points.<\/li>\n<li>Subtract the mean from each element in the data set and square the result. These values are totaled to obtain their mean. The result is the variance.<\/li>\n<li>Obtain the square root of the variance to get the standard deviation.<\/li>\n<\/ol>\n<p>The sample code skeleton provided in the Exercise post listed the <code>values[]<\/code> array as the data set. Variable <code>items<\/code> is set to the number of items in the data set: <code>items = sizeof(values)\/sizeof(int);<\/code> These two variables are passed to the <em>stddev()<\/em> function where they&#8217;re manipulated to calculate and return the standard deviation.<\/p>\n<p>Here&#8217;s my solution, which follows the steps outlined above:<\/p>\n<h3><a href=\"https:\/\/github.com\/dangookin\/C-For-Dummies-Blog\/blob\/master\/2024_05-Exercise.c\" rel=\"noopener\" target=\"_blank\">2024_05-Exercise.c<\/a><\/h3>\n<pre class=\"screen\">\r\n#include &lt;stdio.h&gt;\r\n#include &lt;math.h&gt;\r\n\r\n<span class=\"comments\">\/* whole population calculation *\/<\/span>\r\ndouble stddev(int v[],int items)\r\n{\r\n    int x,total;\r\n    double mean,variance;\r\n\r\n    <span class=\"comments\">\/* calculate sum *\/<\/span>\r\n    total = 0;\r\n    for( x=0; x&lt;items; x++ )\r\n        total += v[x];\r\n\r\n    <span class=\"comments\">\/* calculate the mean (average) *\/<\/span>\r\n    mean = (double)total\/items;\r\n\r\n    <span class=\"comments\">\/* calculate deviations *\/<\/span>\r\n    total = 0;\r\n    for( x=0; x&lt;items; x++ )\r\n        total += (v[x]-mean)*(v[x]-mean);\r\n    variance = (double)total\/items;\r\n\r\n    return( sqrt(variance) );\r\n}\r\n\r\nint main()\r\n{\r\n    int values[] = { 10, 12, 23, 23, 16, 23, 21, 16 };\r\n    int x,items;\r\n\r\n    <span class=\"comments\">\/* output the array's values *\/<\/span>\r\n    items = sizeof(values)\/sizeof(int);\r\n    printf(\"Values:\");\r\n    for( x=0; x&lt;items; x++ )\r\n    {\r\n        printf(\" %2d\",values[x]);\r\n    }\r\n    putchar('\\n');\r\n\r\n    printf(\"The standard deviation is %.4f\\n\",\r\n            stddev(values,items)\r\n          );\r\n\r\n    return 0;\r\n}<\/pre>\n<p>The first <em>for<\/em> loop in the <em>stddev()<\/em> function obtains the total of all values in the data set, <code>v[]<\/code>. The mean is calculated: <code>mean = (double)total\/items;<\/code> The <em>double<\/em> cast is required as <code>mean<\/code> is a <em>double<\/em> variable.<\/p>\n<p>The next <em>for<\/em> loop calculates the deviation: Variable <code>total<\/code> tallies the squares of the difference between each value <code>v[x]<\/code> and the mean: <code>total += (v[x]-mean)*(v[x]-mean);<\/code> The multiplication expression squares the values, which is my preferred method over using the <em>pow()<\/em> function.<\/p>\n<p>Variable <code>variance<\/code> is assigned the value of the total of the squares divided by the number of items.<\/p>\n<p>Finally, the <em>return<\/em> statement returns the square root of variable <code>variance<\/code>, which is the whole population standard deviation value. Remember to link in the math library to assist with the <em>sqrt()<\/em> function.<\/p>\n<p>Here&#8217;s a sample run:<\/p>\n<p><code>Values: 10 12 23 23 16 23 21 16<br \/>\nThe standard deviation is 4.8990<\/code><\/p>\n<p>I checked my solution&#8217;s result with the standard deviation calculated by ChatGPT on the same data set and received the same value. So I think I&#8217;m good. I hope you&#8217;re solution also met met with success and that you didn&#8217;t use ChatGPT to write it for you.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>The challenge for this month&#8217;s Exercise is to calculate the standard deviation for a given set of data points. It&#8217;s a &#8220;whole population calculation&#8221; because all the data points are present. The trick is to follow the equation, transforming it &hellip; <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=6391\">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-6391","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\/6391","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=6391"}],"version-history":[{"count":4,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/6391\/revisions"}],"predecessor-version":[{"id":6411,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/6391\/revisions\/6411"}],"wp:attachment":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=6391"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=6391"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=6391"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}