Note that there are some explanatory texts on larger screens.

plurals
  1. POSplitting array into separate positive and negative arrays c++
    primarykey
    data
    text
    <p>I need to write a function that takes a given array and then splits it into two separate arrays with one array's elements being the positive elements of the main array and the other's elements being the negative elements of the main array. I can't seem to figure out what the loop to do this would look like. </p> <p>I have written a separate function to determine how many positive and negative values are in the main array:</p> <pre><code>void count(int ARRAY[], int SIZE, int&amp;NEG, int&amp;POS) { for (int x=0; x&lt;SIZE; x++) { if(ARRAY[x]&gt;=0) { POS=POS+1 ; } if(ARRAY[x]&lt;0) { NEG=NEG+1 ; } } } </code></pre> <p>This counts the positives and negatives and the number of each would be the size of the respective positive and negative arrays after the split. </p> <p>I have defined the function as such:</p> <pre><code> void split(int ARRAY[], int SIZE, int&amp;NEG_ARRAY, int NEG, int&amp;POS_ARRAY, int POS) </code></pre> <p>I just don't know how to set each of the positive elements in the main array as the elements in the new Positive-Only array and likewise for the negative array.</p> <p>Thanks for your help!</p> <p>After using the answers given and doing my best with the rest of the code, I got about a million lines of errors when trying to compile it. Is there a problem with how I am deleting the three dynamically allocated arrays? What huge error is preventing compiling? Here is my code:</p> <pre><code>#include &lt;iostream&gt; using namespace std; void count(int ARRAY[], int SIZE, int&amp;NEG, int&amp;POS); void split(int ARRAY[], int SIZE, int&amp;NEG_ARRAY, int NEG, int&amp;POS_ARRAY, int POS); void print_array(int ARRAY[], int SIZE); int main() { int SIZE (0); int * ARRAY ; cout&lt;&lt;"Enter number of elements: " ; cin&gt;&gt; SIZE ; ARRAY = new int[SIZE] ; int x(0); int numEle(0); cout&lt;&lt;"Enter list: " &lt;&lt;endl; while(numEle&lt;SIZE) { ARRAY[numEle] = x ; numEle++; cin&gt;&gt;x; } int POS(0), NEG(0) ; count(ARRAY, SIZE, NEG, POS) ; int * NEG_ARRAY; NEG_ARRAY = new int[NEG]; int * POS_ARRAY; POS_ARRAY = new int[POS]; split(ARRAY, SIZE, NEG_ARRAY, NEG, POS_ARRAY, POS) ; cout&lt;&lt;"Negative elements: "&lt;&lt;endl; cout&lt;&lt;print_array(NEG_ARRAY, NEG) &lt;&lt;endl; cout&lt;&lt;"Non-negative elements: "&lt;&lt;endl; cout&lt;&lt;print_array(POS_ARRAY, POS)&lt;&lt;endl; delete[] ARRAY; delete[] NEG_ARRAY; delete[] POS_ARRAY; return 0; } void count(int ARRAY[], int SIZE, int&amp;NEG, int&amp;POS) { for (int x=0; x&lt;SIZE; x++) { if(ARRAY[x]&gt;=0) { POS=POS+1 ; } if(ARRAY[x]&lt;0) { NEG=NEG+1 ; } } } void split(int ARRAY[], int SIZE, int&amp;NEG_ARRAY, int NEG, int&amp;POS_ARRAY, int POS) { NEG=POS=0; for(int x=0; x&lt;SIZE; x++) { if(ARRAY[x]&lt;0) { NEG_ARRAY[NEG++]=ARRAY[x]; } else {POS_ARRAY[POS++]=ARRAY[x]; } } } void print_array(int ARRAY[], int SIZE) { for(int i=0; i&lt;SIZE; i++) { cout &lt;&lt; ARRAY[i] &lt;&lt; " " ; } cout&lt;&lt;endl; } </code></pre> <p>The code is supposed to read in the array and display the new negative and positive arrays. Thanks in advance!</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

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