Skip to main content
added 221 characters in body
Source Link
200_success
  • 145.7k
  • 22
  • 191
  • 481

Your find_second() function is rather weird. It never finds the second-largest number if it is a. It sometimes finds the second-largest number if it is b or c. I don't know if your program works or not — your main() tries to make up for the deficiencies in find_second() by calling it three times — but the function name is a big fat lie.

Special numbers that might also be valid data are dangerous. What if the second-largest number happens to be -1?

For a solution that is easier to verify, I recommend this approach: toss all three numbers into a bag, then exclude the minimum. min() and the maximummax() are trivial to write.

int second_largest(int a, int b, int c) {
    int smallest = min(min(a, b), c);
    int largest = max(max(a, b), c);

    /* Toss all three numbers into a bag, then exclude the
       minimum and the maximum */
    return a ^ b ^ c ^ smallest ^ largest;
}

Your find_second() function is rather weird. It never finds the second-largest number if it is a. It sometimes finds the second-largest number if it is b or c. I don't know if your program works or not — your main() tries to make up for the deficiencies in find_second() by calling it three times — but the function name is a big fat lie.

Special numbers that might also be valid data are dangerous. What if the second-largest number happens to be -1?

For a solution that is easier to verify, I recommend this approach: toss all three numbers into a bag, then exclude the minimum and the maximum.

Your find_second() function is rather weird. It never finds the second-largest number if it is a. It sometimes finds the second-largest number if it is b or c. I don't know if your program works or not — your main() tries to make up for the deficiencies in find_second() by calling it three times — but the function name is a big fat lie.

Special numbers that might also be valid data are dangerous. What if the second-largest number happens to be -1?

For a solution that is easier to verify, I recommend this approach. min() and max() are trivial to write.

int second_largest(int a, int b, int c) {
    int smallest = min(min(a, b), c);
    int largest = max(max(a, b), c);

    /* Toss all three numbers into a bag, then exclude the
       minimum and the maximum */
    return a ^ b ^ c ^ smallest ^ largest;
}
Source Link
200_success
  • 145.7k
  • 22
  • 191
  • 481

Your find_second() function is rather weird. It never finds the second-largest number if it is a. It sometimes finds the second-largest number if it is b or c. I don't know if your program works or not — your main() tries to make up for the deficiencies in find_second() by calling it three times — but the function name is a big fat lie.

Special numbers that might also be valid data are dangerous. What if the second-largest number happens to be -1?

For a solution that is easier to verify, I recommend this approach: toss all three numbers into a bag, then exclude the minimum and the maximum.