424,256 Members | 1,237 Online
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,256 IT Pros & Developers. It's quick & easy.

# find out the largest and second largest number in an array

 P: 1 Expand|Select|Wrap|Line Numbers int a[10],i,l,s,n; l=0,s=0; printf("Enter the limit\n"); scanf("%d",&n); printf("Enter the array\n"); for(i=0;il) { s=l; l=a[i]; } printf("\nLargest no.is%d",l); printf("\nSecond Largest no.is%d",s); Nov 18 '11 #1
Share this Question
5 Replies

 Expert Mod 5K+ P: 8,916 You haven't asked a question Nov 18 '11 #2

 P: 7 Hey dear here is a program to find second greater and third greater no i made two functions to get second and third greater no: #include using namespace std; int sec(int x,int y) { int greater; if(x>y) greater=x; else greater=y; return greater; } int third(int x,int y,int z) { int m; m=sec(sec(x,y),z); return m; } int main() { int a,b,c,z,d; cin>>a; cin>>b; cin>>z; c=sec(a,b); cout<<"2nd greater : "<

 P: 1 @sunithasiraj: Program u posted will not work out for the following inputs : 1) limit = 5, values are 50, 40, 30, 20, 10. the output is 50, 50. 2) This will not find out the largest for Negative numbers too...( Limit = 5, values are -50 -40 -30 -20 -10 and the output is 0, 0) Feb 14 '12 #4

 P: 24 Here is the program which Outputs First and second largest number Expand|Select|Wrap|Line Numbers // max.cpp : Defines the entry point for the console application. //Finds second maximum number in the array.   #include "stdafx.h" #include using namespace std; void max(int [],int *,int *);            //Define a function max which gives maximun ans second maxium number.                         //Pass two int variables by-refernce and an array.   int _tmain(int argc, _TCHAR* argv[]) {     int max1,max2,i,a[5];            //Define three int variables and an array of your required size.     cout<<"enter array elements"<>a[i];     }     max(a,&max1,&max2);            //Call the function.           cout<<"\nMaximum number is"<*m1)     *m1=b[j];                //Maimum number is in *m1.     }     for(j=0;j<5;j++)            //Now find minimum nubmer.     {         if(b[j]*m2 && b[j]<*m1)             {     *m2=b[j];       }     }      }     Feb 18 '12 #5

 100+ P: 542 why not just sort the array....the largest will be the last array element and the second largest will be the second last element. if the array is a[200]then sort it with sort(a,a+200) which is defined in the header. a[199]and a[198] will be the largest and second largest resp. edit:providing last element NOT duplicated Mar 2 '12 #6

### Post your reply

Sign in to post your reply or Sign up for a free account.