Electronics > Beginners
C Program to find maximum between three number using nested if
grumpydoc:
--- Quote from: jesuscf on January 30, 2020, 06:30:47 pm ---max=num1;
if(num2>max) max=num2;
if(num3>max) max=num3;
--- End quote ---
Arguably the best solution because it is clear and readily understandable and also lends itself to the next (and more realistic) problem - find the maximum out of an arbitrary number of items - the most obvious solution being to set max to the first element of an array then run along the array elements updating max if the current element is larger.
But, yeah, like most C programmers I'd define a MAX macro (or use a handy one if I pulled it in with library headers), or inline function and say something like
--- Code: --- m = max(a, max(b,c))
--- End code ---
--- Code: ---int max(int a, int b) {
return (a > b) ? a : b;
}
--- End code ---
If you want, but:
--- Code: ---int max(int x, int y)
{
return x ^ ((x ^ y) & -(x < y));
}
--- End code ---
Is more fun.
Proof this works left as an exercise for the reader.
helius:
--- Quote from: tggzzz on January 30, 2020, 10:51:19 pm ---(and I met him once and thanked him for his semonal Algol-60 compiler :) )
--- End quote ---
I only discovered a few years ago that he also invented Quicksort. Going back to the paper (in a predecessor to ACM Letters, that didn't even exist at the time) it's amazing how opaque it looks with the terminology from that era. Can you imagine describing Quicksort without mentioning a stack or a procedure call? So much of the work of computer science is coming up with concepts (abstractions) that are both intuitive and rigorous.
--- Quote from: tggzzz ---Machine language can be elegant or obfuscated, just like C.
--- End quote ---
I'll admit that I believe this too, but only if we mean something like the order code of a stack machine, where the source and destination are implicit. Instructions on register machines are full of irrelevant detail that imposes a heavy cognitive load, and bizarro 8086 is even harder.
IDEngineer:
This is definitely the most readable:
--- Quote from: jesuscf on January 30, 2020, 06:30:47 pm ---max=num1;
if(num2>max) max=num2;
if(num3>max) max=num3;
--- End quote ---
...but I must admit I got all tingly when I saw oPossum's offering:
--- Quote ---printf("Maximum among all three numbers = %d", (num1 < num2) ? ((num2 < num3) ? num3 : num2) : ((num1 < num3) ? num3 : num1));
--- End quote ---
It might not be as immediately obvious, but it's darned elegant. My OCD loves it. Think about its extensibility....
ve7xen:
--- Quote from: brucehoult on January 30, 2020, 09:54:11 pm ---
--- Quote from: ve7xen on January 30, 2020, 09:35:56 pm ---Personally I would prefer to compose this from the standard two-input max function `int max(int a, int b)`.
So:
--- Code: ---int max(int a, int b) {
return (a > b) ? a : b;
}
int main(void) {
...
int max3 = max(num1, max(num2, num3));
...
}
--- End code ---
Or use C++ and the std::min and std::max library functions.
--- End quote ---
This is going in 180 degrees the wrong direction for a beginner. They need concrete not abstract. They need it to be completely obvious what you can say (and what it means) and what you *can't* say.
You give a toddler Lego to build things from, not a NC milling machine.
--- End quote ---
I don't think it is ever too early to demonstrate idiomatic solutions, if they are simple to understand. It wouldn't be the first solution I'd offer to someone with this question, but the trivial examples were covered and I think it is worth showing other, more 'correct' ways of doing things. It is unfortunate that C doesn't define a max() in the library, but even handwaving away the function definition, understanding how to compose functions is simple, idiomatic, and essential to using the language. When I was going through a computer science education, I was constantly frustrated with how many times I was told 'you know what we said last week? that no longer applies, now we're going to do it this better way', and likewise it was always the hints of sweeter abstractions to come that kept me interested in the early classes.
Perhaps this is why I'm not a teacher.
--- Quote ---Is more fun.
--- End quote ---
Cute :-DD . Interestingly many compilers seem to generate equivalent assembly for these two options. I never would've thought the compiler would consider to simplify several bitwise operations into a cmp.
unitedatoms:
--- Code: ---x=(a>b)?((a>c)?a:c):((b>c)?b:c);
--- End code ---
Navigation
[0] Message Index
[#] Next page
[*] Previous page
Go to full version