Split array into positive and negative

I have a program where I've filled an array with 20 positive and negative integers. I'm supposed to split this original array into two different arrays, one for positive and one for negative integers.
The problem is, when I go to display the positive array, in the spaces where there were negative values in the original array, it displays garbage numbers I don't want. Same with the negative array, in the spaces where there were positive numbers it displays other stuff.
I think there is a problem with the way I am incrementing within my for loop which is causing this, but I don't know how to fix it.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
  #include <iostream>

using namespace std;

//function prototypes
int splitNeg(int [], int [], int);
int splitPos(int [], int [], int);
void displayArr(int [], int);

int main ()
{
	const int SIZE = 20;
	int usedPos, usedNeg;
	int origArr[SIZE] = {4, -7, 12, 6, 8, -3, 30, 7, -20, -13, 17, 6, 31, -4, 3, 19, 15, -9, 12, -18};
	int posArray[SIZE];
	int negArray[SIZE];
	
	
	usedPos = splitPos(origArr, posArray, SIZE);
	usedNeg = splitNeg(origArr, negArray, SIZE);


	cout << "Positive Values: " << endl;
	displayArr(posArray, usedPos);
	cout << endl;
	cout << "Negative Values: " << endl;
	displayArr(negArray, usedNeg);

	return 0;
}

int splitPos(int origArr[], int posArray[], int SIZE)
{
	int j = 0;
	for (int i = 0; i < SIZE; i++ && j++)
	{
		if (origArr[i] >= 0)
			posArray[j] = origArr[i];
			
	}
	
	return j;

}

int splitNeg(int origArr[], int negArray[], int SIZE)
{
	int k = 0;
	for (int i = 0; i < SIZE; i++ && k++)
	{
		if (origArr[i] < 0)
			negArray[k] = origArr[i];
		
	}
	
	return k;
}

void displayArr(int newArray[], int used)
{
	for (int i = 0; i < used; i++)
		cout << newArray[i] << endl;
	return;

}

In splitPos you should only increment j when a positive number is found (and placed into the posArray), and in splitNeg you should only increment k when a negative number is found. At the moment they are incremented every iteration.
Got it, that fixed the problem. Thanks so much!
Topic archived. No new replies allowed.