- 1
Can someone help me with my homew
I'm using C. There are 3 inputs of integer. And you should print the maximum input without using neither condition, loop, nor array. Someone help, please??
4 Answers
+ 2
So far I am seeing others are using conditions and loop statements. Here is my first attempt to fully meet the requirements. I hope to think of something better, because this fails if any values are zero, and with negative numbers it returns whichever number has the largest absolute value.
Details of how it works are in the comments of the linked source.
int max_of_3(int a, int b, int c) {
   int bc, ca, ab;
   bc = !(a/b) | !(a/c);
   ca = !(b/c) | !(b/a);
   ab = !(c/a) | !(c/b);
   return a*!bc | b*!ca | c*!ab;
}
https://code.sololearn.com/cugW4HH4iou7/?ref=app
+ 2
The requirements for not using condition (conditionals?), loop or array is highly "educative".
I gotta say I admire the task requirements, next time "without any code" might be a great candidate, perhaps ...
+ 1
What a crappy way to ask your students to use the exact thing you want... Have they at least taught ternary syntax?
Is there other ways to do this? Maybe a function that returns the biggest of 2, do that twice, then print?
int maxOf(int a,int b){
while(a>b) return a;
while(b>a) return b;
return a;
}
+ 1
I devised a better way than my first answer. This one works for all integers.
int max_of_3(int a, int b, int c) {
int bc, ca, ab;
//find sign bit to determine if Less-Than-0
const int LT0 = 1<<(8*sizeof (int) - 1);
bc = ((a-b)|(a-c))<0; //is b or c > a?
ca = ((b-c)|(b-a))<0; //is c or a > b?
ab = ((c-a)|(c-b))<0; //is a or b > c?
return a*!bc | b*!ca | c*!ab;
}
Try it out here:
https://code.sololearn.com/cugW4HH4iou7/?ref=app