Skip to main content
Commonmark migration
Source Link

###Strange return value###

Strange return value

Why does find_second() ever return -1? That function should actually find the second value, even if it had to loop three times within the function to do so. It doesn't make sense to call it three times from main().

###Minimal comparisons###

Minimal comparisons

Most of the other answers gave alternate solutions where you had to do four comparisons followed by some math operations. The minimal solution requires only three comparisons, and can sometimes return after two. It also doesn't need any other math operations:

int find_second(int a, int b, int c)
{
    if (a > b) {
        if (c > a)
            return a;
        if (b > c)
            return b;
    } else {
        if (c > b)
            return b;
        if (a > c)
            return a;
    }
    return c;
}

###Strange return value###

Why does find_second() ever return -1? That function should actually find the second value, even if it had to loop three times within the function to do so. It doesn't make sense to call it three times from main().

###Minimal comparisons###

Most of the other answers gave alternate solutions where you had to do four comparisons followed by some math operations. The minimal solution requires only three comparisons, and can sometimes return after two. It also doesn't need any other math operations:

int find_second(int a, int b, int c)
{
    if (a > b) {
        if (c > a)
            return a;
        if (b > c)
            return b;
    } else {
        if (c > b)
            return b;
        if (a > c)
            return a;
    }
    return c;
}

Strange return value

Why does find_second() ever return -1? That function should actually find the second value, even if it had to loop three times within the function to do so. It doesn't make sense to call it three times from main().

Minimal comparisons

Most of the other answers gave alternate solutions where you had to do four comparisons followed by some math operations. The minimal solution requires only three comparisons, and can sometimes return after two. It also doesn't need any other math operations:

int find_second(int a, int b, int c)
{
    if (a > b) {
        if (c > a)
            return a;
        if (b > c)
            return b;
    } else {
        if (c > b)
            return b;
        if (a > c)
            return a;
    }
    return c;
}
added 52 characters in body
Source Link
JS1
  • 28.9k
  • 3
  • 42
  • 83

###Strange return value###

Why does find_second() ever return -1? If you simply replaced that line with return a That function should actually find the second value, even if it had to loop three times within the function would be complete and you wouldn't haveto do so. It doesn't make sense to call it three times from main().

###Minimal comparisons###

Most of the other answers gave alternate solutions where you had to do four comparisons followed by some math operations. The minimal solution requires only three comparisons, and can sometimes return after two. It also doesn't need any other math operations:

int find_second(int a, int b, int c)
{
    if (a > b) {
        if (c > a)
            return a;
        if (b > c)
            return b;
    } else {
        if (c > b)
            return b;
        if (a > c)
            return a;
    }
    return c;
}

###Strange return value###

Why does find_second() ever return -1? If you simply replaced that line with return a, the function would be complete and you wouldn't have to call it three times.

###Minimal comparisons###

Most of the other answers gave alternate solutions where you had to do four comparisons followed by some math operations. The minimal solution requires only three comparisons, and can sometimes return after two. It also doesn't need any other math operations:

int find_second(int a, int b, int c)
{
    if (a > b) {
        if (c > a)
            return a;
        if (b > c)
            return b;
    } else {
        if (c > b)
            return b;
        if (a > c)
            return a;
    }
    return c;
}

###Strange return value###

Why does find_second() ever return -1? That function should actually find the second value, even if it had to loop three times within the function to do so. It doesn't make sense to call it three times from main().

###Minimal comparisons###

Most of the other answers gave alternate solutions where you had to do four comparisons followed by some math operations. The minimal solution requires only three comparisons, and can sometimes return after two. It also doesn't need any other math operations:

int find_second(int a, int b, int c)
{
    if (a > b) {
        if (c > a)
            return a;
        if (b > c)
            return b;
    } else {
        if (c > b)
            return b;
        if (a > c)
            return a;
    }
    return c;
}
Source Link
JS1
  • 28.9k
  • 3
  • 42
  • 83

###Strange return value###

Why does find_second() ever return -1? If you simply replaced that line with return a, the function would be complete and you wouldn't have to call it three times.

###Minimal comparisons###

Most of the other answers gave alternate solutions where you had to do four comparisons followed by some math operations. The minimal solution requires only three comparisons, and can sometimes return after two. It also doesn't need any other math operations:

int find_second(int a, int b, int c)
{
    if (a > b) {
        if (c > a)
            return a;
        if (b > c)
            return b;
    } else {
        if (c > b)
            return b;
        if (a > c)
            return a;
    }
    return c;
}