# Thread: Split positive and negative numbers from one array into two arrays

1. ## Split positive and negative numbers from one array into two arrays

Hi,

I've been toiling over this problem for about two weeks, and I'll be the first to admit I have no idea what I'm doing. I'm in a beginner C programming class that I only needed to take for graduating, so I'm not a programmer and I don't want to be one.

That said, I don't mind the challenge, it's just that this is getting frustrating.

So the problem is this:

Define an array w/ 20 integer values, fill array with numbers (input or hard-coded)
Write function called split() to read array and put all + #s in "positive" array and - #s into "negative" array
Call a function to display values in both positive & negative arrays
Extend program to sort positive & negative arrays in ascending order before they are displayed

I created the major array with 20 values and put the numbers in there. I kind of wrote the split() function except I probably did everything wrong. What I did was piece together different parts from the section on Arrays in my textbook as far as I understood them, and it's not working right.

Here's my code:

Code:
```#include <stdio.h>
#define MAXELS 20

int split(int [], int, int);

int main()
{
int vals[MAXELS] = {12, 34, -43, 4, -34, 0, 11, 23, 46, 33, -67, 90, 100, 13, -16, 29, 17, 18, 19, 20} ;
int value;

value = split(vals, MAXELS, 0);

return 0;
}

int split(int num[], int pos, int neg)
{
int x, y;

for (x = 0; x < MAXELS; x++)
if (num[x] >= 0)
num[x] = num[pos];
for (y = 0; y < MAXELS; y++)
if (num[y] <= 0)
num[y] = num[neg];

printf("Positive numbers: \n", x);
printf("Negative numbers: ", y);

return (x, y);
}```
It compiles but the Positive Numbers: and Negative Numbers: print out nothing.

I most likely am missing a concept (or several) here, but I have my textbook open to the Arrays chapter and I'm willing to learn if anyone is kind enough to help me out. I'm kind of embarrassed to throw my code out there because it probably makes NO sense! Thanks in advance though!

2. In split(), you need two more int variables to keep the count of pos and neg numbers. Right now, you are overlaying the passed array.

Also, you could get rid of your second loop by just putting an ELSE after the inner IF, because IF the value is not positive, it is negative.

I kind of understood what you meant about overlaying the array, but not entirely, sorry about that... can you give me an example of what you mean?

I modified my code a little, hopefully not for the worse.

Code:
```int split(int []);

int main()
{
int vals[MAXELS] = {12, 34, -43, 4, -34, 0, 11, 23, 46, 33, -67, 90, 100, 13, -16, 29, 17, 18, 19, 20} ;
int value;

value = split(vals);

return 0;
}

int split(int num[])
{
int i;
int pos;
int neg;

for (i = 0; i < MAXELS; i++)
if (num[i] >= 0)
pos = num[i];
else
neg = num[i];

printf("Positive numbers: \n", pos);
printf("Negative numbers: ", neg);

return (pos, neg);
}```

4. He means you need separate arrays and corresponding sizes to hold the positive and negative values.

Also, the line
Code:
`return (pos, neg);`
is not going to do what you think it does. You can only return a single value from a function.

Your split function should resemble the following:

Code:
```void split(int values[], int positive_values[], int* positive_count, int negative_values[], int* negative_count)
{
/* split() implementation */
}```
Notice that since you can only return a single value, you need to pass these parameters by reference (by pointer).

5. For your counting, that's not quite right. Do this...
Code:
```int pos = 0 ;
int neg = 0 ;

for (i = 0; i < MAXELS; i++)
if (num[i] >= 0)
pos = pos + 1 ;
else
neg = neg + 1 ;```

6. Thanks for clarifying, NeonBlack. I understand what you mean about using pointers. But is there a way to do it without pointers?

I'm just asking because the section in my textbook does not use pointers in its functions in the example problems.

7. Originally Posted by svm
... But is there a way to do it without pointers?
...
Since you know the total, just return the pos value. When you report it, report the pos (as returned), and then report the neg as total-pos.

8. Originally Posted by Dino
For your counting, that's not quite right. Do this...
Code:
```int pos = 0 ;
int neg = 0 ;

for (i = 0; i < MAXELS; i++)
if (num[i] >= 0)
pos = pos + 1 ;
else
neg = neg + 1 ;```
Ahhhh, I see.... thanks for that.

Edit:

Actually now that I thought about it more that's not really what I need to do. That returns the count of all the positive and negative numbers in my array. I need to place all the positives in its own array, then all the negatives in its own array, then display both arrays. Thanks though - but now I'm confused!

9. Oh geez I feel stupid

Gotta put %d in the printf statements. That'll do something more than what it does now at least...

10. ## Got it, roughly

So, I got it to do what I want, kind of.

It displays which numbers are positive in my vals array, and which ones are negative.

Code:
```#include <stdio.h>
#define MAXELS 20

int split(int []);

int main()
{
int vals[MAXELS] = {12, 34, -43, 4, -34, 0, 11, 23, 46, 33, -67, 90, 100, 13, -16, 29, 17, 18, 19, 20} ;
int value;

value = split(vals);

return 0;
}

int split(int num[])
{
int x, y;
int pos;
int neg;

for (x = 0; x < MAXELS; x++)

if (num[x] >= 0)
printf("%d\n", pos = num[x]);

for (y = 0; y < MAXELS; y++)
if (num[y] < 0)
printf("%d\n ", neg = num[y]);

}```
Output:
12
34
4
0
11
23
46
33
90
100
13
29
17
18
19
20
-43
-34
-67
-16

It kind of does what the problem asks ( probably not all the way, but at least SOMETHING happens), and I don't even want to try putting these numbers in ascending order. My brain is hurting enough! But if you want to give me more information feel free to do so, every bit helps.

11. Instead of printing out the value, incorporate some logic to copy the value into the appopriate (positive or negative) array.

Personally, if the original order of values in the input array doesn't matter, my first step would be to sort the whole array before picking values to put into different arrays. The negative and positive values would then be easy to find .....

12. Originally Posted by Dino
Since you know the total, just return the pos value. When you report it, report the pos (as returned), and then report the neg as total-pos.
That won't handle 0 values though.

13. Originally Posted by grumpy
Instead of printing out the value, incorporate some logic to copy the value into the appopriate (positive or negative) array.

Personally, if the original order of values in the input array doesn't matter, my first step would be to sort the whole array before picking values to put into different arrays. The negative and positive values would then be easy to find .....
Actually if he sorted it in ascending order, all he needs to do is find the first positive value and set a pointer to it... now he effectively has two arrays split by the pointer.

14. Originally Posted by CommonTater
Actually if he sorted it in ascending order, all he needs to do is find the first positive value and set a pointer to it... now he effectively has two arrays split by the pointer.
Yep.

15. Originally Posted by CommonTater
Actually if he sorted it in ascending order, all he needs to do is find the first positive value and set a pointer to it... now he effectively has two arrays split by the pointer.
Tater, you always favor the positive numbers -- it ain't fair!!