There are two approaches to calculate maximum,minimum,second-maximum and second-minimum.
The first one is through sorting.You can sort the array in ascending or descending order and can get the required degree of number i.e max,min,etc.
The second approach is without sorting.
The below code will generate 2nd max and 2nd min from a list of numbers in an array without sorting.
Logic: Find the required number using linear search
Code:
#include <stdio.h>
#include <conio.h>
void main()
{
int i,size,min,max,second_max,second_min;
//get size of the array
printf("Enter total number of elements:");
scanf("%d",&size);
//declare an array
int numbers[size];
//fill the array
printf("Enter numbers:\n");
for(i=0;i<size;i++)
{
scanf("%d",&numbers[i]);
}
//initialize max,min,second_max,second_min with first element of the array
max=min=second_min=second_max=numbers[0];
/*find the max and min numbers from the array.
start searching from index 1 because max,min already contain the numbers at
index 0
*/
for(i=1;i<size;i++)
{
/*if the number at index i is greater then the number in max
then put the number at i in max
*/
if(max<numbers[i])
max=numbers[i];
/*if the number at index i is less then the number in min
then put the number at i in min
*/
if(min>numbers[i])
min=numbers[i];
}
/*find the second max and second min numbers from the array
go through the array,if the number at index i is equal to the number in max
or min,then skip the checking process and proceed to the next number in the
array
*/
for(i=1;i<size;i++)
{
if(numbers[i]==max || numbers[i]==min)
continue;
if(second_max<numbers[i])
second_max=numbers[i];
if(second_min>numbers[i])
second_min=numbers[i];
}
//print the second max and second min numbers
printf("\nThe second maximum number is %d\nThe second minimum number is %d",second_max,second_min);
printf("\npress any key to continue");
getch();
}
The first one is through sorting.You can sort the array in ascending or descending order and can get the required degree of number i.e max,min,etc.
The second approach is without sorting.
The below code will generate 2nd max and 2nd min from a list of numbers in an array without sorting.
Logic: Find the required number using linear search
Code:
#include <stdio.h>
#include <conio.h>
void main()
{
int i,size,min,max,second_max,second_min;
//get size of the array
printf("Enter total number of elements:");
scanf("%d",&size);
//declare an array
int numbers[size];
//fill the array
printf("Enter numbers:\n");
for(i=0;i<size;i++)
{
scanf("%d",&numbers[i]);
}
//initialize max,min,second_max,second_min with first element of the array
max=min=second_min=second_max=numbers[0];
/*find the max and min numbers from the array.
start searching from index 1 because max,min already contain the numbers at
index 0
*/
for(i=1;i<size;i++)
{
/*if the number at index i is greater then the number in max
then put the number at i in max
*/
if(max<numbers[i])
max=numbers[i];
/*if the number at index i is less then the number in min
then put the number at i in min
*/
if(min>numbers[i])
min=numbers[i];
}
/*find the second max and second min numbers from the array
go through the array,if the number at index i is equal to the number in max
or min,then skip the checking process and proceed to the next number in the
array
*/
for(i=1;i<size;i++)
{
if(numbers[i]==max || numbers[i]==min)
continue;
if(second_max<numbers[i])
second_max=numbers[i];
if(second_min>numbers[i])
second_min=numbers[i];
}
//print the second max and second min numbers
printf("\nThe second maximum number is %d\nThe second minimum number is %d",second_max,second_min);
printf("\npress any key to continue");
getch();
}
Post a Comment