FIND OUT SECOND LARGEST NUMBER IN AN UNSORTED ARRAY USING C PROGRAM





C program to find the second largest element in an 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





67 comments:

Anonymous said...

Fantastic site!!
Thank you very much...
Subha

Anonymous said...

hey Jason, here is the code:
if(a[i]>max)
{
max2=max;
max=a[i]}
}
else if(a[i]>max2)
{
max2=a[i];
}

Paul said...

i fixed the code.

if(a[i]>max)
{
max2=max;
max=a[i]}
}
else if(a[i]>max2&&a[i]!=max)
{
max2=a[i];
}

and here's how you find the third and the fourth largest numbers.

min=num[0];
for(i=1;inum[i])
min=num[i];
}
max=min;
max2=min;
max3=min;
max4=min;
for(i=0;imax)
{
max4=max3;
max3=max2;
max2=max;
max=num[i];
}
else if(num[i]>max2&&num[i]!=max)
max2=num[i];
else if(num[i]>max3&&num[i]!=max2&&num[i]!=max)
max3=num[i];
else if(num[i]>max4&&num[i]!=max3&&num[i]!=max2&&num[i]!=max)
max4=num[i];
}

Paul SIIT 20 said...

Woops, got some typos there.

min=num[0];
for(i=1;inum[i])
min=num[i];
}

Paul SIIT 20 said...

lol something's wrong here, but you get the concept.

Anonymous said...

Kindly answer this prob...if i want to find out the position of the largest number in an array than how to find it???..thnx in advance =)

Ritesh said...

To find out the largest element array. I think following link will help you.

http://cquestionbank.blogspot.com/2008/01/write-c-program-to-find-largest-number.html

Anonymous said...

to find nthbiggest... pseudo code...
correct me i it has errors..

static int k = 0; // golbal

void main()
{
int i, j,size,n,nthbig;
int arr[];
\\enter nth heighest...

for(i=1;i<=n;i++)
nthbig=selectbig();
}



int selectbig()
{
int temp;

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

temp = a[i];
a[i]=a[k];
a[k]=temp;
k++;

return big;
}

vaibhav said...

but how do we create the algorithm

Anonymous said...

thank u

Anonymous said...

very good

Anonymous said...

Write a program that accepts five numbers from the user and displays the highest and lowest number. Assume that there are no duplicate values.

venkatesh said...

Use the technique of bubble sorting
for(i=0;i<=1;i++){
for(j=0;ja[j+1]){
temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;
}
}
}
printf("%d",a[n-1]);
}

Anonymous said...

with respect to the code for finding out the second highest number, correct output is not being given for the following set of inputs:
4,4,3..Please help!

urvinder said...

in this program secondbig=a[size-j] not a[size-j-1]....tyhen this works fine for the input 1 5 2 3 4

urvinder said...

in this program secondbig=a[size-j] not a[size-j-1]....tyhen this works fine for the input 1 5 2 3 4

Anonymous said...

guys how to do it without sorting the array

GOBINATH said...

Hi think this program is wrong ..pls verify ....

GOBINATH said...
This comment has been removed by the author.
Anonymous said...

hey bro, go'g to do Mini project on this sem ,am do'g mca , plz suggest a live project . . .its instance.

rishi said...

sec_big=arr[0];
for(i=0;isec_big)&&(sec_big<big))
{
sec_big=arr[i];
}
}

kishore raavi said...
This comment has been removed by the author.
kishore raavi said...
This comment has been removed by the author.
kishore raavi said...

#include

main()
{
int a[] = {9000, 90, 6, 20, 54, 1000, 23, 58,12, 500};
int i, fl, sl;

fl =a[0]; sl = a[0];

for(i = 1; i < 10; i++)
{
if(fl < a[i])
{
sl = fl;
fl = a[i];

}
else if (sl < a[i])
sl = a[i];
else if (sl == fl && sl > a[i])
sl = a[i];
}

printf("FL = %d \n SL = %d", fl, sl);

}

kishore raavi said...

#include

main()
{
int a[] = {9000, 90, 6, 20, 54, 1000, 23, 58,12, 500};
int i, fl, sl;

fl =a[0]; sl = a[0];

for(i = 1; i < 10; i++)
{
if(fl < a[i])
{
sl = fl;
fl = a[i];

}
else if (sl < a[i])
sl = a[i];
else if (sl == fl && sl > a[i])
sl = a[i];
}

printf("FL = %d \n SL = %d", fl, sl);

}

reach me for queries at raavi.kishore@gmail.com

Anonymous said...

it is nice

Anonymous said...

please help me out to correct the programme
here the coding
// Program-13
//Aim-To search a element from an array
#include
#include
void main()
{
int a[50],i,j,s,flag=1,pos; clrscr();
printf("How many numbers do you want to enter (max 50)");
scanf("%d",&i);
printf("Enter the numbers\n");
for(j=0;j<i;j++)
{
scanf("%d",&a[j]);
}
printf("Enter the element you want to search ");
scanf("%d",&s);
for(j=0;j<i;j++)
if(a[j]==s)
{
flag=1;
pos=j;
}
else
{
flag=0;
}
if(flag==1)
{
printf("Element is found at %d position",pos+1);
}
else
{
printf("Element is not found");
}
getch();
}

mahesh Ece said...

guys i need a program to find second largest after sorting the array pls help me out...

Amit Singh said...

Above programe "find the second largest element in an array" will produce wrong value if u Enter elements in array {4,5,3,2,1}. Try it

Anonymous said...

in the above program should be initilalised as
secondbig=a[0];//correct

not as
secondbig=a[size-j-1];//incorrect

Anonymous said...

in the above program should be initilalised as
secondbig=a[0];//correct

not as
secondbig=a[size-j-1];//incorrect


YOU well GEt correct output

ricci said...

hi, this doesn't work for me when the biggest value is the first.

Tariq Sajid said...

how t find second largest in sorted array?

aamir khurram said...

initial value of max and max 2 is a[0]. isnt it?

BHARGAV said...

HEY UR PROGRAM IS RIGHT ONLY,WHEN IT IS IN SORTED ORDER,APART FROM SORTED ARRAY IF U GIVE THIS CODE WONT WORK .EX TRY THIS ONE 3 2 6 1 4 IT WONT GET CORRECT SECOND NO.......

Anonymous said...

FIND OUT SECOND LARGEST NUMBER IN AN UNSORTED ARRAY USING C PROGRAM:
it gives an incorrect output for the folowing i/p:
i/p: 3 2 5 1 0
o/p: second smallest: 5

Anonymous said...

if you give i/p as
3
4
5
1
2
then the output is 5
which is incorrect....please reply

Jwel said...

plz provide me code for finding second highest element of an unsorted array using pointer

Ramniwas Saini said...

your logic of finding 2nd largest no. is not always right.
here if a[0] is bigest then it will not work

Ramniwas Saini said...
This comment has been removed by the author.
Ramniwas Saini said...

Program as asked by you ,position is started from 0

#include

void main()
{
int a[20],f=0,i,n,srch;
printf("\nhow many numbers u want to insert(<=20)");
scanf("\n%d",&n);
for(i=0;i<n;i++)
scanf("\n%d",&a[i]);
printf("Enter the numbers you want to search\n");
scanf("\n%d",&srch);
for(i=0;i<n;i++)
{
if(a[i]==srch)
{
f++;
break;
}
}
if(f==0)
printf("\nnumber not found");
else
printf("\nnumber is found at %d position",i);
}

Ramniwas Saini said...

hi admin ,your program of finding 2nd largest element from an array is not working for an array of 5 elements{45,30,55,48,18}

Ramniwas Saini said...

here the correct code for finding 2nd largest number from an array

#include
void main()
{
int a[50],n,i,j=0,max,max2;
printf("Enter the size of the array: ");
scanf("%d",&n);
printf("Enter %d elements in to the array: ", n);
for(i=0;ia[i] && max2<a[i])
max2=a[i];
}

printf("Second biggest: %d", max2);

}

Fahim Faiaz said...
This comment has been removed by the author.
Fahim Faiaz said...

sorry the first one was wrong this is correct

#include

main()
{
int a[20],i,sbig,n,fbig,temp;

printf("enter array element\n");
scanf("%d",&n);
printf("enter array elements: ");

for(i=0;ifbig)
{
temp=sbig;
sbig=fbig;
fbig=temp;
}
for(i=2;ifbig)
{
sbig=fbig;
fbig=a[i];
}
else if(a[i]>sbig)

sbig=a[i];

printf("the second biggest is %d",sbig);
}

Jatin dhillwan said...

there should be
i=0 in for loop for second maximum value as
secondbig=a[size-j-1];
for(i=0;i<size;i++){
if(secondbig <a[i] && j != i)
secondbig =a[i];
}

mathesh waran said...

guys easy way to understand...
finding a maximum value.. but not store a already found highest element that's it
--------------------------------------------------------------------------------------------------------------------
#include
#include
int main()
{
int i,max,second,n,a[10];
printf("\n enter a n value");
scanf("%d",&n);
printf("\n enter a array");
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
max=a[0];

second=a[0];
for(i=1;i<=n;i++)
{
if(max<a[i])
{
max=a[i];


}
}
for(i=1;i<=n;i++)
{

if(second<a[i])
{
if(a[i]!=max)
second=a[i];


}


}
printf("\n %d",max);
printf("\n %d",second);

return 0;
}

Subhra Sankha Bose said...

#include
#include
main()
{
int n,a[20],big1,i,j,big2;
printf("enter No: ");
scanf("%d",&n);
for(i=0;ibig1)
{
big1=a[i];
j=i;
}
}
if(j==(n-1))
big2=a[j-1];
else if(j==0)
big2=a[1];
else
big2=a[j+1];

for(i=0;ibig2)&&(i!=j))
big2=a[i];
}
printf("\n%d",big2);
getch();
}
//NOTE: ARRAY VALUE SHOULD NOT BE DUPLICATE

Saurabh Joshi said...
This comment has been removed by the author.
Saurabh Joshi said...
This comment has been removed by the author.
keerthana said...

You are given a sequence of integers as input, terminated by -1. (That is, the input integers may be positive, negative or 0. -1 in the input signals the end of the input.) -1 is not considered as part of the input. Find the second largest number in the input.

keerthana said...

can anyone help me out in solving this...

Naveen said...

#include
void main()
{
int a[10],n,i,max,smax,t;
printf("enter no. of terms\n");
scanf("%d",&n);
for(i=0;imax)
{
smax=max;
max=a[i];
}
else if(a[i]>smax&&a[i]!=max)
{
smax=a[i];
}
}
printf("second no. is\t%d",smax);

}

Sahil Mittal said...

can anyone tell me the code to print the following structure

1
3 5
7 9 11

sanny chauhan said...

Good post
Here is a link for C/C++ programs and pointer programs. This may be useful for you.

Second largest element in array

C++ Programs

Nantha Kumar said...

#include
#include
void main()
{
int i,j,n=3,a=1;
for(i=0;i<n;i++)
{
for(j=0;j<=i;j++)
{
printf("%d",a);
a=a+2;
}
printf("\n");
}
}

Sourav Roy said...

m=a[0];
n=m;
for(i=0;i<p-1;i++)
{
if(m<a[i+1])
{
n=m;
m=a[i+1];
}
if(n<a[i+1]&&a[i+1]<m)
{
n=a[i+1];
}
}

keep calm and implement this code.... and ya.... n is the second largest....

Unknown said...

this program is giving wrong output....

if u give the size 4
and no 8 ,7 ,9 ,0
then it give the output 7
but correct output is 8

Ashish Prashant said...

#include
#include
int main()
{
int a[10],i,ctr=0,j,n,big,big1,l;
clrscr();
printf("enter the size::");
scanf("%d",&n);
printf("\nenter the elment::");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
big=a[0];
for(i=1;i<n;i++){
if(big<a[i]){
big=a[i];
j=i;
}
if(i==n-1 && ctr==0){
ctr++;
l=j;
big1=a[j];
a[j]=0;
i=0;
big=a[0];
}
}
a[l]=big1;
printf("\nsecond largest no=%d",big);
getch();
return 0;
}

Karan R Karanjekar said...

Thanks a lot! I appreciate your help.

John Abruzzi said...

import java.util.*;
class test
{
public static void main(String args[])
{
int n,l,i,j=0,l1=0;
Scanner in=new Scanner(System.in);
System.out.println("Enter no of el:");
n=in.nextInt();
int a[]=new int[n];
for(i=0;i<=n-1;i++)
{
a[i]=in.nextInt();
}
l=a[0];
for(i=0;i<=n-1;i++)
{
if(a[i]>l)
{
l=a[i];
j=i;
}
}
a[j]=0;
l1=a[0];

for(i=0;i<=n-1;i++)
{
if(l1<a[i])
{
l1=a[i];
}
}
System.out.println("1st Large:"+l+"\n2nd Large:"+l1);
}}

Shubham Gupta said...

this is some what wrong
give input as:
let there be 8 elemets
6 5 4 7 3 2 1 0
second smallest will come as 5 not 6

actually in second loop i should be initialized from 0

Deepankan Sharma said...

Try it will work

#include
int main(){
int a[50],size,i,j=0,big,big2;
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]){
big2=big;
big=a[i];
}

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

}
printf("2nd highest no is %d: ", big2);
}

Tobin Karukulathel Tomy said...

#include
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=0;i<size;i++){
if(big<a[i]){
big=a[i];

}
}

secondbig=a[0];
for(i=0;i<size;i++){
if(secondbig < a[i] && a[i] != big)
secondbig =a[i];
}

printf("first Second biggest:%d %d",big, secondbig);
return 0;
}

Aboody Magdy said...

#include
using namespace std;
int main()
{
int array[100];
int s2ndmax;
int y=0;
cout<<"enter the element of array"<>x;
cout<<"Enter your numbers"<>array[i];
}
int max=array[0];
for(int i=0;imax){
max=array[i];
y=i;
}
}

for(int c=y;cmax){
max=array[i];
y=i;}
}
cout<<max;
}

Aboody Magdy said...
This comment has been removed by the author.
Aadhil RF said...

for(int i=0;i>a[i];
if(a[i]>max1&&i<(n/2))
max1=a[i];
else if(a[i]>max2)
max2=a[i];
}
if(max1>max2)
cout<<"The second max is "<<max2;
else
cout<<"The second max is "<<max1;