codescracker


c

C Program to Print Second Largest & Second Smallest Array Element



« C Tutorial C Examples »

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:

// Write a program in C to read any 10 Elements
// in an array and print out the second largest
// and second smallest element among them
// -----------codescracker.com-----------

#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
    // and smallest element as first one was
    // removed earlier
    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:

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:


« C Tutorial C Examples »






Tools
Calculator

Quick Links
Signup - Login - Give Online Test