Home > Backend Development > C++ > body text

C program to find the second largest and second smallest number in an array

王林
Release: 2023-09-06 08:33:12
forward
1093 people have browsed it

C program to find the second largest and second smallest number in an array

Enter the array elements and then use the swapping technique to sort the numbers in descending order. Subsequently, with the help of index position, try to print the second largest and second smallest element in the array.

Array is used to save a group of common elements under the same name.

Array is used to save a group of common elements under the same name. p>

The array operations in C language are as follows -

  • Insert
  • Delete
  • Search li>

Algorithm

Given below is an algorithm to find the second largest and second smallest number in an array -

Step 1 strong> - Declare and read the number of elements.

Step 2 - Declare and read the array size at runtime.

Step 3 - Enter the array elements.

Step 4 - Arrange the numbers in descending order.

Step 5 - Then, use the index to find the second largest and second smallest number.

Step 6 - Print the second largest and second smallest number.

Program

Given below is the C program for finding the second largest and second smallest number in an array -

#include<stdio.h>
void main(){
   int i,j,a,n,counter,ave,number[30];
   printf ("Enter the value of N</p><p>");
   scanf ("%d", &n);
   printf ("Enter the numbers </p><p>");
   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</p><p>");
   for (i=0; i<n; ++i)
      printf ("%10d</p><p>",number[i]);
   printf ("The 2nd largest number is = %d</p><p>", number[1]);
   printf ("The 2nd smallest number is = %d</p><p>", 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</p><p>");
   else
      printf("The average of 2nd largest & 2nd smallest in array is %d in numbers</p><p>", counter);
}
Copy after login

Output

When the above program is executed, the following results will be produced-

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
Copy after login

The above is the detailed content of C program to find the second largest and second smallest number in an array. For more information, please follow other related articles on the PHP Chinese website!

source:tutorialspoint.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template