Sunday, 1 September 2013

Write a c program to find out second largest element of an unsorted array.



Write a c program to find out second largest element of an unsorted array.

#include<stdio.h>
int main(){
  int a[50],size,i,j=0,big,secondbig;
  printf("Enter the size of the array: ");
  scanf("%d",&size);
  printf("Enter %d elements in to the array: ", size);
  for(i=0;i<size;i++)
      scanf("%d",&a[i]);

  big=a[0];
  for(i=1;i<size;i++){
      if(big<a[i]){
           big=a[i];
           j = i;
      }
  }

  secondbig=a[size-j-1];
  for(i=1;i<size;i++){
      if(secondbig <a[i] && j != i)
          secondbig =a[i];
  }
  
  printf("Second biggest: %d", secondbig);
  return 0;
}

Sample output:
Enter the size of the array: 5
Enter 5 elements in to the array: 5 3 2 1 0
Second biggest: 3

1 comments:

taffiulery on 5 March 2022 at 05:51 said...

Tatsujin no Hazou-Toki (Sega Saturn) - Titanium Legs
Tatsujin titanium guitar chords no titanium engagement rings Hazou-Toki babyliss pro titanium is titanium aura quartz the apple watch 6 titanium first

Post a Comment

 

C Programming Language Interview Questions and Answers Tutorial for beginners. Copyright 2013 All Rights Reserved