# Thread: simple problem for ' min & max'

1. ## simple problem for ' min & max'

Hello,

I am a C program beginner. I want to ask how to find out min & max value. Because I tried over a week but still cannot find out what is wrong.
Code:
```#include <.h>
int main(){

int i=0;
float min,max,input;

printf("input number");
scanf("%f",&input);
input=min=max;

do{
printf("input number");
scanf("%f",&input);

if(input< min){
input=min;
}

if(input> max){
input=max;
}
i=i+1;
}while(i<3);

printf("max %f and min %f", max, min);

return 0;
}```
I would like to ask what the problem is, because the results always are 0.
Even I tried to set min =9999 and max = -9999, but the results also are 9999 and -9999.

Thank You 2. Try this way round instead.
min = input; 3. You mean's use 'instead' replaced to 'input=min=max;' ? I tried, but the result is same. 4. What exactly did you try? 5. Originally Posted by kelu You mean's use 'instead' replaced to 'input=min=max;' ? I tried, but the result is same.
Well do you consider assignment to be like
3 = input;

or

input = 3; 6. Hi kelu!

Better you read at first all values in, for example diameters of parts made by a lathe-machine.
Then you search the max value in the array.
Now set min to max.
After this you can find min.

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

int main(){

int i=0;
float min,max, average, range, measurments;

/** first set all elements of the array to zero*/
max = average = range = 0;
for (i = 0; i < 5 ; i++)
measurments[i] = 0;

/**then read in the values */
for (i = 0; i < 5 ; i++)
{
printf("input number");
scanf("%f",&measurments[i]);
}

/** now get max needed to calculate min*/
for (i = 0; i < 5 ; i++)
{
if (measurments[i] > max) max = measurments[i];
average += measurments[i];
}

/** calculate average*/
average /= 5;

/** important: set min to maximum*/
min = max;

/** get min */
for (i = 0; i < 5 ; i++)
{
if (measurments[i] < min) min = measurments[i];
}
/**calculate the range after get min */
range = max - min;

printf("max: %f    min: %f   average: %f   range: %f\n", max, min, average, range);

return 0;
}```
Try and see. 7. 1) Read the first input value.
2) set min and max to the first value.
3) Read in the remaining values and test against min and max, and adjust min and/or max accordingly.
4) Done.

No need to make it complicated. 8. Take a good book for C . . . Max - Min is one of the first exercises for beginners.

Code:
```//Maximum - Minimum

#include <stdio.h>
#include <stdlib.h>

int main(void)
{
int a, b, z;

printf("\nGeben Sie zwei Zahlen ein a <Leertaste> b: ");
scanf("%d %d", &a, &b);

if(a == b)
{
printf("\nBeide Zahlen sind gleich gross!");
exit(0);
}
else
{
//Tenärer Operator: ((a > b) ? a : b)
printf("\nDie groessere Zahl ist %d", z = ((a > b) ? a : b));
}
return(0);
}``` 9. Originally Posted by Kernelpanic Take a good book for C . . . Max - Min is one of the first exercises for beginners.
The code you present (In German, not English as is normally used here.) only determines the max of two values.

The OP's code currently is for 3 numbers, but what if the number of inputs is changed to 10, 20, or more?

Th OP is on the right track, but does need correction, and better indentation. 10. Originally Posted by rstanley The OP's code currently is for 3 numbers, ...
Three numbers? Where?

... but what if the number of inputs is changed to 10, 20, or more?
Can he do, but does not belong to his question. 11. Thanks, Salem, I changed the max=input. It makes the program can be output the max result. And I still hard work to find out the result for min, and others requirements for this program; as max & min just a small part in my assignment. 12. Thanks for your help, As I known this program can be done by <stdio.h> only, also no need to use Array.
I am reading this post every day. And also modifying this program. 13. No offense meant. But in all my books about C is this min-max program gladly with the "tenaeren Operator"; authors like it. Without one or more books about a programming language one can not learn it. Good luck! 14. Originally Posted by Kernelpanic Three numbers? Where?

Can he do, but does not belong to his question.
Code:
```   do{
...
}while(i<3);```
Actually he does input 4 numbers, one before the do while loop, and 3 inside.

A min-max program implies more than 2 numbers. A ternary statement should only be used for testing two numbers. 15. If you don't save the values you've put in, you can only calculate min, max, average, range, not the standard deviation.
If you don't need the standard deviation it is enough to us a code like this.
Code:
```#include <stdio.h>
#include <stdlib.h>

int main(){

int i, n = 3;
float min, max = 0, sum = 0, average = 0, range = 0, measurments;

min = 3.4028234E38;

printf("\nnumber of elements: ");
scanf("%d", &n);

/**then read in the values */
for (i = 0; i < n ; i++)
{
printf("\ninput number %d: ", i);
scanf("%f",&measurments);
sum += measurments;
if (i > 0)
{
average = sum / (i + 1);
range = max - min;
}
if (measurments > max) max = measurments;
if (measurments < min) min = measurments;
printf("max: %f    min: %f   average: %f   range: %f\n", max, min, average, range);
}

return 0;
}```
But it is useful when you compare more random samples by working in quality assurance
that the random samples have the same size(number of elements).

example:
To control a diameter of a bore in a part made by a lathe machine
needs to have the same size of the number of parts (may be 5 or 6 parts)
you control every 2 hours or after produce 200 parts.

to compare the standard deviation or other values of
a random sample witch includes 5 workpieces
a random sample including 12 workpieces
and
a random sample including 8 workpieces

It would be a delusion of wrong statistical variances. Popular pages Recent additions int, max, min, number;, printfinput 