Note that there are some explanatory texts on larger screens.

plurals
  1. POAverage of odd cells?
    text
    copied!<p>For an exercise I am doing, I am supposed to find out the average of the items contained in odd numbered cells of an array and some other things. Finding the average of the odd numbered cells in the only thing I'm having a problem with. Here is my code, what am I doing wrong? The final function is the odd numbered cells average function. Thanks.</p> <pre><code>#include &lt;iostream&gt; #include &lt;iomanip&gt; using namespace std; const int SIZE = 1000; int randoms[SIZE]; int sum2 = 0; int top = 0; int maximum = 0; int highest(int randoms[]); int findsum(int randoms[]); int average(int randoms[], int sum); int oddavg(int randoms[]); int main() { int sum = 0; int top = 0; int avg = 0; int oddaverage = 0; for (int i = 0; i &lt; SIZE; i++) { randoms[i] = (rand() % 5000 + 1); cout &lt;&lt; randoms[i] &lt;&lt; setw(10) &lt;&lt; " "; } cout &lt;&lt; endl &lt;&lt; endl; cout &lt;&lt; "The sum of the values in the array is "; sum = findsum(randoms); cout &lt;&lt; sum &lt;&lt; endl; cout &lt;&lt; endl &lt;&lt; endl; cout &lt;&lt; "The highest value in the array is "; top = highest(randoms); cout &lt;&lt; top &lt;&lt; endl; cout &lt;&lt; endl &lt;&lt; endl; cout &lt;&lt; "The average of all of the numbers in the array is "; avg = average(randoms, sum); cout &lt;&lt; avg &lt;&lt; endl; cout &lt;&lt; endl &lt;&lt; endl; cout &lt;&lt; "The average of all of the numbers in the odd cells is "; oddaverage = oddavg(randoms); } int findsum(int randoms[]) { for (int i = 0; i &lt; SIZE; i++) { sum2 += randoms[i]; } return sum2; } int highest(int randoms[]) { for (int i = 0; i &lt; SIZE; i++) { if (randoms[i] &gt; maximum) { maximum = randoms[i]; } } top = maximum; return top; } int average(int randoms[], int sum) { int avg = 0; for (int i = 0; i &lt; SIZE; i++) { avg = (sum / SIZE); } return avg; } int oddavg(int randoms[]) { int avg = 0; int sum = 0; for (int i = 0; i &lt; SIZE; i++) { if (randoms[i] / 2 == 1) { sum += randoms[i]; } } avg = sum / SIZE; return avg; } </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