Showing posts with label Largest and Smallest in Array. Show all posts
Showing posts with label Largest and Smallest in Array. Show all posts

Wednesday, July 6, 2011

Program to find the Largest and Smallest element in the given array


#include<iostream.h>
#include<conio.h>
void main()
{
  clrscr();
  int ar[100];
  int r,l,s,i;
  for(i=0;i<100;i++)
  {
    ar[i]=0;
  }
  cout<<"Enter the size of the array:(less than 100) "<<endl;
  cin>>r;
  cout<<"Enter the elements of the array:"<<endl;
  for(i=0;i<r;i++)
  {
    cout<<"Enter element number:"<<(i+1)<<endl;
    cin>>ar[i];
  }
  l=s=ar[0];
  for(i=1;i<r;i++)
  {
if(l<ar[i])
     l=ar[i];
if(s>ar[i])
     s=ar[i];
  }
  cout<<"The largest of the elements is: and the smallest of the elements is:"<<endl<<l<<endl<<s<<endl;
  getch();
}