{"id":1167,"date":"2015-01-17T00:01:41","date_gmt":"2015-01-17T08:01:41","guid":{"rendered":"http:\/\/c-for-dummies.com\/blog\/?p=1167"},"modified":"2021-07-24T09:22:04","modified_gmt":"2021-07-24T16:22:04","slug":"averaging-an-array-of-unknown-size","status":"publish","type":"post","link":"https:\/\/c-for-dummies.com\/blog\/?p=1167","title":{"rendered":"Averaging an Array of Unknown Size"},"content":{"rendered":"<p>Imagine  you&#8217;re working with an array that has room to store thousands of integer values. You&#8217;ve been hired to craft a function that averages those values, but you don&#8217;t really know how many values are stored in the array. The guy who gave you the assignment (me), simply said that the array is capped with a zero value.<br \/>\n<!--more--><br \/>\nIn last week&#8217;s <a href=\"http:\/\/c-for-dummies.com\/blog\/?p=1155 \">Lesson<\/a>, I showed you how to mark the end of an array by capping it with a zero value. That&#8217;s an approach similar to the way character arrays (strings) work in the C language.<\/p>\n<p>Once you know that an array is capped with a zero, you can manipulate the array&#8217;s elements and know exactly when they stop. In this week&#8217;s example code, I borrow the <em>average()<\/em> function from this month&#8217;s <a href=\"http:\/\/c-for-dummies.com\/blog\/?p=1138\">Exercise<\/a>, modifying it to handle an <em>int<\/em> array capped with a zero value. (The core of the <em>main()<\/em> function is stolen from last week&#8217;s <a href=\"http:\/\/c-for-dummies.com\/blog\/?p=1155 \">Lesson<\/a>.)<\/p>\n<pre class=\"screen\">\r\n#include &lt;stdio.h&gt;\r\n\r\n#define MAX 10\r\n\r\nint average(int *a);\r\n\r\nint main()\r\n{\r\n    int number[MAX];\r\n    int count,avg;\r\n\r\n    count = 0;\r\n    while(count < MAX)\r\n    {\r\n        printf(\"Type your favorite number, 0 to end: \");\r\n        scanf(\"%d\",&#038;number[count]);\r\n        if( number[count] == 0 )\r\n            break;\r\n        count++;\r\n    }\r\n    avg = average(number);\r\n    printf(\"The average of those values is %d\\n\",avg);\r\n\r\n    return(0);\r\n}\r\n\r\nint average(int *a)\r\n{\r\n    int x,total;\r\n\r\n    x = total = 0;\r\n\r\n    while(a[x])\r\n    {\r\n        total += a[x];\r\n        x++;\r\n    }\r\n    return(total\/x);\r\n}<\/pre>\n<p>Does <em>average()<\/em> need to be an <em>int<\/em> function? Look at the <code>return<\/code> statement.<\/p>\n<p>In this case, I coded the function to return an <em>int<\/em> value because integers are being processed. That's okay, but it's not going to be precise. My advice would be to calculate an average as a floating point value. In fact, the <em>average()<\/em> functions I've seen in other languages are defined as floating point.<\/p>\n<p>An obvious question at this point would be, \"What if you're averaging an array and zero is a valid value?\"<\/p>\n<p>The stock answer is, \"Then you use the value -1, or any negative number, to cap the array.\" That happens a lot in C.<\/p>\n<p>Then again, what if <em>all<\/em> values &mdash; the entire range for the variable type, negative to positive and zero &mdash; are allowed in the array?<\/p>\n<p>In that case, a solution is still possible &mdash; but you have to <em>cheat<\/em>. I'll present that cheat in <a href=\"http:\/\/c-for-dummies.com\/blog\/?p=1179\">next week's Lesson<\/a>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>The secret is to find where the array ends. <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=1167\">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-1167","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\/1167","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=1167"}],"version-history":[{"count":7,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/1167\/revisions"}],"predecessor-version":[{"id":4906,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/1167\/revisions\/4906"}],"wp:attachment":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1167"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1167"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1167"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}