FIND OUT SECOND LARGEST NUMBER IN AN UNSORTED ARRAY USING C PROGRAM





C program to find the second largest element in an array



#include<stdio.h>
int main(){
  int a[50],size,i,j=0,big,secondbig;
  printf("Enter the size of the array: ");
  scanf("%d",&size);
  printf("Enter %d elements in to the array: ", size);
  for(i=0;i<size;i++)
      scanf("%d",&a[i]);
 
  big=a[0];
  for(i=1;i<size;i++){
      if(big<a[i]){
           big=a[i];
           j = i;
      }
  }

  secondbig=a[size-j-1];
  for(i=1;i<size;i++){
      if(secondbig <a[i] && j != i)
          secondbig =a[i];
  }
  
  printf("Second biggest: %d", secondbig);
  return 0;
}

Sample output:
Enter the size of the array: 5
Enter 5 elements in to the array: 5 3 2 1 0
Second biggest: 3





48 comments:

  1. Fantastic site!!
    Thank you very much...
    Subha

    ReplyDelete
  2. hey Jason, here is the code:
    if(a[i]>max)
    {
    max2=max;
    max=a[i]}
    }
    else if(a[i]>max2)
    {
    max2=a[i];
    }

    ReplyDelete
    Replies
    1. initial value of max and max 2 is a[0]. isnt it?

      Delete
  3. i fixed the code.

    if(a[i]>max)
    {
    max2=max;
    max=a[i]}
    }
    else if(a[i]>max2&&a[i]!=max)
    {
    max2=a[i];
    }

    and here's how you find the third and the fourth largest numbers.

    min=num[0];
    for(i=1;inum[i])
    min=num[i];
    }
    max=min;
    max2=min;
    max3=min;
    max4=min;
    for(i=0;imax)
    {
    max4=max3;
    max3=max2;
    max2=max;
    max=num[i];
    }
    else if(num[i]>max2&&num[i]!=max)
    max2=num[i];
    else if(num[i]>max3&&num[i]!=max2&&num[i]!=max)
    max3=num[i];
    else if(num[i]>max4&&num[i]!=max3&&num[i]!=max2&&num[i]!=max)
    max4=num[i];
    }

    ReplyDelete
    Replies
    1. your logic of finding 2nd largest no. is not always right.
      here if a[0] is bigest then it will not work

      Delete
  4. Paul SIIT 207/13/11, 12:12 AM

    Woops, got some typos there.

    min=num[0];
    for(i=1;inum[i])
    min=num[i];
    }

    ReplyDelete
  5. Paul SIIT 207/13/11, 12:16 AM

    lol something's wrong here, but you get the concept.

    ReplyDelete
  6. Kindly answer this prob...if i want to find out the position of the largest number in an array than how to find it???..thnx in advance =)

    ReplyDelete
  7. To find out the largest element array. I think following link will help you.

    http://cquestionbank.blogspot.com/2008/01/write-c-program-to-find-largest-number.html

    ReplyDelete
  8. to find nthbiggest... pseudo code...
    correct me i it has errors..

    static int k = 0; // golbal

    void main()
    {
    int i, j,size,n,nthbig;
    int arr[];
    \\enter nth heighest...

    for(i=1;i<=n;i++)
    nthbig=selectbig();
    }



    int selectbig()
    {
    int temp;

    big=a[0];
    for(i=k;i<size;i++)
    {
    if(big<a[i])
    big=a[i];
    }

    temp = a[i];
    a[i]=a[k];
    a[k]=temp;
    k++;

    return big;
    }

    ReplyDelete
  9. but how do we create the algorithm

    ReplyDelete
  10. very good

    ReplyDelete
  11. Write a program that accepts five numbers from the user and displays the highest and lowest number. Assume that there are no duplicate values.

    ReplyDelete
  12. Use the technique of bubble sorting
    for(i=0;i<=1;i++){
    for(j=0;ja[j+1]){
    temp=a[j];
    a[j]=a[j+1];
    a[j+1]=temp;
    }
    }
    }
    printf("%d",a[n-1]);
    }

    ReplyDelete
  13. with respect to the code for finding out the second highest number, correct output is not being given for the following set of inputs:
    4,4,3..Please help!

    ReplyDelete
  14. in this program secondbig=a[size-j] not a[size-j-1]....tyhen this works fine for the input 1 5 2 3 4

    ReplyDelete
  15. in this program secondbig=a[size-j] not a[size-j-1]....tyhen this works fine for the input 1 5 2 3 4

    ReplyDelete
  16. guys how to do it without sorting the array

    ReplyDelete
  17. Hi think this program is wrong ..pls verify ....

    ReplyDelete
  18. This comment has been removed by the author.

    ReplyDelete
  19. hey bro, go'g to do Mini project on this sem ,am do'g mca , plz suggest a live project . . .its instance.

    ReplyDelete
  20. sec_big=arr[0];
    for(i=0;isec_big)&&(sec_big<big))
    {
    sec_big=arr[i];
    }
    }

    ReplyDelete
  21. This comment has been removed by the author.

    ReplyDelete
  22. This comment has been removed by the author.

    ReplyDelete
  23. #include

    main()
    {
    int a[] = {9000, 90, 6, 20, 54, 1000, 23, 58,12, 500};
    int i, fl, sl;

    fl =a[0]; sl = a[0];

    for(i = 1; i < 10; i++)
    {
    if(fl < a[i])
    {
    sl = fl;
    fl = a[i];

    }
    else if (sl < a[i])
    sl = a[i];
    else if (sl == fl && sl > a[i])
    sl = a[i];
    }

    printf("FL = %d \n SL = %d", fl, sl);

    }

    ReplyDelete
  24. #include

    main()
    {
    int a[] = {9000, 90, 6, 20, 54, 1000, 23, 58,12, 500};
    int i, fl, sl;

    fl =a[0]; sl = a[0];

    for(i = 1; i < 10; i++)
    {
    if(fl < a[i])
    {
    sl = fl;
    fl = a[i];

    }
    else if (sl < a[i])
    sl = a[i];
    else if (sl == fl && sl > a[i])
    sl = a[i];
    }

    printf("FL = %d \n SL = %d", fl, sl);

    }

    reach me for queries at raavi.kishore@gmail.com

    ReplyDelete
  25. it is nice

    ReplyDelete
  26. please help me out to correct the programme
    here the coding
    // Program-13
    //Aim-To search a element from an array
    #include
    #include
    void main()
    {
    int a[50],i,j,s,flag=1,pos; clrscr();
    printf("How many numbers do you want to enter (max 50)");
    scanf("%d",&i);
    printf("Enter the numbers\n");
    for(j=0;j<i;j++)
    {
    scanf("%d",&a[j]);
    }
    printf("Enter the element you want to search ");
    scanf("%d",&s);
    for(j=0;j<i;j++)
    if(a[j]==s)
    {
    flag=1;
    pos=j;
    }
    else
    {
    flag=0;
    }
    if(flag==1)
    {
    printf("Element is found at %d position",pos+1);
    }
    else
    {
    printf("Element is not found");
    }
    getch();
    }

    ReplyDelete
    Replies
    1. This comment has been removed by the author.

      Delete
    2. Program as asked by you ,position is started from 0

      #include

      void main()
      {
      int a[20],f=0,i,n,srch;
      printf("\nhow many numbers u want to insert(<=20)");
      scanf("\n%d",&n);
      for(i=0;i<n;i++)
      scanf("\n%d",&a[i]);
      printf("Enter the numbers you want to search\n");
      scanf("\n%d",&srch);
      for(i=0;i<n;i++)
      {
      if(a[i]==srch)
      {
      f++;
      break;
      }
      }
      if(f==0)
      printf("\nnumber not found");
      else
      printf("\nnumber is found at %d position",i);
      }

      Delete
  27. guys i need a program to find second largest after sorting the array pls help me out...

    ReplyDelete
  28. Above programe "find the second largest element in an array" will produce wrong value if u Enter elements in array {4,5,3,2,1}. Try it

    ReplyDelete
    Replies
    1. in the above program should be initilalised as
      secondbig=a[0];//correct

      not as
      secondbig=a[size-j-1];//incorrect


      YOU well GEt correct output

      Delete
  29. in the above program should be initilalised as
    secondbig=a[0];//correct

    not as
    secondbig=a[size-j-1];//incorrect

    ReplyDelete
    Replies
    1. hi, this doesn't work for me when the biggest value is the first.

      Delete
  30. how t find second largest in sorted array?

    ReplyDelete
  31. HEY UR PROGRAM IS RIGHT ONLY,WHEN IT IS IN SORTED ORDER,APART FROM SORTED ARRAY IF U GIVE THIS CODE WONT WORK .EX TRY THIS ONE 3 2 6 1 4 IT WONT GET CORRECT SECOND NO.......

    ReplyDelete
  32. FIND OUT SECOND LARGEST NUMBER IN AN UNSORTED ARRAY USING C PROGRAM:
    it gives an incorrect output for the folowing i/p:
    i/p: 3 2 5 1 0
    o/p: second smallest: 5

    ReplyDelete
  33. if you give i/p as
    3
    4
    5
    1
    2
    then the output is 5
    which is incorrect....please reply

    ReplyDelete
  34. plz provide me code for finding second highest element of an unsorted array using pointer

    ReplyDelete
  35. hi admin ,your program of finding 2nd largest element from an array is not working for an array of 5 elements{45,30,55,48,18}

    ReplyDelete
    Replies
    1. here the correct code for finding 2nd largest number from an array

      #include
      void main()
      {
      int a[50],n,i,j=0,max,max2;
      printf("Enter the size of the array: ");
      scanf("%d",&n);
      printf("Enter %d elements in to the array: ", n);
      for(i=0;ia[i] && max2<a[i])
      max2=a[i];
      }

      printf("Second biggest: %d", max2);

      }

      Delete
  36. This comment has been removed by the author.

    ReplyDelete
  37. sorry the first one was wrong this is correct

    #include

    main()
    {
    int a[20],i,sbig,n,fbig,temp;

    printf("enter array element\n");
    scanf("%d",&n);
    printf("enter array elements: ");

    for(i=0;ifbig)
    {
    temp=sbig;
    sbig=fbig;
    fbig=temp;
    }
    for(i=2;ifbig)
    {
    sbig=fbig;
    fbig=a[i];
    }
    else if(a[i]>sbig)

    sbig=a[i];

    printf("the second biggest is %d",sbig);
    }

    ReplyDelete
  38. there should be
    i=0 in for loop for second maximum value as
    secondbig=a[size-j-1];
    for(i=0;i<size;i++){
    if(secondbig <a[i] && j != i)
    secondbig =a[i];
    }

    ReplyDelete
  39. guys easy way to understand...
    finding a maximum value.. but not store a already found highest element that's it
    --------------------------------------------------------------------------------------------------------------------
    #include
    #include
    int main()
    {
    int i,max,second,n,a[10];
    printf("\n enter a n value");
    scanf("%d",&n);
    printf("\n enter a array");
    for(i=0;i<n;i++)
    {
    scanf("%d",&a[i]);
    }
    max=a[0];

    second=a[0];
    for(i=1;i<=n;i++)
    {
    if(max<a[i])
    {
    max=a[i];


    }
    }
    for(i=1;i<=n;i++)
    {

    if(second<a[i])
    {
    if(a[i]!=max)
    second=a[i];


    }


    }
    printf("\n %d",max);
    printf("\n %d",second);

    return 0;
    }

    ReplyDelete
  40. #include
    #include
    main()
    {
    int n,a[20],big1,i,j,big2;
    printf("enter No: ");
    scanf("%d",&n);
    for(i=0;ibig1)
    {
    big1=a[i];
    j=i;
    }
    }
    if(j==(n-1))
    big2=a[j-1];
    else if(j==0)
    big2=a[1];
    else
    big2=a[j+1];

    for(i=0;ibig2)&&(i!=j))
    big2=a[i];
    }
    printf("\n%d",big2);
    getch();
    }
    //NOTE: ARRAY VALUE SHOULD NOT BE DUPLICATE

    ReplyDelete