1

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!

9
  • 2
    Please include the errors into your question. Commented Nov 15, 2013 at 3:58
  • 1
    What is the first error you get? Did you try to debug? Did you try to run your code before it has been completed? Commented Nov 15, 2013 at 3:58
  • There are literally too many to include. I tested the code up to and including the "void count(int ARRAY[], int SIZE, int&NEG, int&POS);" function. The first few errors are: "line49:invalid initialization of reference type 'int&' from expression of type 'int*'. line13: error in passing argument 3 of 'void split(int*, int,int&, int, int&, int)' and line52: no match for 'operator<<' in 'std::cout << print_array(NEG_ARRAY, NEG)'. After that I see a lot of '<_CharT, _Traits>' in the errors if that helps Commented Nov 15, 2013 at 4:06
  • Why are you setting NEG and POS to zero in your split function? These don't even need to be passed into split. If we assume that NEG_ARRAY and POS_ARRAY are the correct size, you might as well use local variables to keep track of the current place in these arrays. If we don't assume this, what you use the size variables for still doesn't make sense - it would make more sense to use them, in this case, to make sure you are within the valid indices. Commented Nov 15, 2013 at 4:08
  • Shouldn't you be reading the value before storing it? so cin>>x; before ARRAY[numEle] = x, otherwise you'll lose the first value and you'll be "off by one". Commented Nov 15, 2013 at 4:08

3 Answers 3

2

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++;
}
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for all of your help. Explanations were very helpful.
2

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.

Comments

0

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!

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.