# Thread: count the number of positive and negative numbers

1. ## 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>

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. 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.

3. Originally Posted by quzah
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. 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.

5. 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>

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. 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.