Search This Blog

Saturday, 22 February 2014

finding two largest numbers in an array

/*
   ar[]={1,3,2,6,3,7,8,4}
   1st largest value is : 8
   2nd largest value is:7
*/


#include<iostream.h>
#include<conio.h>

void main()
{
  int n[10],i,max1,max2;
  cout<<"Enter the array elements:";
  for(i=0;i<5;i++)
    cin>>n[i];
  cout<<"Entered array is:";
  for(i=0;i<5;i++)
    cout<<n[i]<<"\t";
  max1=n[0];
  for(i=0;i<5;i++)
  {
    if(max1<n[i])
    max1=n[i];
  }
  cout<<"\n\n1st Largest number is:"<<max1;

  for(i=0;i<5;i++)
  {
    if(max1==n[i])
    n[i]=0;
  }
  max2=n[0];
  for(i=0;i<5;i++)
  {
    if(max2<n[i])
    max2=n[i];
  }
  cout<<"\n\n2nd Largest number is:"<<max2;
  getch();
}

Output


No comments:

Popular Posts