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

Advertisements
This C Program finds second largest & smallest elements in an array. The program sorts the array in an descending order. Then it finds the second largest and smallest element in an array and also find the average of an array element. Later it checks if the resultant average number is present in a given array. If found, display appropriate message.

Here is source code of the C program to find the second largest & smallest elements in an array. The C program is successfully compiled and run on a Linux system. The program output is also shown below.

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


$ cc pgm68.c
$ a.out
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

If you wish to look at other example programs on Arrays, go to C Programming Examples on Arrays. If you wish to look at programming examples on all topics, go to C Programming Examples.
Advertisements
Subscribe Newsletter & Posts

Manish Bhojasia, a technology veteran with 17+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He is Linux Kernel Developer and 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 & Cluster Administration, Advanced C Programming, SAN Storage Technologies, SCSI Internals and Storage Protocols such as iSCSI & Fiber Channel. Stay connected with him below.

Practice Interview Questions & Answers, Quizzes, Objective, Multiple Choice Aptitude Tests for Freshers and Experienced People in these topics - Learn C, C++, Java and C# Programming with coding example on Simple problems as well as tough Algorithms and Data-structures along with runtime output: - C Programs, Data-Structures and Algorithms, C++ Algorithms, Java Algorithms, C# Programs, Android Programs in Java

C Tutorials and Linux Commands Tutorial - C Tutorials, Linux Commands

Career Mentoring with our Founder / CTO - Read More
Advertisements

Follow Manish & Sanfoundry