Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>I do not know C/C++ but here is a general idea of the loop assuming the values are coming from an array. (<em>since I am unaware of how they are coming in, i.e. user input, etc.</em>)</p> <h3>Logic:</h3> <ul> <li>Use a <code>for</code> loop structure opposed to a <code>while</code>, to loop over each element of the array.</li> <li>Initialize two variables to keep count, <code>positiveSum</code> and <code>negativeSum</code>.</li> <li>At each iteration of the element, check to see if it's greater than <code>0</code>. That's how you can divide the positive and negative numbers accordingly.</li> <li>If greater than zero, add the element onto the running <code>positiveSum</code>, else add it to the running sum of <code>negativeSum</code>.</li> <li>When the loop finishes, <code>positiveSum</code> and <code>negativeSum</code> should have the calculated sum.</li> </ul> <p>If this is homework, (<em>I don't remember if the homework tag was there prior to the question, or was added on later</em>) this pseudo code should point you in the right direction without explicitly doing the <em>entire</em> work for you.</p> <h3>Pseudo Java Code (<em>not tested or compiled</em>)</h3> <pre><code> // as a good convention, I always initialize variables, // for numbers I always use zero's. double positiveSum, negativeSum = 0.0; // assuming array holds the array of values. for (i=0; i &lt; array.length; i++) { // if positive, add it to the count if (array[i] &gt; 0) positiveSum = positiveSum + array[i]; // else negative else negativeSum = negativeSum + array[i]; } </code></pre> <p>Once it's completed, both <code>positiveSum</code> and <code>negativeSum</code> should hold the correct calculated sum. </p> <p>If you have any questions along the way, I can edit my answer to help you achieve the correct answer, I wish I could give it away but that's what your responsibilities are for homework. </p>
 

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