find out the largest and second largest number in an array

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • sunithasiraj
    New Member
    • Nov 2011
    • 1

    find out the largest and second largest number in an array

    Code:
    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;i<n;i++)
    scanf("%d",&a[i]);
    for(i=0;i<n;i++)
    if(a[i]>l)
    {
    s=l;
    l=a[i];
    }
    printf("\nLargest no.is%d",l);
    printf("\nSecond Largest no.is%d",s);
    Last edited by Banfa; Nov 18 '11, 11:15 AM. Reason: Added code tags round the code
  • Banfa
    Recognized Expert Expert
    • Feb 2006
    • 9067

    #2
    You haven't asked a question

    Comment

    • yasirassassin
      New Member
      • Nov 2011
      • 7

      #3
      program to find second greater and third greater integer

      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<iostre am>
      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:))

      Comment

      • praveensundarpv
        New Member
        • Feb 2012
        • 1

        #4
        @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)

        Comment

        • Maraj
          New Member
          • Nov 2011
          • 24

          #5
          Here is the program which Outputs First and second largest number
          Code:
          // max.cpp : Defines the entry point for the console application.
          //Finds second maximum number in the array.
          
          #include "stdafx.h"
          #include<iostream>
          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"<<endl;	//Input value of array elements.
          	for(i=0;i<5;i++)			//Loop for input.
          	{
          	cin>>a[i];
          	}
          	max(a,&max1,&max2);			//Call the function.
          						
          
          
          	cout<<"\nMaximum number is"<<max1<<endl;//These lines will execute after function execution.
          	cout<<"\n 2nd maximum number is"<<max2<<endl;
          	return 0;
          }
           void max(int b[5],int *m1,int *m2)		//Function will receive address of variables and array.
           {	int j,min=b[0];				//Define an int min required to find second maximum number.
          	*m1=b[0];
          	
          	for(j=0;j<5;j++)			//First find maximum number.			
          	{
          	if(b[j]>*m1)
          	*m1=b[j];				//Maimum number is in *m1.
          	}
          	for(j=0;j<5;j++)			//Now find minimum nubmer.
          	{
          		if(b[j]<min)
          			min=b[j];		//Minimum nuber goes to min.
          	}
          	*m2=min;
          	cout<<"\nMinimum number is"<<min<<endl;
          
          	for(j=0;j<5;j++)			//Now here the logic for second maximum number.
          	{
          		
          						//If b[J] is greatar than *m2 and b[j] is less than *m1 which is maximum number
          						//then assign *m2 the value of element of array pointing currently.
          	if(b[j]>*m2 && b[j]<*m1)		
          	{
          	*m2=b[j];
          	
          	}
          	}
          	
          
           }

          Comment

          • whodgson
            Contributor
            • Jan 2007
            • 542

            #6
            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
            Last edited by whodgson; Mar 4 '12, 03:00 AM. Reason: to qualify last statement

            Comment

            Working...