C Program to Print Second Largest & Second Smallest Array Element

In this tutorial, we will learn about how to create a program in C that will ask from user to enter 10 array elements as input and then find and print out the second largest and second smallest element from that given array. Here is the program:

#include<stdio.h>
#include<conio.h>
int main()
{
    int arr[10], great, small, i, j, k, limit=10, count=0;
    printf("Enter any 10 elements: ");
    for(i=0; i<limit; i++)
    {
        scanf("%d", &arr[i]);
    }
	
    // remove duplicate element
    for(i=0; i<limit; i++)
    {
        for(j=i+1; j<limit; j++)
        {
            if(arr[i]==arr[j])
            {
                count++;
                limit = limit-count;
                for(k=j; k<limit; k++)
                    arr[k] = arr[k+1];
                count = 0;
            }
        }
    }
	
    // find first largest and smallest element
    great = arr[0];
    small = arr[0];
    for(i=0; i<limit; i++)
    {
        if(great<arr[i])
            great = arr[i];
        if(small>arr[i])
            small = arr[i];
    }
	
    // remove first largest element
    for(i=0; i<limit; i++)
    {
        if(great==arr[i])
        {
            count++;
            limit = limit-count;
            for(j=i; j<limit; j++)
                arr[j] = arr[j+1];
            count = 0;
        }
    }
	
    // remove first smallest element
    for(i=0; i<limit; i++)
    {
        if(small==arr[i])
        {
            count++;
            limit = limit-count;
            for(j=i; j<limit; j++)
                arr[j] = arr[j+1];
            count = 0;
        }
    }
	
    // now find largest and smallest element, this time it will be second largest/largest
    great = arr[0];
    small = arr[0];
    for(i=0; i<limit; i++)
    {
        if(great<arr[i])
            great = arr[i];
        if(small>arr[i])
            small = arr[i];
    }
    printf("\nThe second largest number = %d", great);
    printf("\nThe second smallest number = %d", small);
    getch();
    return 0;
}

The program was build and run under Code::Blocks IDE, here is the first snapshot of the sample run:

second largest smallest element c

Provide any 10 elements and then press ENTER key to see the output that will prints the second largest and the second smallest element from the list of given 10 numbers as array elements as shown in the second snapshot of the sample run given here:

print second smallest largest c program

Here is another sample run of the above program. This time let's suppose user has provided some duplicate elements as input say 1, 2, 3, 4, 1, 2, 1, 8, 8, 4 are the 10 numbers given by user at run-time. As you can see that, there are some duplicate elements in such way:

  • 1 present three times
  • 2 present two times
  • 3 present one time
  • 4 present two times
  • 8 present two times

Therefore the above program will remove all the duplicate elements first and then will find out the first largest and smallest element, and then will remove both to find again the largest and smallest element that will be the second largest and smallest element of the given element. In this case, the second largest element will be 4 and the second smallest element will be 2. This is the final snapshot of the sample run:

c program print second largest smallest

Here are some of the main steps used in above program:

  • Receive any 10 numbers as 10 array elements
  • Remove all the duplicate elements from the array
  • To remove duplicate elements from the array, follow these steps:
    • Take a variable say limit at start of the program and initialize it with 10 (this is the size of the array)
    • Create a for loop, that runs from 0 to one less than the value of limit variable
    • Inside this for loop, create another for loop, that starts from one greater than the value of i (outer loop variable), that is i+1 and runs upto one less than the value of variable limit
    • Compare the element present at ith index and the element present at jth index, that is whether it is an equal or not
    • If it is equal to each other, then remove that element and continue to check for the next match
  • Now find the first largest and smallest element
  • Here are the steps to find the first largest and smallest element:
    • Initialize the first element as largest and smallest one
    • Now compare with each and every element of the array that is whether the largest element is less than any other element or not
    • If it is found as lesser then place that element or initialize that element as largest element and continue to check for the next
    • Process the same steps to find the first smallest element
  • Now remove the first largest and smallest element from the given array
  • Again find the largest and smallest element from the given array
  • As the first largest and smallest element was deleted, therefore this time the largest and smallest element will be the second largest and smallest element
  • Print the value of both the variable say great and small that holds the second largest and second smallest element from the given array by user at run-time

C Online Test


« Previous Program Next Program »