# Counting

This is a discussion on Counting within the C Programming forums, part of the General Programming Boards category; I am trying to figure out how to count loops to do an average of numbers input. It is a ...

1. ## Counting

I am trying to figure out how to count loops to do an average of numbers input. It is a lab for a college class so I will accept only hints if neccessary. I can use a "while" statement to accept all the inputs the user enters but I cannot seem to figure out how to count how many the user inputs so I can divide by that to get the average of the inputs. Would appreciate any help I can get. Thank you.

2. Originally Posted by rculley1970
I am trying to figure out how to count loops to do an average of numbers input. It is a lab for a college class so I will accept only hints if neccessary. I can use a "while" statement to accept all the inputs the user enters but I cannot seem to figure out how to count how many the user inputs so I can divide by that to get the average of the inputs. Would appreciate any help I can get. Thank you.

Do you have any code so far? If so, post it. While it's for class, we can't 'give' you the answer, like you stated above, but yes we can hint to it.

3. Code:
```int count = 0;
int total = 0;
while(1) {
/* get input from user */
/* add input value to total */
if ( /* user entered a value to indicate no more inputs */ )
break;
count++;
}
/* output the calculated average */```

4. Here is the code that i came up with to input some numbers and then try to average them. The only problem i really have right now is how to count the loops so i can divide it as an average.
Code:
```#include <stdio.h>
main()
{
float n, answer, average = 0, loop = 0, sum = 0.0;
scanf("%f", &n);

while (n >= 0)
{
sum += n;
scanf("%f", &n);
}
printf("%.2f \n", n);
printf("the sum is:  %.2f \n", sum);
average = sum / n;
printf("average is: %.2f \n", average);
return 0;
}```

5. Add an int count variable like I suggested, and put a count++ in your loop to increment it each time they enter a number. Divide sum by count instead of n. (dividing by n in your case is pointless, because it's the last value entered).

6. thank you, let me work on it.

7. Hmm...I'm going to try to say this without making it too obvious so that you can figure out what I mean yourself, but if you need more help figuring out what I mean let me know.

There's a flaw in your programing that has to do with the order that things are done in. Remember that your program will execute starting at the top and going down line by line until it hits that return at the end of the program.

Also, for my own curiosity, I don't understand the need to have that line just after the while loop, in which you print the last number that was obtained...it seems to me like it would make more sense to print each number as it is obtained, so the output of the code is all them numbers that were entered, and then the sum and then the average. If this is the case then you should reconsider where you put that printf statement.

Hope this helps, ask if you need more help.
-Crazed

EDIT: just to let you know I purposly didn't answer the question you posted because the answer posted above is probably the easiest way to do it

8. Thank you CWR, the hint helped and i figured it out. here is the code:
Code:
```#include <stdio.h>
main()
{
float n, average = 0, count = 0, sum = 0.0;
scanf("%f", &n);
while (n >= 0)
{
++count;
sum += n;
scanf("%f", &n);
}
printf("%.2f \n", count);
printf("the sum is:  %.2f \n", sum);
average = sum / count;
printf("average is: %.2f \n", average);
return 0;
}```
Did this change take into your consideration CrazedBrit? I took the loop int out.

I am just trying to take in any number of int or float and display the sum and average of them. This worked so far. too late at night to keep working on it so goodnight and thank you for your help everyone.

9. Ah, you're right. As usual I wasn't thinking clearly Excellent job.