C Program to Find the Second Largest & Smallest Elements in an Array

«
»

This C Program finds second largest & smallest elements in an Array.

Problem Description

The program will implement a one dimentional array and sort the array in descending order. Then it finds the second largest and smallest element in an array and also find the average of these two array elements. Later it checks if the resultant average number is present in a given array. If found, display appropriate message.

Problem Solution

1. Create a one dimentional array and fill its content to its size.
2. Arrange the array elements in the descending order.
3. The second largest number would be the 2nd element of array, whereas the second smallest number would be the last second element of array.
4. Take average of these two numbers.
5. Now, look for this average number in the array.

advertisement
Program/Source Code

Here is source code of the C program to find the second largest & smallest elements in an array. The program is successfully compiled and tested using Turbo C compiler in windows environment. The program output is also shown below.

  1.  
  2.     /*
  3.      * C program to accept a list of data items and find the second largest
  4.      * and smallest elements in it. Compute the average of both and search
  5.      * for the average value if it is present in the array.
  6.      * Display appropriate message on successful search.
  7.     */
  8.  
  9.     #include <stdio.h>
  10.     void main ()
  11.     {
  12.  
  13.         int number[30];
  14.         int i, j, a, n, counter, average;
  15.  
  16.         printf("Enter the value of N\n");
  17.         scanf("%d", &n);
  18.  
  19.         printf("Enter the numbers \n");
  20.         for (i = 0; i < n; ++i)
  21.             scanf("%d", &number[i]);
  22.  
  23.         for (i = 0; i < n; ++i)
  24.         {
  25.             for (j = i + 1; j < n; ++j)
  26.             {
  27.                 if (number[i] < number[j])
  28.                 {
  29.                     a = number[i];
  30.                     number[i] = number[j];
  31.                     number[j] = a;
  32.                 }
  33.             }
  34.  
  35.         }
  36.  
  37.         printf("The numbers arranged in descending order are given below \n");
  38.  
  39.         for (i = 0; i < n; ++i)
  40.         {
  41.             printf("%d\n", number[i]);
  42.         }
  43.  
  44.         printf("The 2nd largest number is  = %d\n", number[1]);
  45.         printf("The 2nd smallest number is = %d\n", number[n - 2]);
  46.  
  47.         average = (number[1] + number[n - 2]) / 2;
  48.         counter = 0;
  49.  
  50.         for (i = 0; i < n; ++i)
  51.         {
  52.             if (average == number[i])
  53.             {
  54.                 ++counter;
  55.             }
  56.         }
  57.  
  58.         if (counter == 0 )
  59.             printf("The average of %d  and %d is = %d is not in the array \n",
  60.             number[1], number[n - 2], average);
  61.  
  62.         else
  63.             printf("The average of %d  and %d in array is %d in numbers \n",
  64.             number[1], number[n - 2], counter);
  65.     }
Program Explanation

1. Declare an array, a of some fixed capacity, 30.
2. Take the size of the array as input from users.
3. Define all the elements of the array using for loop.
4. Sort the element of the array using Insertion Sort in descending order.
5. The second largest number would be the second element of array (array[1]) and second smallet number would be the second last element of array (array[n-2]).
6. Take the average of these two numbers by adding both and dividing by 2.
7. Using for loop, scan each and every element of the array to check whether the element equals the average.
8. If the average is present in the array, then print appropriate message.
9. Exit

advertisement
Runtime Test Cases
Enter the value of N
4
Enter the numbers
450
340
120
670
The numbers arranged in descending order are given below
670
450
340
120
The 2nd largest number is  = 450
The 2nd smallest number is = 340
The average of 450  and 340 is = 395 is not in the array

Sanfoundry Global Education & Learning Series – 1000 C Programs.

Here’s the list of Best Reference Books in C Programming, Data Structures and Algorithms.

advertisement

advertisement
advertisement
advertisement
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He is Linux Kernel Developer & SAN Architect and is passionate about competency developments in these areas. He lives in Bangalore and delivers focused training sessions to IT professionals in Linux Kernel, Linux Debugging, Linux Device Drivers, Linux Networking, Linux Storage, Advanced C Programming, SAN Storage Technologies, SCSI Internals & Storage Protocols such as iSCSI & Fiber Channel. Stay connected with him @ LinkedIn