October 12, 2021, Learn eTutorial

1042

In this c program, we need to calculate the second largest and smallest number of the given numbers in a list or array. Also, calculate the average of the second largest and smallest number. And again, check the average number is present in the list.

To find out the second largest number in an array, we have to sort the array in descending order. Means arrange the number in biggest to smallest. So the number in the second position of the array will be the second-largest number. It will be easy to get that after sorting the array.

To find the second smallest number in an array, we have to sort the array in descending order means arrange the number from biggest to smallest. So the number in the **n-2** the position of the array will be the second-smallest number.

The logic of this program is, first read some numbers from the user and save it in an array. By using nested `for loop`

, arrange the numbers in descending order. Then display the numbers arranged in descending order as the sorted array. Display the second largest number as the **number[1] **and the second smallest number as the **number[n-2]**.

Now calculate **average = number[1] + number[n-2]) /2**. Set the **counter = 0**.By using a `for loop`

with the condition'** i '**

**STEP 1:** Include the header files to use the built-in functions in the C program.

**STEP 2:** Declare the integer variables** i, j, a, n, counter, ave**, and the Array **number[30]**.

**STEP 3**: Read the value of **N** into the variable **n**.

**STEP 4:** Read the numbers into the array **number[i]** using `for loop`

.

**STEP 5:** Set **i=0**.

**STEP 6:** Check the condition** i**

**STEP 7:** set** j=i+1**.

**STEP 8:** Check if **number[i]**

**STEP 9:** Increment** j **by 1 and do step 8.

**STEP 10:** Increment '**i'** by 1 do step 6.

**STEP 11:** Display the numbers arranged in descending order as the numbers in the array **number[]** using `for loop`

.

**STEP 12:** Display The 2nd largest number as the number[1].

**STEP 13:** Display The 2nd smallest number as the number[n-2].

**STEP 14:** Calculate **ave=(number[1] +number[n-2])/2**.

**STEP 15:** Set **counter=0**.

**STEP 16:** By using a `for loop`

with the condition** i**

**STEP 17:** Check if **ave==number[i]** then increment counter by 1.

**STEP 18:** Repeat step 16 by incrementing the value of **'i**' by 1.

**STEP 19:** Check if **counter==0** then display ,the average of the number[1] and number[n-2]** **=ave is not in the array.

**STEP 20:** Check if **counter==1** then display ,the average of the number[1] and number[n-2] =ave is present in the array.

` ````
#include <stdio.h>
void main() {
int number[30];
int i, j, a, n, counter, ave;
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]; /* interchange the numbers to make it in proper order */
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]);
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 %d and %d is = %d is not in the array\n", number[1], number[n - 2], ave);
else
printf("The average of ?nd %d in array is %d in numbers\n", number[1], number[n - 2], counter);
} /* End of main() */
```

Enter the value of N 6 Enter the numbers 30 80 10 40 70 90 The numbers arranged in descending order are given below 90 80 70 40 30 10 The 2nd largest number is = 80 The 2nd smallest number is = 30 The average of 80 and 30 is = 55 is not in the array