Thursday, October 5, 2017

Second Largest Value

The program must accept a set of N integer values as command line arguments and print the second largest value. If there is no second largest value, print -1 as the output.

Example Input/Output 1:
Input:
20 30 30 20 30 15 12 11 10 6
Output:
20

Example Input/Output 2:
Input:
19 19 19 19 19 19
Output:
-1

Solution:
#include <stdio.h>
#include <stdlib.h>
void main(int c,char *a[])
{
    int i,f=-1,s,n;
    for(i=1;i<c;i++)
     {
         n=atoi(a[i]);
        if(n>f)
        {
            s=f;
            f=n;
        }
        else if(n>s&&n<f)
         s=n;
     }
     printf("%d",s);
}
Share: