C Program for Second Maximum and Second Minimun in an One-Dimensional Array

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();

}    

C Program for Maximum and Minimun number in an One-Dimensional Array

The below code will find the maximum and minimum numbers in an array.

Logic: Use linear search to search for the max and min numbers.

Code:

#include <stdio.h>
#include <conio.h>  

void main()
{
   int i,size,max,min;

  //get the size of the array  

   printf("Welcome\nEnter the total number of elements(numbers):");
   scanf("%d",&size);
   
  //declare an array

  int numbers[size];
  
  //take input , to fill the array.
  
  printf("Enter numbers");
  for(i=0;i<size;i++)
  {
     scanf("%d",&numbers[i];
   }

 /*to calculate maximum and minimum,these variables need to be initialized with some data,therefore we initialize it with the first elements of the array*/

  max=numbers[0];
  min=numbers[0];

 //use linear search to find the max and min number of the array

//star from index 1 as min and max are already initialized with the element at index o
 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];
 }

 //print the max and min numbers

 printf("\nMax is %d\nMin is %d",max,min);

 printf("\npress any key to continue");
 getch();

}   
 

C Program for Sum of Numbers in an One-Dimensional Array

The below code will generate the sum of all the numbers stored in an 1D-Array.

Logic: Add all the numbers from index zero to the last index

Code:

#include <stdio.h>
#include <conio.h>

void main()
{
   int i,sum;
   sum =0;
   int numbers[]={1,5,2,3,4,7};

  for(i=0;i<6;i++)
  {
     sum=sum+numbers[i];
  }
   
  printf("Sum is %d",sum);

 printf("\npress any key to continue");
 getch();
}
Older Posts
© Copyright Encyclopedia of C | Designed By Code Nirvana
Back To Top