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

  1. #1
    Registered User
    Join Date
    Nov 2010
    Posts
    8

    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. #2
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Chappell Hill, Texas
    Posts
    2,332
    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.
    Mainframe assembler programmer by trade. C coder when I can.

  3. #3
    Registered User
    Join Date
    Nov 2010
    Posts
    8
    Hi Dino, thanks for your reply.

    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. #4
    Registered User NeonBlack's Avatar
    Join Date
    Nov 2007
    Posts
    431
    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).
    I copied it from the last program in which I passed a parameter, which would have been pre-1989 I guess. - esbo

  5. #5
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Chappell Hill, Texas
    Posts
    2,332
    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 ;
    Mainframe assembler programmer by trade. C coder when I can.

  6. #6
    Registered User
    Join Date
    Nov 2010
    Posts
    8
    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. #7
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Chappell Hill, Texas
    Posts
    2,332
    Quote Originally Posted by svm View Post
    ... 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.
    Mainframe assembler programmer by trade. C coder when I can.

  8. #8
    Registered User
    Join Date
    Nov 2010
    Posts
    8
    Quote Originally Posted by Dino View Post
    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!
    Last edited by svm; 11-26-2010 at 05:54 PM. Reason: thought about it more

  9. #9
    Registered User
    Join Date
    Nov 2010
    Posts
    8
    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. #10
    Registered User
    Join Date
    Nov 2010
    Posts
    8

    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. #11
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    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 .....
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  12. #12
    Registered User NeonBlack's Avatar
    Join Date
    Nov 2007
    Posts
    431
    Quote Originally Posted by Dino View Post
    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.
    I copied it from the last program in which I passed a parameter, which would have been pre-1989 I guess. - esbo

  13. #13
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by grumpy View Post
    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. #14
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Quote Originally Posted by CommonTater View Post
    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.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  15. #15
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Quote Originally Posted by CommonTater View Post
    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!!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Load Array From Whitespace-delimited TextFile
    By pantherman34 in forum C Programming
    Replies: 7
    Last Post: 04-30-2010, 06:12 PM
  2. Reading negative numbers from command line
    By Ace2187 in forum C Programming
    Replies: 3
    Last Post: 12-06-2009, 01:21 PM
  3. Changing from positive to negative numbers
    By vopo in forum C++ Programming
    Replies: 19
    Last Post: 09-10-2008, 02:21 PM
  4. Using TextOut() to align positive & negative numbers
    By csonx_p in forum Windows Programming
    Replies: 4
    Last Post: 05-27-2008, 07:12 AM
  5. how to handle integer overflow in C
    By kate1234 in forum C Programming
    Replies: 8
    Last Post: 04-23-2003, 12:20 PM