**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

Fantastic site!!

ReplyDeleteThank you very much...

Subha

hey Jason, here is the code:

ReplyDeleteif(a[i]>max)

{

max2=max;

max=a[i]}

}

else if(a[i]>max2)

{

max2=a[i];

}

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

Deletei fixed the code.

ReplyDeleteif(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];

}

your logic of finding 2nd largest no. is not always right.

Deletehere if a[0] is bigest then it will not work

Woops, got some typos there.

ReplyDeletemin=num[0];

for(i=1;inum[i])

min=num[i];

}

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

ReplyDeleteKindly 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 =)

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

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

to find nthbiggest... pseudo code...

ReplyDeletecorrect 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;

}

but how do we create the algorithm

ReplyDeletethank u

ReplyDeletevery good

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

ReplyDeleteUse the technique of bubble sorting

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

}

with respect to the code for finding out the second highest number, correct output is not being given for the following set of inputs:

ReplyDelete4,4,3..Please help!

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

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

ReplyDeleteguys how to do it without sorting the array

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

ReplyDeleteThis comment has been removed by the author.

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

ReplyDeletesec_big=arr[0];

ReplyDeletefor(i=0;isec_big)&&(sec_big<big))

{

sec_big=arr[i];

}

}

This comment has been removed by the author.

ReplyDeleteThis comment has been removed by the author.

ReplyDelete#include

ReplyDeletemain()

{

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

}

#include

ReplyDeletemain()

{

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

it is nice

ReplyDeleteplease help me out to correct the programme

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

}

This comment has been removed by the author.

DeleteProgram as asked by you ,position is started from 0

Delete#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);

}

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

ReplyDeleteAbove 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

ReplyDeletein the above program should be initilalised as

Deletesecondbig=a[0];//correct

not as

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

YOU well GEt correct output

in the above program should be initilalised as

ReplyDeletesecondbig=a[0];//correct

not as

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

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

Deletehow t find second largest in sorted array?

ReplyDeleteHEY 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.......

ReplyDeleteFIND OUT SECOND LARGEST NUMBER IN AN UNSORTED ARRAY USING C PROGRAM:

ReplyDeleteit gives an incorrect output for the folowing i/p:

i/p: 3 2 5 1 0

o/p: second smallest: 5

if you give i/p as

ReplyDelete3

4

5

1

2

then the output is 5

which is incorrect....please reply

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

ReplyDeletehi 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}

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

Delete#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);

}

This comment has been removed by the author.

ReplyDeletesorry the first one was wrong this is correct

ReplyDelete#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);

}

there should be

ReplyDeletei=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];

}