# Thread: Averaging two different numbers/loop?

1. ## Averaging two different numbers/loop?

Alright so I have a homework question that I'm a little confused about. I'm pretty new to C/programming in general, so this is probably actually really simple...
I just don't understand how you would have it count how many negatives and positives there are based on the user's input?
If anyone could assist me in any way that would be super great!

Here's the problem and the code it provides:

Modify the program so that is displays the average of the positive and negative numbers. (Hint: be careful not to count the number 0 as a negative number.)

Code:
```#include <stdio.h>
#define MAXNUMS 5
int main()
{
int i;
float number;
float postotal = 0.0f;
float negtotal = 0.0f;

for (i =1; i <= MAXNUMS; i++)
{
printf("Enter a number (positive or negative) : ");
scanf("%f", &number);
if (number > 0)
postotal += number;
else
negtotal += number;
}
printf("\nThe positive total is %f", postotal);
printf("\nThe negative total is %f\n", negtotal);

return 0;
}``` 2. If I were to give you these numbers:
-7, 2, -2, -5, 5, 4, 5, -6, 0, 4

Just by hand, how would you work out what the average of the negative and positive numbers without including 0. 3. I am guessing zero is NOT supposed to count as a negative or positive number from what the OP posted; but, it might be counted as a positive number by many people.
I would suggest asking if it is supposed to be figured in the positive number average.

Tim S. 4. I understand how to average, I just don't understand how to get the amount of negatives/positives the user inputs. If I just divide by the total of 5 that's given in the code already, that wouldn't give me the correct average since there aren't always going to be 5 negatives or 5 positives... my question is how do get that specific number of how many negatives/positives they enter so I can divide by it and get the correct average. 5. my question is how do get that specific number of how many negatives/positives they enter so I can divide by it and get the correct average.
Are you sure you're supposed to find separate averages for positive and negative, and not the average of all values entered?

If so, then just create some variables to act as counters.

Hint: You're already executing separate bits code depending on whether the input is positive or negative. Perhaps these bits of code could be expanded upon. 6. Originally Posted by Xeydrian I understand how to average, I just don't understand how to get the amount of negatives/positives the user inputs.
For this you normally use a counter. Here is some pseudocode

Code:
```nitems = 0
while there is more input:
get an input item
++nitems
printf "The user input %d items", nitems``` 7. I think that's what I needed. I created a few new variables to count that and one to calculate the average itself. My program works properly now! It wasn't averaging correctly when I was messing around with it earlier.

Thanks for the responses everyone and taking the time to try to help me! Popular pages Recent additions 