Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<pre><code>ave = sum/array.size(); </code></pre> <p>That will do integer division. The average is very likely going to be a floating point value, so you need to cast first:</p> <pre><code>float ave = static_cast&lt;float&gt;(sum) / array.size(); </code></pre> <p>To do the other averages, you'll want to sum up only the positive and negative values, respectively, and then divide by the number of positive and negative numbers, respectively.</p> <p>Just to show it can be done:</p> <pre><code>void averages(const std::vector&lt;int&gt;&amp; vec, double&amp; overall_average, double&amp; positive_average, double&amp; negative_average) { std::tuple&lt;int, int, int, int, int, int&gt; sums_and_counts = std::accumulate(vec.begin(), vec.end(), std::make_tuple(0, 0, 0, 0, 0, 0), [](std::tuple&lt;int, int, int, int, int, int&gt; t, int i) { std::get&lt;0&gt;(t) += i; std::get&lt;1&gt;(t) += 1; if (i &lt; 0) { std::get&lt;2&gt;(t) += i; std::get&lt;3&gt;(t) += 1; } if (i &gt; 0) { std::get&lt;4&gt;(t) += i; std::get&lt;5&gt;(t) += 1; } return t; }); overall_average = positive_average = negative_average = 0.0; if (std::get&lt;1&gt;(sums_and_counts)) { overall_average = static_cast&lt;double&gt;(std::get&lt;0&gt;(sums_and_counts)) / std::get&lt;1&gt;(sums_and_counts); } if (std::get&lt;3&gt;(sums_and_counts)) { negative_average = static_cast&lt;double&gt;(std::get&lt;2&gt;(sums_and_counts)) / std::get&lt;3&gt;(sums_and_counts); } if (std::get&lt;5&gt;(sums_and_counts)) { positive_average = static_cast&lt;double&gt;(std::get&lt;4&gt;(sums_and_counts)) / std::get&lt;5&gt;(sums_and_counts); } } </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