## INDEX

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

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

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

2. #include
using namespace std;
int main()
{
int array[100];
int s2ndmax;
int y=0;
cout<<"enter the element of array"<>x;
}
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;
}

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

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

3. 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];
}

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

4. Paul SIIT 207/13/11, 12:12 AM

Woops, got some typos there.

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

5. Paul SIIT 207/13/11, 12:16 AM

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

6. 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 =)

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

8. 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;
}

9. but how do we create the algorithm

10. thank u

11. very good

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

13. 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]);
}

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

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

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

17. guys how to do it without sorting the array

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

19. This comment has been removed by the author.

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

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

22. This comment has been removed by the author.

23. This comment has been removed by the author.

24. #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);

}

25. #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

26. it is nice

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
{
}
getch();
}

1. This comment has been removed by the author.

2. 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)
else
printf("\nnumber is found at %d position",i);
}

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

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

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

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

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

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

31. how t find second largest in sorted array?

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

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

34. if you give i/p as
3
4
5
1
2
then the output is 5

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

36. 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}

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

}

37. This comment has been removed by the author.

38. 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);
}

39. 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];
}

40. 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;
}

41. #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

42. This comment has been removed by the author.

43. This comment has been removed by the author.

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

45. can anyone help me out in solving this...

46. #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);

}

47. can anyone tell me the code to print the following structure

1
3 5
7 9 11

1. #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");
}
}

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

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

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

1. #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;
}

51. Thanks a lot! I appreciate your help.

52. 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);
}}

53. 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);
}

54. #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;
}

1. This comment has been removed by the author.