1

I've been asked to make a short program that gets 3 numbers, and print the middle one i.e. 3 2 0 -> "middle number is 2" another example : 4 4 5 -> middle number is 4 (the user may enter the same number twice

it would be pretty easy with the if function and so but

  • I'm not allowed to use conditionals or loops

edit: cant use arrays I've tried to divide numbers by each other to get a 0 which indicates one is bigger or smaller

Lostdawn
  • 61
  • 1
  • 4
  • 1
    use an array of size 3 to store the inputs, and print the value of `array[1]` once done. – Sourav Ghosh Nov 05 '20 at 07:48
  • 2
    What have you tried to far? We can't just give you the solution to your homework problem. – Peter Nov 05 '20 at 07:48
  • 6
    Also, does "middle" mean the second one entered or the second smallest/largest? – Peter Nov 05 '20 at 07:49
  • Does this answer your question? [3 numbers in ascending order WITHOUT the use of conditional statements in Java. As in I can't use if statements at all](https://stackoverflow.com/questions/28035107/3-numbers-in-ascending-order-without-the-use-of-conditional-statements-in-java) – vgru Nov 05 '20 at 07:51
  • cant use arrays :/ – Lostdawn Nov 05 '20 at 07:59
  • The edit has a good idea. You can select the larger of two numbers with garbage like this: `max = a * !!(a/b) + b * !!(b/a);` The trick is that `!!` converts 0 to 0, and any non-zero value to 1. – user3386109 Nov 05 '20 at 08:09
  • 1
    middle means by size.. not the order of entering @Peter – Lostdawn Nov 05 '20 at 08:11
  • use this stuff https://stackoverflow.com/a/17449473/7172363 .............. – Алексей Неудачин Nov 05 '20 at 08:12
  • Does 'conditionals' include `?` and `!`? IMO, yes, in which case, you can't even find the smallest (or largest) of two numbers. You're always going to end up with a branch i one way or another. – goodvibration Nov 05 '20 at 09:32
  • Does the solution need to work for all `int`,? What is the allowable input range of the 3 numbers? – chux - Reinstate Monica Nov 05 '20 at 12:31
  • 2
    From your comments, @Lostdawn, there seem to be several more restrictions than you describe in the question. I would suggest editing the full set of requirements into the question, including especially any limitations on which operators you may use, and any requirements for the range of input numbers that must be supported. – John Bollinger Nov 05 '20 at 14:01

4 Answers4

2

As mentioned in the almost duplicate question and in previous answer, one way is to sum the three values and then to substract the min and max values.

However, using min and max function of the library is cheating a little bit, as it is likely that internally conditionals are used.

A way forward is to use the relations:

min(a, b) = (a + b - abs(a-b))/2;
max(a, b) = (a + b + abs(a-b))/2;

etc.

It was mentioned in a comment that using abs is cheating also. In practice, I don't know how it is implemented internally.

One possibility is to used the tricks mentioned in this post: get absolute values....

For example, abs(a) = sqrt (a*a);

Damien
  • 4,809
  • 4
  • 15
  • 20
  • Range limitations: `abs(a-b)` fails when `a-b` overflows or `a-b == INT_MIN`. `sqrt (a*a)` fails when `a > sqrt(INTMAX)`. – chux - Reinstate Monica Nov 05 '20 at 12:25
  • @chux-ReinstateMonica Of course. I don't think it is really important in such a theoretical exercise. The link I mentionned proposes a better solution of `abs`. I wanted to keep a solution as simple as possible – Damien Nov 05 '20 at 12:58
1

Use these two functions:

int maximum(int a, int b, int c)
{
    // initialize max with a
    int max = a;
    
    // set max to b if and only if max is less than b
    (max < b) && (max = b); // these are not conditional statements.
    
    // set max to c if and only if max is less than c
    (max < c) && (max = c); // these are just Boolean expressions.

    return max;
}

int minimum(int a, int b, int c)
{
    // initialize min with a
    int min = a;
    
    // set min to b if and only if min is more than b
    (min > b) && (min = b);
    
    // set min to c if and only if min is more than c
    (min > c) && (min = c);
    
    return min;
}

Then the mid-value can be calculated easily using:

(a+b+c)-minimum(a, b, c)-maximum(a, b, c)

Edit:

To run the code without defining any function, we can use the following. It is exactly same as above, but does all the works inside main function.

int main() {
    int a=3, b=2, c=0, min, max;

    // initialize min with a
    min = a;

    // set min to b if and only if min is more than b
    (min > b) && (min = b);

    // set min to c if and only if min is more than c
    (min > c) && (min = c);

    // similarly find max
    max = a;
    (max < b) && (max = b);
    (max < c) && (max = c);

    printf("%d", a+b+c-min-max);
    return 0;
}
Soham Saha
  • 53
  • 6
  • is there a way to find min max inside the main program? we werent taught to do side-programs and i rather not do that – Lostdawn Nov 05 '20 at 09:09
  • 1
    The question explicitly states 'no conditionals', while in your answer - `(max < b) && (max = b)` - there are 3 logical conditions (assuming you meant `==` there, otherwise this code won't even work). – goodvibration Nov 05 '20 at 09:35
  • <, >, ==, != these are called 'relational' operators. By 'no conditionals', I think it meant that, no if-else, and no `condition?statement1:statement2;` type statement. I have checked that the code works fine. The trick is, if the statement in the left of `&&` returns 1(true), only then the statement in the right gets executed. – Soham Saha Nov 05 '20 at 10:46
  • 1
    @SohamSaha, although `<` and `>` are relational operators, the sticking point with this is `&&`. The standard calls it a "logical operator", not a "conditional operator", but using it as a substitute for an `if` statement seems to violate at least the spirit of the prohibition against conditional statements. That may nevertheless be exactly what is expected, but there are other approaches that are more clearly within the rules (as I understand them). – John Bollinger Nov 05 '20 at 13:47
1

Use wider than int math to handle range limitations.

Find absolute value by division trick.1

((3 * d) / (3 * d + 1)) -->  0 when d >= 0
((3 * d) / (3 * d + 1)) --> -1 when d < 0

No conditions used.

long long my_abs_diff(long long a, long long b) {
  long long d = 0LL + a - b;
  return d * (1 - 2 * ((3 * d) / (3 * d + 1)));
}

long long my_min(long long a, long long b) {
  return ((a + b) - my_abs_diff(a, b)) / 2;
}

long long my_max(long long a, long long b) {
  return ((a + b) + my_abs_diff(a, b)) / 2;
}

int median3(int a, int b, int c) {
  long long mn = my_min(my_min(a, b), c);
  long long mx = my_max(my_max(a, b), c);
  return (int) (-mn - mx + a + b + c);
}

Simplification may exist.


1In C, since C99, integer division is defined as truncating towards zero. It is this directed-ness towards zero code is exploiting to effect an "if". A cost to this approach is that we need a few more bits of integer range. For pre-C99, code can use div() to insure the desired quotient truncation.

Tested successfully with many edge cases and 10,000,00,00 random combinations over entire int range: [INT_MIN ... INT_MAX].

chux - Reinstate Monica
  • 143,097
  • 13
  • 135
  • 256
0

Hint : first find the min and max values by using std::min and std::max
Then sum all the numbers and subtract the min and max values. What you are left with is the middle value.

Barak Friedman
  • 279
  • 1
  • 2