You see, I\'ve self-taught myself C++ (not completely, I\'m still procrastinating -_-). So, now I started university and they\'re teaching C and they made us do a program of
One idea may be to compute the maximum and minimum of the first two numbers. Then, you compare the rest of the numbers in pairs. The greater one of each pair is compared against the current maximum, and the smaller one of each pair is compared against the current minimum. This way you do 3 comparisons for every 2 elements, which is slightly more efficient than Arpit's answer (2 comparisons for each element).
In code:
#include
int main(int argc, char **argv) {
int a, b, c, d;
printf("Enter four integers (separated by space): ");
scanf("%d %d %d %d", &a, &b, &c, &d);
int max, min;
if (a > b) {
max = a;
min = b;
}
else {
max = b;
min = a;
}
if (c > d) {
if (c > max) {
max = c;
}
if (d < min) {
min = d;
}
}
else {
if (d > max) {
max = d;
}
if (c < min) {
min = c;
}
}
printf("max = %d, min = %d\n", max, min);
return 0;
}