简体   繁体   中英

C++ Splitting an array into 2 separate arrays

I need to write a program 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.

After doing my best with 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 a new negative and a new positive array. Thanks in advance!

You have the following bugs:

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

change to :

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

also the :

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

change to :

void split(int ARRAY[], int SIZE, int*NEG_ARRAY, int NEG, int*POS_ARRAY, int POS){..}

and

cout<<print_array(NEG_ARRAY, NEG) <<endl
cout<<print_array(NEG_ARRAY, POS) <<endl;

to :

print_array(NEG_ARRAY, NEG);
print_array(NEG_ARRAY, POS);

After fixed these bugs, it can compile and run well.

There is a bunch of errors in your code. The worst one is passing the arrays by references in the declaration and definition of the split function. Change both to void split(int ARRAY[], int SIZE, int *NEG_ARRAY, int NEG, int *POS_ARRAY, int POS); , and most of the errors will be gone.

The rest is from the two lines in which you print the array in your main :

cout<<print_array(NEG_ARRAY, NEG) <<endl;

You don't want to print the function, you want to use the function to print inside it (which you do correctly). You need to change the calls to simply:

print_array(NEG_ARRAY, NEG);

And that'll make your code compile.

Hovewer there's one more error, which will make the whole app work in an improper way. In the place you input the values, you need to get the input from cin before inputting it in the array. Like this:

while(numEle<SIZE) {
  cin>>x;
  ARRAY[numEle] = x ;
  numEle++;
}

First of all, using a std::vector is almost always nicer than using dynamically allocated C arrays. You don't get the horrible mixture of pointers and square bracket array access, and you don't need to pass round extra size variables.

Secondly, the standard library has some nice algorithms to help do what you want to do. Let's assume that you write the given numbers into a vector called vec . You can then use std::partition to move all the elements less than zero to the first half of the vector, and all the elements greater than or equal to zero to the second half, like so:

inline bool less_than_zero(int a)
{
    return a < 0;
}

std::vector<int>::iterator midpoint = std::partition(vec.begin(),
                                                     vec.end(),
                                                     less_than_zero);

(There are other ways of specifying the predicate, but a simple function definition like this is easiest for demonstration purposes.)

The returned iterator points to the first item in the vector which is non-negative. So now you can easily copy the values into two new vectors:

std::vector<int> negative(vec.begin(), midpoint);
std::vector<int> positive(midpoint, vec.end());

And that's it!

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