# Thread: How to correct code to find the largest and smallest #

1. ## How to correct code to find the largest and smallest #

So I'm needing to figure out how to find the largest and smallest number entered by a user. So far I'm able to add, find the average and count how many numbers are entered.
I'm just having trouble with find the max and min. I tried if statements and it breaks the program will it wont let me exit the while loop or the program will do a force close after entering a value.

Code:
```#include <stdio.h>
#include <stdlib.h>

int main(){

int maxNum=0, minNum=0, value, count=0, sum=0;
double average;

printf("To exit the program enter -1\n");
while(value!=-1){
printf("enter a value\n");
scanf("%d", &value);
count++;
sum = sum + value;
}
count= count-1;
sum= sum+1;
average = (double)(sum/count);
printf("number of entries is %d and sum is %d\n", count, sum);
printf("the average is %.2lf\n", average);
printf("min is %d and max is %d", minNum, maxNum);
}``` 2. Show us what you've tried with regard to finding the min/max, and we can try to help you fix it.

"value" is uninitialized the first time you check its value (line 13).

Code:
`average = (double)(sum/count);`
Here, you're casting the result of integer division (i.e. any fractional part has already been truncated before the cast is applied). Just cast one of the variables instead. 3. what do you mean by uninitialized?
The code I posted, works fine and no errors

Here is what I tried with the If statements

Code:
```#include <stdio.h>#include <stdlib.h>

int main(){

int maxNum=0, minNum=0, value, count=0, sum=0;
double average;

printf("To exit the program enter -1\n");
while(value!=-1){
printf("enter a value\n");
scanf("%d", &value);
count++;
sum = sum + value;

if(value > 0){
value = minNum;
value = maxNum;
}
if(value > maxNum)
value = maxNum;
if(value < minNum)
value = minNum;
}
count= count-1;
sum= sum+1;
average = (double)(sum/count);
printf("number of entries is %d and sum is %d\n", count, sum);
printf("the average is %.2lf\n", average);
printf("min is %d and max is %d", minNum, maxNum);
}```
so youre saying I can just make sum as a double and then I wont need to multiply by the "double" 4. Originally Posted by tlxxxsracer what do you mean by uninitialized?
The code I posted, works fine and no errors
Value is not initialized when you check it's value with line 13. You never gave it a value when you declared it (look at line 7). You are relying on undefined behavior. This code is bound to break. Take the advice you are given and you should do it graciously.

Plus you should look at how you are supposed to define main 5. so youre saying I can just make sum as a double and then I wont need to multiply by the "double"
I don't know if you understand what's going on there. There's no "multiply by double" - that is a cast; that is, it converts one type to another.

Code:
`average = (double)(sum/count);`
So what this is doing is:
- dividing "sum" by "count" (both integers, so the result is an integer)
- casting that result to a double

So if you have, for instance, 5/2, the division yields 2, and the cast changes it to 2.0 (which is wrong).

If you cast one of the operands to a double, then the result of the division will be a double.

Code:
`average = (double)sum/count;`
So now, it:
- treats the int "sum" as a double
- divides "sum" (seen as a double) by "count".

Using the same example, you would have 5.0/2, which yields a double result, 2.5

Your min/max logic is pretty close, but not there yet. For starters, you're overwriting "value" with "minVal"/"maxVal". You have the assignments backwards.

There are neater ways of doing it, but you seem to be on the right track. The other problem is that the sentinal value (-1) is included in the logic, which you probably don't want.

You got around this for "count" and "sum" by decrementing/incrementing (respectively) those variables after the loop, but this is a hack that probably won't help much for the min/max portion of the assignment.

If instead you skipped the logic in your loop with an input of -1, then the min/max would work as expected and you wouldn't have to "correct" count and sum after the loop.

Thinking carefully about the logic, and planning it well, can result in a much more elegant solution. Popular pages Recent additions 