0
#include <stdio.h>

int main(void) {
    int a = 0, b = 0, c = 0, 
    total = 0, helper = 0, helper_2 = 0, helper_3 = 0;

    scanf("%d%d%d%d", &total, &a, &b, &c);

    helper   = (a <= b && a <= c ? a : (b <= c ? b : c));
    helper_2 = (c >= b && a >= c ? c : (b >= a && a >= c ? a : (c >= b && b >= a ? b : 0)));
    helper_3 = (a >= b && a >= c ? a : (b >= c ? b : c));

    if ((total < 0) || (total > 1000) || (helper <= 0) || 
        (helper_2 <= 0) || (helper_3 <= 0) || (helper > 1000) || 
        (helper_2 > 1000) || (helper_3 > 1000)) {
        printf("0");
        return 0;
    }
    else if ((helper + helper_2 + helper_3) <= total) {
        printf("3");
    }
    else if (helper + helper_2 <= total) {
        printf("2");
    }
    else if (helper <= total) {
        printf("1");
    }
    else {
        printf("0");
    }

    return 0;
}

That is the code. I sort three variables, and store them in 3 different variables according to their size, the small one goes to the first one and so on... The thing is, all the variables MUST be between 0 and 1001. It seems to work well, but it does not. It has errors but I cannot find them. I just would like to know if some of you guys can help me to improve this code or even tell me what inputs to use that would return an unexpected value. Thanks so much:D

Edit: I fixed part of them by removing the 0 from the helper_2 expression:D it would return 0 if the input would be for ex: 6, 1, 3, 2.


EDIT2: I've already answered it how you solve using ternary, but the other answers are so much better than the one I was using. So, thank you so much for everyone who helped me:D

  • 1
    Print out the three sorted variables, then try all the different input permutations. There are only six ways the numbers can be arranged: `1 2 3`, `1 3 2`, `2 1 3`, `2 3 1`, `3 1 2`, `3 2 1`. Try all six and see which ones it gets wrong. – John Kugelman Oct 03 '21 at 05:53
  • @JohnKugelman, I do test all of them, they all work, but I'm missing something and can't find what. – Game changer Oct 03 '21 at 05:59
  • How do you know it doesn't work? – Support Ukraine Oct 03 '21 at 06:04
  • @4386427, it's an old question from a competition, the website that provides it has also a "judge", it shows me it has erros based on the various tests it does, but it doesn't tell what are they. – Game changer Oct 03 '21 at 06:09
  • @ClébsonSouza Do you have a link? – Support Ukraine Oct 03 '21 at 06:09
  • Why not just write functions for `minof3`, `midof3` and `maxof3` -- then you can access each sorted value directly. [old example - min, mid, max of 3](https://stackoverflow.com/a/39198107/3422102) – David C. Rankin Oct 03 '21 at 06:16
  • @4386427 https://www.thehuxley.com/problem/2910?quizId=6935 . There we go:) it's in portuguese, and I guess you'll have to sign in to access. – Game changer Oct 03 '21 at 06:19
  • @DavidC.Rankin I could only use if else or ternary operator – Game changer Oct 03 '21 at 06:21
  • 1
    It would help while debugging code, and later, when revisiting, if you used clear naming conventions. Instead of `helper`, use `min_abc` for example. And comment each section with expected inputs, outputs, and what processing it is expected to do. – hellork Oct 03 '21 at 06:26
  • @hellork thanks for the hint:D i'll try it – Game changer Oct 03 '21 at 06:33
  • @ClébsonSouza - updated to only use *ternary* in the function. If you can't use a function, you can just move the code into `main()` in one block. Either way -- food for thought. The functions avoid having to nest ternaries -- which rapidly become difficult to digest. – David C. Rankin Oct 03 '21 at 06:37
  • the posted code fails to properly calculate `helper_2` with an input of 100 3 2 1 Please correct such logic errors – user3629249 Oct 03 '21 at 06:48

3 Answers3

1

The expression:

helper_2 = (c >= b && a >= c ? c : (b >= a && a >= c ? a : (c >= b && b >= a ? b : 0)));
                                                                                

is wrong. The last part can return zero! That's a bug. When we know that a, b and c are all greater than zero, the result just can't be zero.

Try input a=1, b=3, c=2 and print the value of the helper variables.

int a = 1;
int b = 3;
int c = 2;
    
int helper_1   = (a <= b && a <= c ? a : (b <= c ? b : c));
int helper_2 = (c >= b && a >= c ? c : (b >= a && a >= c ? a : (c >= b && b >= a ? b : 0)));
int helper_3 = (a >= b && a >= c ? a : (b >= c ? b : c));
    
printf("%d %d %d --> %d %d %d\n", a, b, c, helper_1, helper_2, helper_3);

Output:

1 3 2 --> 1 0 3

which is obviously wrong

My advice is to avoid the complex ternary conditionals. Write simple if statements instead.

For instance:

if (a > b)
{
    // swap a and b
}
if (a > c)
{
    // swap a and c
}
if (b > c)
{
    // swap b and c
}
// Now a, b and c is sorted with a being smallest
Support Ukraine
  • 42,271
  • 4
  • 38
  • 63
1

You could really use a few helper functions to clean up your code.

One function to "swap" a pair of variables if the first is greater than the second:

void Sort2(int* x, int* y) {
   if (*x > *y) {
       int tmp = *x;
       *x = *y;
       *y = tmp;
   }
}

Another to validate a range:

int isInRange(int minimum, int maximum, int value) {
    return ((minimum <= value) && (value <= maximum));
}

Then your code gets really simple:

int main(void) {
    int a = 0, b = 0, c = 0, 
    total = 0;

    scanf("%d%d%d%d", &total, &a, &b, &c);

    Sort2(&a, &b);
    Sort2(&a, &c);
    Sort2(&b, &c);

    // a,b, and c are in sorted order

    if !(isInRange(0, 1000, total) && isInRange(1, 1000, a) && isInRange(1, 1000, b) && isInRange(1, 1000, c))
    {
        printf("0");
        return 0;
    }

    if ((a + b + c) <= total) {
        printf("3");
    }
    else if (a + b <= total) {
        printf("2");
    }
    else if (a <= total) {
        printf("1");
    }
    else {
        printf("0");
    }

    return 0;
}
selbie
  • 100,020
  • 15
  • 103
  • 173
0

Just remove helper_2 condition and use:

a>b?  ( c>a? a : (b>c? b:c) )  :  ( c>b? b : (a>c? a:c) )  )

instead. :D

  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Oct 03 '21 at 07:31
  • @4386427 may I ask why? I just did so because I personally thought that a small code would be better than a lot of if & else. – Game changer Oct 03 '21 at 17:27