C Program to Find the Second Largest Element in an Array

Before going to the program first let us understand what is Second Largest Element in an Array?

Second Largest Element in Array:

                             Element which is having the second largest value in a given array is called as the Second Largest Element in an Array.

Example: Let us consider an array of values as given below:

a[]={5,4,8,6,1,9,3};

then,

The Second Largest Element in an Array is “8”.

Now let us see the program code for Second Largest Element in an Array and understand the code using the Explanation given below.

Program code to find the Second Largest Element in an Array:

#include<stdio.h>
#include<conio.h>

void main()
{
    int a[50];
    int n,i,large,s_large;
    clrscr();
    
    printf("\n Enter number of elements: ");
    scanf("%d",&n);
    
    printf("\n Enter the elements: ");
    for(i=0;i<n;i++)
    {
        scanf("%d",&a[i]);
    }
    
    large=s_large=a[0];
    
    for(i=1;i<n;i++)
    {
        if(large<a[i])
        {
            s_large=large;
            large=a[i];
        }
        else if(s_large<a[i] && a[i]!=large)
        {
            s_large=a[i];
        }
    }
    
    printf("\n The Second Largest Element in the given Array: %d", s_large);
    getch();
}

Explanation:

  • First the computer reads the number of elements in the array from the user and stores it in the “n” variable using the following lines:
printf("\n Enter number of elements: ");
scanf("%d",&n);

 Note: %d is used to read the Integer value.

  • Then it reads the elements one by one using the for loop and stored in the “a” array variable respectively using the following lines:
printf("\n Enter the elements: ");
for(i=0;i<n;i++)
{
    scanf("%d",&a[i]);
}
  • Then it assigns the value of a[0] to “large” and “s_large” variables using the following line:
large=s_large=a[0];
  • Then using for loop and if-else-if conditions the second largest element is found and stored in “s_large” variable using the following lines:
for(i=1;i<n;i++)
{
    if(large<a[i])
    {
        s_large=large;
        large=a[i];
    }
    else if(s_large<a[i] && a[i]!=large)
    {
        s_large=a[i];
    }
}
  • Finally the Second Largest Element in the given Array is printed on the screen using the following line:
printf("\n The Second Largest Element in the given Array: %d", s_large);  

Step by Step working of the above Program Code:

  1. Let us assume that a user enters the Number of elements as 5.
  2. And the user enters the Elements as {6 3 7 9 5}.
  3. So it stores the elements in the array a[]={6,3,7,9,5}.
  4. Now it assigns the value of a[0] to large and s_large(i.e. large=s_large=6).
  5. Then it assigns the value of i=1 and the loop continues till the condition of the for loop is true.

5.1.   i<n    (1<5)    for loop condition is true

large<a[i]   (6<3)    if condition is false

s_large<a[i] && a[i]!=large   (6<3 && 3!=6)    else-if condition is false

 i++    (i=1+1)     So  i=2

5.2.  i<n    (2<5)    for loop condition is true

large<a[i]   (6<7)   if condition is true

s_large=large    So, s_large=6

large=a[i]    So,  large=7

 i++    (i=2+1)     So  i=3

5.3.  i<n    (3<5)    for loop condition is true

large<a[i]   (7<9)   if condition is true

s_large=large    So, s_large=7

large=a[i]    So,  large=9

 i++    (i=3+1)     So  i=4

5.4.  i<n    (4<5)    for loop condition is true

large<a[i]   (9<5)    if condition is false

s_large<a[i] && a[i]!=large   (7<5 && 5!=9)    else-if condition is false

 i++    (i=4+1)     So  i=5

5.5.  i<n    (5<5)    for loop condition is false

It comes out of the for loop.

  1. Finally it prints The Second Largest Element in the given Array: 7
  2. Thus program execution is completed.

Output:

second largest element

TO DOWNLOAD THE PROGRAM CODE : CLICK HERE

 

You may also like...

1 Response

  1. Ashok says:

    Tnx very nice method

Leave a Reply

Your email address will not be published. Required fields are marked *