Advertisement
Md_Touhid

Find second largest

Jun 8th, 2023
521
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.72 KB | None | 0 0
  1. #include<stdio.h>
  2.  
  3. int main()
  4. {
  5.     printf("Array size : ");
  6.     int arraySize;
  7.     scanf("%d", &arraySize);
  8.  
  9.     int array[arraySize];
  10.     for(int i=0; i<arraySize; i++)
  11.     {v c
  12.         printf("Enter element %d : ", i);
  13.         scanf("%d", &array[i]);
  14.     }
  15.  
  16.     int scndMaxElement = find_scnd_max(array, arraySize);
  17.  
  18.     printf("The 2nd max element is : %d", scndMaxElement);
  19. }
  20.  
  21. int find_scnd_max(int array[], int arraySize)
  22. {
  23.     int maxElement = array[0];
  24.     int scndMaxElement = array[0];
  25.  
  26.     for(int i=1; i<arraySize; i++)
  27.     {
  28.         if(array[i]>maxElement)
  29.         {
  30.             scndMaxElement = maxElement;
  31.             maxElement = array[i];
  32.         }
  33.     }
  34.  
  35.     return scndMaxElement;
  36. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement