This C Program finds second largest & smallest elements in an Array.
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.
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.
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.
/*
* C program to accept a list of data items and find the second largest
* and smallest elements in it. Compute the average of both and search
* for the average value if it is present in the array.
* Display appropriate message on successful search.
*/
#include <stdio.h>
void main ()
{
int number[30];
int i, j, a, n, counter, average;
printf("Enter the value of N\n");
scanf("%d", &n);
printf("Enter the numbers \n");
for (i = 0; i < n; ++i)
scanf("%d", &number[i]);
for (i = 0; i < n; ++i)
{
for (j = i + 1; j < n; ++j)
{
if (number[i] < number[j])
{
a = number[i];
number[i] = number[j];
number[j] = a;
}
}
}
printf("The numbers arranged in descending order are given below \n");
for (i = 0; i < n; ++i)
{
printf("%d\n", number[i]);
}
printf("The 2nd largest number is = %d\n", number[1]);
printf("The 2nd smallest number is = %d\n", number[n - 2]);
average = (number[1] + number[n - 2]) / 2;
counter = 0;
for (i = 0; i < n; ++i)
{
if (average == number[i])
{
++counter;
}
}
if (counter == 0 )
printf("The average of %d and %d is = %d is not in the array \n",
number[1], number[n - 2], average);
else
printf("The average of %d and %d in array is %d in numbers \n",
number[1], number[n - 2], counter);
}
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
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 Books in C Programming, Data Structures and Algorithms.
- Practice Computer Science MCQs
- Apply for Computer Science Internship
- Apply for C Internship
- Check Computer Science Books
- Check C Books