- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
C program to find the second largest and smallest numbers in an array
Enter the array elements and then, arrange the numbers in descending order by using the swapping technique. Later on, with the help of an index location, try to print the second largest and the second smallest element in an array.
An array is used to hold the group of common elements under one name.
The array operations in C programming language are as follows −
- Insert
- Delete
- Search
Algorithm
Given below is an algorithm to find the second largest and the second smallest numbers in an array −
Step 1 − Declare and read the number of elements.
Step 2 − Declare and read the array size at runtime.
Step 3 − Input the array elements.
Step 4 − Arrange numbers in descending order.
Step 5 − Then, find the second largest and second smallest numbers by using an index.
Step 6 − Print the second largest and the second smallest numbers.
Program
Given below is the C program to find the second largest and the second smallest numbers in an array −
#include<stdio.h> void main(){ int i,j,a,n,counter,ave,number[30]; printf ("Enter the value of N
"); scanf ("%d", &n); printf ("Enter the numbers
"); 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
"); for (i=0; i<n; ++i) printf ("%10d
",number[i]); printf ("The 2nd largest number is = %d
", number[1]); printf ("The 2nd smallest number is = %d
", number[n-2]); ave = (number[1] +number[n-2])/2; counter = 0; for (i=0; i<n; ++i){ if (ave==number[i]) ++counter; } if (counter==0) printf("The average of 2nd largest & 2nd smallest is not in the array
"); else printf("The average of 2nd largest & 2nd smallest in array is %d in numbers
", counter); }
Output
When the above program is executed, it produces the following result −
Enter the value of N 5 Enter the numbers 10 12 17 45 80 The numbers arranged in descending order are given below 80 45 17 12 10 The 2nd largest number is = 45 The 2nd smallest number is = 12 The average of 2nd largest & 2nd smallest is not in the array
- Related Articles
- Java program to find Largest, Smallest, Second Largest, Second Smallest in an array
- C# program to find Largest, Smallest, Second Largest, Second Smallest in a List
- Python program to find Largest, Smallest, Second Largest, and Second Smallest in a List?
- Find the smallest and second smallest elements in an array in C++
- Maximum sum of smallest and second smallest in an array in C++ Program
- C++ Program to find the second largest element from the array
- Maximum sum of smallest and second smallest in an array in C++
- Rearrange An Array In Order – Smallest, Largest, 2nd Smallest, 2nd Largest,. Using C++
- C# Program to find the smallest element from an array
- Program to find Smallest and Largest Word in a String in C++
- Program to find largest element in an array in C++
- C# Program to find the largest element from an array
- C++ Program to Find Largest Element of an Array
- C++ program to find Second Smallest Element in a Linked List
- Program to find the largest and smallest ASCII valued characters in a string in C++
