# Thread: simple problem for ' min & max'

1. @rusyoldguy

The OP didn't ask about "Standard Deviation". The OP is a beginner, and is learning C, programming a simple program.

As for your more advanced program, first I would never assume a fixed number of entries. What if redirecting input from a file with unknown number of values? For the OP's simple program, setting the number of values to be entered is fine.

And, secondly, I would never set min to some predefined high value! However if you do, please use the predefined value for the compiler in float.h. "FLT_MIN" or "FLT_MAX"

min = max = first input number!!!

2. Thanks for rstanley explained this is looping program. And thanks rusyoldguy told me the min value. And thanks all.
Finally, I am trying to write "how many times of inputs are better than twice inputs for the last time".

Actually, this looping program can be for the user choose their enter times, as entering 10 times, then this program will get 10 inputs, then calculate the min, max, average...etc. Then find out the"how many times of inputs are better than twice inputs for the last time" in 10 times.

3. Originally Posted by rstanley
A min-max program implies more than 2 numbers. A ternary statement should only be used for testing two numbers.
Three variables I think is also ok, and it work too, but with four I have a problem. This is just an interesting exercise, of course. Three was easy, but four? - Somewhere exist an error in reasoning.

Maybe I should open a new thread?

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

int main(void)
{
int i=0;
int min, max, max2, zahl;
int number_comp[4];
int *zgr = &number_comp[0];

for(int j = 0; j < 4; j++)
{
printf("Input number: ");
scanf("%d", &zahl);
number_comp[j] = zahl;
}

max = (zgr[0] > zgr[1] ? ((zgr[0] > zgr[2]) ? zgr[0] : zgr[2]) : zgr[1]);
printf("Von 3 Zahlen die groesste: %d\n\n", max);

//Runs not always correct
max2 = (zgr[0] > zgr[1] ? ((zgr[0] > zgr[2]) ? ((zgr[0] > zgr[3]) ? zgr[0] : zgr[3]) : zgr[1]) : zgr[3]);
printf("Von 4 Zahlen die groesste: %d\n", max2);

return(0);
}```

4. Max of 4 using the conditional operator.
Code:
```#include <stdio.h>

int main() {
int a, b, c, d;
scanf("%d%d%d%d", &a, &b, &c, &d);
int m = a >= b
? a >= c
? a >= d ? a : d
: c >= d ? c : d
: b >= c
? b >= d ? b : d
: c >= d ? c : d;
printf("%d\n", m);
}```

5. Originally Posted by Kernelpanic
Three variables I think is also ok, and it work too, but with four I have a problem. This is just an interesting exercise, of course. Three was easy, but four? - Somewhere exist an error in reasoning.

Maybe I should open a new thread?
...
I think that would be a good idea, as this goes beyond the OP's original question.

6. @Kernelpanic and @john.c

The use of nested ternary operators is not recommended, ESPECIALLY, more than two.

A min - max algorithm is much better for the programmer, and maintainer, let alone for a less experienced programmer. Plus, it makes for a generic function where you can send in an array of numbers of various lengths, to be added to your personal or company library.

7. Last post for this "just for fun"-problem. Thanks John.C now it runs correct; with minimum too.

Three numbers is really maximum more are nonsense.
@rstanley, for a practical application, a properly program for any numbers is better, of course.

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

int main(void)
{
//int i=0;
int min, min2, max, max2, max3, zahl;
int number_comp[4];
int *zgr = &number_comp[0];

for(int j = 0; j < 4; j++)
{
printf("Input number: ");
scanf("%d", &zahl);
number_comp[j] = zahl;
}

max = (zgr[0] > zgr[1] ? ((zgr[0] > zgr[2]) ? zgr[0] : zgr[2]) : zgr[1]);
printf("Von 3 Zahlen die groesste: %d\n\n", max);

max2 = zgr[0] >= zgr[1]
? zgr[0] >= zgr[2]
? zgr[0] >= zgr[3] ? zgr[0] : zgr[3]
: zgr[2] >= zgr[3] ? zgr[2] : zgr[3]
: zgr[1] >= zgr[2]
? zgr[1] >= zgr[3] ? zgr[1] : zgr[3]
: zgr[2] >= zgr[3] ? zgr[2] : zgr[3];
printf("Von 4 Zahlen die groesste: %d\n\n", max2);

max3 = zgr[0] >= zgr[1] ? zgr[0] >= zgr[2] ? zgr[0] >= zgr[3] ? zgr[0] : zgr[3]    : zgr[2] >= zgr[3] ? zgr[2] : zgr[3] : zgr[1] >= zgr[2] ? zgr[1] >= zgr[3] ? zgr[1] : zgr[3] : zgr[2] >= zgr[3] ? zgr[2] : zgr[3];
printf("Von 4 Zahlen die groesste: %d\n\n", max3);

min2 = zgr[0] <= zgr[1] ? zgr[0] <= zgr[2] ? zgr[0] <= zgr[3] ? zgr[0] : zgr[3]    : zgr[2] <= zgr[3] ? zgr[2] : zgr[3] : zgr[1] <= zgr[2] ? zgr[1] <= zgr[3] ? zgr[1] : zgr[3] : zgr[2] <= zgr[3] ? zgr[2] : zgr[3];
printf("Von 4 Zahlen die kleinste: %d\n\n", min2);

return(0);}```

8. Max-Min with an array for any numbers.

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

//Quantity
#define ANZAHL 5

int maximum_aus_feld(int *eingabe, int groesse);
int minimum_aus_feld(int *eingabe, int groesse);

int main(void)
{
int zahl;
int number_comp[ANZAHL];
int *zgr = &number_comp[0];

for(int j = 0; j < ANZAHL; j++)
{
printf("Input number: ");
scanf("%d", &zahl);
number_comp[j] = zahl;
}
printf("Maximum = %d -- Minimum = %d\n", maximum_aus_feld(zgr, ANZAHL),
minimum_aus_feld(zgr, ANZAHL));

return(0);
}

int maximum_aus_feld(int *eingabe, int groesse)
{
//Auf Anfang setzen - Put to the beginning
int maximum = eingabe[0], i;
for(i = 1; i < groesse; i++)
{
//If "eingabe[]"(Input) greater than maximum is eingabe[]
if(eingabe[i] > maximum)
{
maximum = eingabe[i];
}
}
return maximum;
}

int minimum_aus_feld(int *eingabe, int groesse)
{
int minimum = eingabe[0], i;
for(i = 1; i < groesse; i++)
{
//The same like maximum only "<"
if(eingabe[i] < minimum)
{
minimum = eingabe[i];
}
}
return minimum;
}```