Thread: count the number of positive and negative numbers

  1. #1
    Registered User
    Join Date
    Apr 2009
    Posts
    16

    count the number of positive and negative numbers

    Program Description:
    Ask the user to input however many positive and negative numbers they would like (spaces between numbers). Count and display number of positive and negative numbers.

    The Problem:
    My solution for this problem was to put each number in one of two arrays 'positive' or 'negative'. Then output the count of each array. I just don't know how to store each number that is inputted by the user. It would be easier if I knew how many numbers was going to be inputted but oh well:P

    I am still new to C and have grown accustomed to using fgets and sscanf, so I hope thats ok. However, I put '???' in the third argument of sscanf because I am not sure what do put there. Just looking for a little guidance, thanks people

    The Code:
    Code:
    /*
    Exercise 7-6 : Write a program that takes a series of numbers and counts the
    number of positive and negative values.
    
    */
    /*
    Exercise 7-6 : Write a program that takes a series of numbers and counts the
    number of positive and negative values.
    
    */
    #include<stdio.h>
    
    char line[100];	//read line input
    int listPosNums[]; //will hold negative list of numbers
    int listNegNums[]; //will hold positive list of numbers
    int main(void)
    {
    	printf("Please enter a list of positive and negative numbers (separated by spaces): ");
    	fgets(line, sizeof(line), stdin);
    	sscanf(line, "%d%n", ???);
    	
    	return 0;
    }

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Code:
    int listPosNums[]; //will hold negative list of numbers
    int listNegNums[]; //will hold positive list of numbers
    Neither of these are complete. They have no specified size. Unless you actually need to display each positive and negative, then you don't really need arrays. It doesn't sound like that's a requirement, so you could just make a counter for positive and a counter for negative.

    Or you could just have one array of numbers, and just read all of them, then loop through it and count/display either positive or negative. There are a few ways you could do this task.


    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    Apr 2009
    Posts
    16
    Quote Originally Posted by quzah View Post
    Code:
    int listPosNums[]; //will hold negative list of numbers
    int listNegNums[]; //will hold positive list of numbers
    Neither of these are complete. They have no specified size. Unless you actually need to display each positive and negative, then you don't really need arrays. It doesn't sound like that's a requirement, so you could just make a counter for positive and a counter for negative.

    Or you could just have one array of numbers, and just read all of them, then loop through it and count/display either positive or negative. There are a few ways you could do this task.


    Quzah.
    I like the idea of just creating a counter for a positive and negative. However, in my sscanf() what do I put in the 2nd and third arguments since I do not know how many numbers the user will enter?

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    sscanf works just like scanf does, except the first argument is what buffer you are reading from.
    Code:
    sscanf( frombuffer, "thisformat", &theseargs );
    Generally it goes something like that.
    Code:
    while( x < arraysize && fgets(...) != NULL )
        if( sscanf( buf, "%d", &numbers[ x++ ] ) != 1 )
            x--; /* reset our counter and ignore this line */
    Since you don't actually have to care what they enter, you don't actually need to read into an array. Just read it into an integer, see if it's < 0 or not, and increment the appropriate counter.

    Quzah.
    Hope is the first step on the road to disappointment.

  5. #5
    Registered User
    Join Date
    Apr 2009
    Posts
    16
    Sorry quzah I just don't understand. What I have so far should enter all the values into the array listNumbers correct? I'm so confused on how to count each pos and neg number. It's not homework but it is a problem in my practical c programming book that has bothered me for a few days.
    Code:
    /*
    Exercise 7-6 : Write a program that takes a series of numbers and counts the
    number of positive and negative values.
    
    */
    #include<stdio.h>
    
    char line[100];	//read line input
    int listNumbers[100];
    int i;
    int main(void)
    {
    	printf("Please enter a list of positive and negative numbers (separated by spaces): ");
    	while((fgets(line, sizeof(line), stdin)) != NULL)
    	{
    	sscanf(line, "%d", &listNumbers[i++]);
    	
    	}
    	
    	return 0;
    }

  6. #6
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    You don't need to bother with fgets unless you really want to.
    Code:
    printf("enter numbers, 0 to stop:\n");
    while( scanf( " %d", &number ) == 1 && number != 0 )
    {
        if less than zero
            increment less than zero counter
        if greater than zero
            increment greater than zero counter
    }
    if number != 0
        you entered something scanf didn't expect which caused it to exit the loop early

    Quzah.
    Hope is the first step on the road to disappointment.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 16
    Last Post: 11-27-2010, 11:44 PM
  2. Using TextOut() to align positive & negative numbers
    By csonx_p in forum Windows Programming
    Replies: 4
    Last Post: 05-27-2008, 07:12 AM
  3. How can i convert negative number to positive number ?
    By winsonlee in forum C Programming
    Replies: 2
    Last Post: 05-05-2004, 08:02 AM
  4. random number between negative and positive number
    By anomaly in forum C++ Programming
    Replies: 6
    Last Post: 12-06-2003, 08:40 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