423,851 Members | 1,358 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 423,851 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
  1. int a[10],i,l,s,n;
  2. l=0,s=0;
  3. printf("Enter the limit\n");
  4. scanf("%d",&n);
  5. printf("Enter the array\n");
  6. for(i=0;i<n;i++)
  7. scanf("%d",&a[i]);
  8. for(i=0;i<n;i++)
  9. if(a[i]>l)
  10. {
  11. s=l;
  12. l=a[i];
  13. }
  14. printf("\nLargest no.is%d",l);
  15. printf("\nSecond Largest no.is%d",s);
Nov 18 '11 #1
Share this Question
Share on Google+
5 Replies


Banfa
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<iostream>
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 : "<<c<<endl;

d=third(a,b,z);
cout<<"finally Third greater no is: "<<d<<endl;

system("pause");
return 0;
}
Hope it helps:))
Nov 18 '11 #3

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
  1. // max.cpp : Defines the entry point for the console application.
  2. //Finds second maximum number in the array.
  3.  
  4. #include "stdafx.h"
  5. #include<iostream>
  6. using namespace std;
  7. void max(int [],int *,int *);            //Define a function max which gives maximun ans second maxium number.
  8.                         //Pass two int variables by-refernce and an array.
  9.  
  10. int _tmain(int argc, _TCHAR* argv[])
  11. {
  12.     int max1,max2,i,a[5];            //Define three int variables and an array of your required size.
  13.     cout<<"enter array elements"<<endl;    //Input value of array elements.
  14.     for(i=0;i<5;i++)            //Loop for input.
  15.     {
  16.     cin>>a[i];
  17.     }
  18.     max(a,&max1,&max2);            //Call the function.
  19.  
  20.  
  21.  
  22.     cout<<"\nMaximum number is"<<max1<<endl;//These lines will execute after function execution.
  23.     cout<<"\n 2nd maximum number is"<<max2<<endl;
  24.     return 0;
  25. }
  26.  void max(int b[5],int *m1,int *m2)        //Function will receive address of variables and array.
  27.  {    int j,min=b[0];                //Define an int min required to find second maximum number.
  28.     *m1=b[0];
  29.  
  30.     for(j=0;j<5;j++)            //First find maximum number.            
  31.     {
  32.     if(b[j]>*m1)
  33.     *m1=b[j];                //Maimum number is in *m1.
  34.     }
  35.     for(j=0;j<5;j++)            //Now find minimum nubmer.
  36.     {
  37.         if(b[j]<min)
  38.             min=b[j];        //Minimum nuber goes to min.
  39.     }
  40.     *m2=min;
  41.     cout<<"\nMinimum number is"<<min<<endl;
  42.  
  43.     for(j=0;j<5;j++)            //Now here the logic for second maximum number.
  44.     {
  45.  
  46.                         //If b[J] is greatar than *m2 and b[j] is less than *m1 which is maximum number
  47.                         //then assign *m2 the value of element of array pointing currently.
  48.     if(b[j]>*m2 && b[j]<*m1)        
  49.     {
  50.     *m2=b[j];
  51.  
  52.     }
  53.     }
  54.  
  55.  
  56.  }
  57.  
  58.  
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 <algorithm> 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.