Home > Mobile >  Sorting 3 variables in C just does not work
Sorting 3 variables in C just does not work

Time:10-04

#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

CodePudding user response:

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

CodePudding user response:

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;
}

CodePudding user response:

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

  •  Tags:  
  • c
  • Related