简体   繁体   中英

Splitting array into separate positive and negative arrays c++

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.

I have written a separate function to determine how many positive and negative values are in the main array:

void count(int ARRAY[], int SIZE, int&NEG, int&POS)
{

  for (int x=0; x<SIZE; x++)
    {
      if(ARRAY[x]>=0)
      {
      POS=POS+1 ;
      }
      if(ARRAY[x]<0)
      {
      NEG=NEG+1 ;
      }
    }
}

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.

I have defined the function as such:

 void split(int ARRAY[], int SIZE, int&NEG_ARRAY, int NEG, int&POS_ARRAY, int POS)

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.

Thanks for your help!

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:

#include <iostream>
using namespace std;


void count(int ARRAY[], int SIZE, int&NEG, int&POS);
void split(int ARRAY[], int SIZE, int&NEG_ARRAY, int NEG, int&POS_ARRAY, int POS);
void print_array(int ARRAY[], int SIZE);


int main()
{

  int SIZE (0);
  int * ARRAY ;

  cout<<"Enter number of elements: " ;
  cin>> SIZE ;

  ARRAY = new int[SIZE] ;
  int x(0);
  int numEle(0);

  cout<<"Enter list: " <<endl;

  while(numEle<SIZE)
    {
      ARRAY[numEle] = x ;
      numEle++;
      cin>>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<<"Negative elements: "<<endl;
  cout<<print_array(NEG_ARRAY, NEG) <<endl;

  cout<<"Non-negative elements: "<<endl;
  cout<<print_array(POS_ARRAY, POS)<<endl;


  delete[] ARRAY;
  delete[] NEG_ARRAY;
  delete[] POS_ARRAY;

  return 0;
}



void count(int ARRAY[], int SIZE, int&NEG, int&POS)
{

  for (int x=0; x<SIZE; x++)
    {
      if(ARRAY[x]>=0)
      {
      POS=POS+1 ;
      }
      if(ARRAY[x]<0)
      {
      NEG=NEG+1 ;
      }
    }
}

void split(int ARRAY[], int SIZE, int&NEG_ARRAY, int NEG, int&POS_ARRAY, int POS)
{

  NEG=POS=0;
  for(int x=0; x<SIZE; x++)
    {
      if(ARRAY[x]<0)
    { NEG_ARRAY[NEG++]=ARRAY[x]; }
      else {POS_ARRAY[POS++]=ARRAY[x]; }

    }
}

void print_array(int ARRAY[], int SIZE)
{

  for(int i=0; i<SIZE; i++)
    {

      cout << ARRAY[i] << " " ;
    }
  cout<<endl;
}

The code is supposed to read in the array and display the new negative and positive arrays. Thanks in advance!

You may get some C style answers

But here how I'd do using STL algorithms, as this is tagged for C++

Use std::partition

bool is_pos(int i) { return i > 0; }

auto p = std::partition(std::begin(ARRAY), 
         std::end(ARRAY), std::ptr_fun(is_pos));

std::copy(std::begin(ARRAY), p, std::begin(POS_ARRAY));

std::copy(p,  std::end(ARRAY), std::begin(NEG_ARRAY));

Also you should use std::vector for such operations

Demo Here

Its easy to modify your count() function:

void split(int ARRAY[], int SIZE, int NEG [], int POS [])
{
  int ncount = 0, pcount = 0;
  for (int x=0; x<SIZE; x++)
    {
      if(ARRAY[x]>=0)
      {
          POS[pcount++] = ARRAY[x];
      }
      if(ARRAY[x]<0)
      {
          NEG[ncount++] = ARRAY[x];
      }
    }
}

This code will divide negative & positive numbers into separate arrays,

void split(int ARRAY[], int SIZE, int NEG_ARRAY[], int&NEG, int POS_ARRAY[], int&POS)
{
    NEG=POS=0;
    for (int i(0); i<SIZE; i++)
    {
        if (ARRAY[i]<0) NEG_ARRAY[NEG++]=ARRAY[i];
        else POS_ARRAY[POS++]=ARRAY[i];
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM