C Program to Print the Second Largest and Second Smallest Array Element

In this tutorial, we will learn how to create a program in C that will ask the 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 the 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 the 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 the 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 the 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 the largest and smallest element, this time it will be the second largest/smallest
    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 built and run in the Code::Blocks IDE. Here is the first snapshot of the sample run:

second largest smallest element c

Provide any 10 elements and then press the ENTER key to see the output that will print 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. Assume the user has provided some duplicate elements as input this time, say 1, 2, 3, 4, 1, 2, 1, 8, 8, and 4 are the 10 numbers provided by the user at run-time. As you can see, there are some duplicate elements in the following way:

Therefore, the above program will first remove all the duplicate elements and then find out the first largest and smallest element, and then remove both to find 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 the above program:

C Quiz


« Previous Program Next Program »


Liked this post? Share it!