Sagnik
Sagnik

Reputation: 223

2nd largest from 3 numbers, using if_else

I'm kind of new to C programming and I recently bumped into a question of finding the 2nd largest number from 3 numbers. I tried it using if...else, but it is always giving the smallest number as output. What is the logical error I'm making in this code?

#include<stdio.h>

int main() {
    int a;
    int b;
    int c;
    a=10;
    b=30;
    c=7;
    if(a>b) {
        if(a>c) {
            if(b>c)
                printf("2nd largest is %d",c);
            else
                printf("2nd largest is %d",b);
        }
    } else

    {
        if(b>c) {
            if(c>a)
                printf("2nd largest is %d",a);
            else
                printf("2nd largest is %d",c);
        } else
            printf("2nd largest is %d",b);
    }
}

Upvotes: 5

Views: 6873

Answers (5)

deanosaur
deanosaur

Reputation: 611

if(a>=b && a>=c) {   
    if(b>=c) printf("2nd largest is %d",b);
    else printf("2nd largest is %d",c);
} else if(b>=a && b>=c) {
    if(a>c=) printf("2nd largest is %d",a);
    else printf("2nd largest is %d",c);
} else if(c>=a && c>=b) {
    if(a>=b) printf("2nd largest is %d",a);
    else printf("2nd largest is %d",b);
}

or

 static int middleVal(int a, int b, int c) {
    if(a >= b && a >= c) return b > c? b : c;
    return middleVal(b,c,a);
    }

Upvotes: 1

Steve Cox
Steve Cox

Reputation: 2007

There are a lot of ways to do these operations, but if you just had the binary min and max functions at your disposal:

printf("2nd largest is %d", min(min(max(a,b),max(b,c)),max(a,c)));

If you need a more generalized selection (not just the 2nd largest element, but the kth largest element) use a selection algorithm like quickselect: http://en.wikipedia.org/wiki/Quickselect#Algorithm

Upvotes: 0

Frederic Nault
Frederic Nault

Reputation: 996

I would suggest you to use qsort and array to acheive this

there is an example :

#include <stdio.h>
#include <stdlib.h>

// This is a custom function for qsort where we define the sort rules
int cmpfunc (const void * a, const void * b)
{
    return ( *(int*)b - *(int*)a );
}
int main() {
int n;

// Set the number of element
int values[]           = {10, 30, 7};

// Dynamically get the number of element 
int nbOfValue;
nbOfValue = sizeof(values) / sizeof(int);

// Set the number of element to display
int nbOfValueToDisplay = 2;


/*
    Will reorganize you array value 
*/
qsort(values, nbOfValue, sizeof(int), cmpfunc);


for (n = 0 ; n < nbOfValueToDisplay; n++ ) {
  printf("%d ", values[n]);
}

}

Upvotes: 0

femtoRgon
femtoRgon

Reputation: 33341

if(a>b) {
    if(a>c) { //a is the greatest!
        if(b>c) // the *greater* of the two remaining is the second greatest
            printf("2nd largest is %d",c); // if b > c, output b!
        else
            printf("2nd largest is %d",b); // if c > b, output c! 
    } // I think this got missed:  What if c > a and a > b?
} else

{
    if(b>c) { //b is the greatest!
        if(c>a) //As above, the *greater* of the two remaining is what you are looking for
            printf("2nd largest is %d",a); // c is greater, so output it
        else
            printf("2nd largest is %d",c); // a is greater
    } else // c > b and b > a, this is correct.
        printf("2nd largest is %d",b);
}

Upvotes: 2

M Thotiger
M Thotiger

Reputation: 344

Logic is fine but need to swap the printing of the variables as below

1)

        if(b>c)
            printf("2nd largest is %d",b);
        else
            printf("2nd largest is %d",c);

2)

       if(c>a)
            printf("2nd largest is %d",c);
        else
            printf("2nd largest is %d",a);

Upvotes: 1

Related Questions