0

I want to calculate second largest number from inputs. So I have written this code. But this code is prints 38 every time I run it no matter what input is. Please help me finding solution to this question. Thanks in advance.

#include<stdio.h>
int main()
{
    int a , b , c , d , max , max2;
    scanf("%d %d %d %d" , &a , &b , &c , &d);
    if (a>b && a>c && a>d)
        max = a;
    else if (b>a && b>c && b>d)
        max = b;
    else if (c>a && c>b && c>d)
        max = c;
    else
        max = d;
    if (max == a)
        if(b>c && b>d)
            b = max2;
        else if(c>c && c>b)
            c = max2;
        else
            d = max2;
    else if(b == max)
        if(a>c && a>d)
            a = max2;
        else if(c>a && c>d)
            c = max2;
        else
            d = max2;
    else if(c == max)
        if(a>d && a>b)
            a = max2;
        else if(b>a && b>d)
            b = max2;
        else
            d = max2;
    else if(d == max)
        if(a>c && a>b)
            a = max2;
        else if(b>a && b>c)
            b = max2;
        else
            c = max2;

    printf("%d" , max2);
}
Jack
  • 23
  • 5
  • 2
    The line `b = max2;` should be `max2 = b;` (and similarly for all the other lines with `max2` in). – Adrian Mole Jan 30 '20 at 09:41
  • 1
    You are printing `max2` but you never set it. – kaylum Jan 30 '20 at 09:41
  • Please enable warnings in your compiler. And if you get warnings, take care about them. – Gerhardh Jan 30 '20 at 09:42
  • Re "*Please enable warnings in your compiler.*", For example, you can use `-Wall -Wextra -pedantic` with `gcc`. – ikegami Jan 30 '20 at 09:46
  • must go through, [What is a debugger and how can it help me diagnose problems?](https://stackoverflow.com/questions/25385173/what-is-a-debugger-and-how-can-it-help-me-diagnose-problems) – TruthSeeker Jan 30 '20 at 09:50

3 Answers3

0

you are trying to copy max2 values in the variables.

copy variable values in max2.

replace your code as below:

int main()
{
    int a , b , c , d , max , max2;
    scanf("%d %d %d %d" , &a , &b , &c , &d);
    if (a>b && a>c && a>d){
        max = a;
    }
    else if (b>a && b>c && b>d){
        max = b;
    }
    else if (c>a && c>b && c>d){
        max = c;
    }
    else{
        max = d;
    }
    if (max == a){
        if(b>c && b>d){
            max2=b;
        }
        else if(c>d && c>b){
            max2=c;
        }
        else{
            max2=d;
        }
    }
    else if(b == max){
        if(a>c && a>d){
            max2=a;
        }
        else if(c>a && c>d){
            max2=c;
        }
        else{
            max2=d;
        }
    }
    else if(c == max){
        if(a>d && a>b){
            max2=a;
        }
        else if(b>a && b>d){
            max2=b;
        }
        else{
            max2=d;
        }
    }
    else if(d == max){
        if(a>c && a>b){
            max2=a;
        }
        else if(b>a && b>c){
            max2=b;
        }
        else{
            max2=c;
        }
    }

    printf("%d" , max2);
}
Nidhi257
  • 754
  • 1
  • 5
  • 23
0

The problem is in code block like these:

else if(c == max){
    if(a>d && a>b){
        a = max2;
    }
    else if(b>a && b>d){
        b = max2;
    }
    else{
        d = max2;
    }
}

Here you're not assigning the content of a, b or d to max2, you're assigning the content of the uninitialized variable max2 to those other variables. Your compiler should at the very least issue a warning here.

a = max2; needs to be max2 = a;. You need to swap around those assignments everywhere where you get a warning about using the uninitialized variable max2.

Blaze
  • 16,736
  • 2
  • 25
  • 44
0

Use functions instead of writing so many repeated code and call to perform the task and highly recommend to learn debugging manually and also using software.

#include<stdio.h>
int maximum (int a,int b,int c){
    if (a>b && a>c )
        return (a);
    else if ( b>c)
        return (b);
    else 
        return (c);
    }
int main()
{
    int a , b , c , d , max , max2;
    scanf("%d %d %d %d" , &a , &b , &c , &d);
    if (a>b && a>c && a>d)
        max = a;
    else if ( b>c && b>d)
        max = b;
    else if  ( c>d )
        max = c;
    else 
        max = d;
// Use functions for repeatative task 
    if (max == a)
        max2 = maximum(b,c,d); 
    else if(b == max)
        max2 = maximum(a,c,d);
    else if(c == max)
        max2 = maximum(b,a,d);
    else if(d == max)
        max2 = maximum(b,c,a);

    printf("%d" , max2);
}