Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>TF,</p> <p><strong>DISCLAIMER:</strong> This code is just off the top of my head... it hasn't even been compiled, let alone tested.</p> <pre><code>// returns the average of array[first..last] inclusive. int average(int[] array, int first, int last) { int sum = 0; for (i = first; i &lt;= last; i++) sum += array[i]; return sum / (last - first + 1); // not sure about the +1 } </code></pre> <p>Then what you'd do is loop through the indexes of your <code>frequencyBar</code> array [0..7], setting <code>frequencyBar[i] = average(array, first, last);</code>... the tricky bit is calculating the <code>first</code> and <code>last</code> indexes... try <code>i*8</code> and <code>(i+1)*8</code> respectively... that may not be exactly right, but it'll be close ;-)</p> <p>Cheers. Keith.</p> <hr> <p><strong>EDIT:</strong> Bored... waiting for my test results to come back. No news is good news, right? ;-)</p> <p>It turns out that passing the <code>length</code> is a fair bit simpler than passing the <code>last</code> index.</p> <pre><code>#include &lt;stdio.h&gt; int sum(int array[], int first, int length) { int sum = 0; for (int i = first; i &lt; first+length; i++) sum += array[i]; return sum; } double average(int array[], int first, int length) { double total = sum(array, first, length); #ifdef DEBUG printf("DEBUG: [%2d..%2d] %d", first, first+length-1, array[first]); for (int i = first+1; i &lt; first+length; i++) printf(" + %d", array[i]); printf(" = %d / %d = %f\n", (int)total, length, total/length); #endif return total / length; } int main(int argc, char* argv[]) { int array[] = { // average 1, 2, 3, 4, 5, 1, 2, 3, // 2.625 4, 5, 1, 2, 3, 4, 5, 1, // 3.125 2, 3, 4, 5, 1, 2, 3, 4, // 3 5, 1, 2, 3, 4, 5, 1, 2, // 2.875 3, 4, 5, 1, 2, 3, 4, 5, // 3.375 1, 2, 3, 4, 5, 1, 2, 3, // 2.625 4, 5, 1, 2, 3, 4, 5, 1, // 3.125 2, 3, 4, 5, 1, 2, 3, 4 // 3 }; double frequency[8]; for (int i = 0; i &lt; 8; i++) frequency[i] = average(array, i*8, 8); for (int i = 0; i &lt; 8; i++) printf("%f ", frequency[i]); printf("\n"); } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload