# Thread: Finding simple min, max, mean in C

1. ## Finding simple min, max, mean in C

Hey I seem to be gotten stuck on a simple C problem. The program is supposed to ask user for a number, till EOF and then print out the highest, lowest and average statistic.
I have something like this in the code:
Buf is a char, and all this is inside a loop:
Code:
```do {
input=atof(gets(buf));
max=min=sum=input;
count++;
if (input <= min){
input=min;}
if (input >= max){
input=max;}
sum = sum + input;

}
while (strlen(buf) > 0);```
Then i just print out the average(sum/count) and max and min.

So if anybody can point me to a valid (but simple) solution that would be appreciated.

Thanks..
R.

2. S'pose we'd better get the basics out of the way first
<< !! Posting Code? Read this First !! >>

And never use gets()
http://faq.cprogramming.com/cgi-bin/...&id=1043284351

I'm guessing there are 3 more lines in your program, so why not post all of them rather than leaving us to guess whether buf is a char* or not ?

3. When I want the min or max of a series of items, I initialize them before the loop to "impossibly high" for min, and "impossibly low" for max:
Code:
```min = 32356;
max = -32356;
sum = 0;

/ * Now do the do: */

do  {
get input
/* you need to count the number of items input, so you can divide and get an average */
numItems++
if min > input
min = input

if max < input
max = input

/* and sum is never assigned to any input. It's like this: */
sum += input
print min, max, sum/numItems

if input == trigger  /* in your case, EOF */
break
}```

4. >>When I want the min or max of a series of items, I initialize them before the loop to "impossibly high" for min, and "impossibly low" for max:

I disagree with that logic - never assume that anything is impossibly large/small. I prefer to store the first value in the max (or min) variable, and compare all subsequent values to that value - if a value is bigger, it's the new max.

I agree with Salem - post your whole code.

5. I disagree with that logic - never assume that anything is impossibly large/small. I prefer to store the first value in the max (or min) variable
Same here.

6. A counter point would be that it's actually easier to write the code if you initially set the max to the variable's lower bound, and the minimum to the variable's upper bound. You don't have to "special case" for the first read.

Quzah.

7. Originally Posted by quzah
A counter point would be that it's actually easier to write the code if you initially set the max to the variable's lower bound, and the minimum to the variable's upper bound. You don't have to "special case" for the first read.

Quzah.

8. ## followup

Hey guys, thanks for your help. Unfortunately I'm still having trouble with the code. It only gives me the max, the min stays zero.
Here's the complete code:
Code:
```
#include <stdio.h>
#include <math.h>
#include <stdlib.h>

char buf[BUFSIZ + 1];
int count=0;
float input,sum;
min=32356;
max=-32356;

int main()
{
do {
printf("enter");
input=atof(gets(buf));

count++;

if (min > input)
min=input;
if (max < input)
max=input;

sum +=input;

//diagnostics
printf("input%i\n",input);
printf("count%i\n",count);
printf("max%i\n",max);
printf("min%i\n",min);
//
}
while (strlen(buf) > 0);

printf("\ncount %i\n",count-1);
printf(" min %i\n",min);
printf("  max %i\n",max);
printf(" ave %f\n",(sum/(count-1)));

return (0);
}```
The printf tells me that while it's in the loop the min max and ave are all right, but when it finishes, the min is allways 0.

Here's the output:
enter 2
input 0
count 1
max 2
min 2

enter 5
input 0
count 2
max 5
min 2

enter 2
input 0
count 3
max 5
min 2

enter
input0
count4
max5
min0

entered 3
low 0
high 5
average 3.000000

At this point the low should be 2,
I'msure i'm making some stupid little mistake somewhere, it's just I don't know where..

9. >>I'msure i'm making some stupid little mistake somewhere, it's just I don't know where..

you're making more than one little stupid mistake...

Code:
```char buf[BUFSIZ + 1];
int count=0;
float input,sum;
min=32356; /*missing type specifiers here*/
max=-32356;```
Judging by the printfs later, they're supposed to be ints.

next, you were told about gets already, by Salem no less. Don't use it. Ever. Full Stop.

atof returns double, not float (deceptively). In fact, there's hardly any reason to use float variables, doubles are better.

Code:
```	if (min > input)
min=input;
if (max < input)
max=input;

sum +=input;

//diagnostics
printf("input%i\n",input);```
Now in these lines, input is float, min and max are int, so you are casting - not good. Also, sum is not initialised here, and you should use %f for printing the input variable.

Code:
`while (strlen(buf) > 0);`
Need to include string.h to use strlen.

Lastly, to answer your question, you press enter to break your loop - the loop runs all the way through when you press enter (doesn't quit immediately). The result is that atof is called on an invalid string, and is returning 0 on error, which is less than any of your test values (you never tried a negative value) - so it overwrites your value.

To fix it - I'd redesign your program quite a bit - I'd use fgets to read input, then use something like strtol (or strtod if you insist of floating point), to break the loop I'd try something like typing q (in which case you'd use an if statement to check the value entered with strcmp, and if the value entered was q, break).

10. 1. #include <limits.h> and initialize "min" and "max" to INT_MIN and INT_MAX respectively, becuase you'll break on moderately pathological inputs.
2. GET THE HELL RID OF gets() ALREADY!
3. That code won't even compile. "min" and "max" aren't declared and you use strlen without <string.h>

Edit: Whoops, slow internet connection.

11. >>1. #include <limits.h> and initialize "min" and "max" to INT_MIN and INT_MAX respectively,

other way around?

12. Originally Posted by Richie T
>>1. #include <limits.h> and initialize "min" and "max" to INT_MIN and INT_MAX respectively,

other way around?
Um, yes. =D

13. Thanks guys. I'll try your suggestions and see what happens.

14. Originally Posted by Richie T
>>

Lastly, to answer your question, you press enter to break your loop - the loop runs all the way through when you press enter (doesn't quit immediately). The result is that atof is called on an invalid string, and is returning 0 on error, which is less than any of your test values (you never tried a negative value) - so it overwrites your value.

To fix it - I'd redesign your program quite a bit - I'd use fgets to read input, then use something like strtol (or strtod if you insist of floating point), to break the loop I'd try something like typing q (in which case you'd use an if statement to check the value entered with strcmp, and if the value entered was q, break).

Well again thank you guys for the answers and all these suggestions, I did manage to get the code to work (just didn't think throughly enough).