Assignments » Single Dimension Array » Set1 »Solution 4

Write a C++ program to find the largest and smallest element of an array.

#include<iostream>
using namespace std;

int main()
{
	int Arr[100],n,i,small,large;

	cout<<"Enter number of elements you want to insert ";
	cin>>n;

	for(i=0;i<n;i++)
	{
		cout<<"Enter element "<<i+1<<":";
		cin>>Arr[i];
	}

	small=Arr[0];
	large=Arr[0];

	for(i=1;i<n;i++)
	{
		if(Arr[i]<small)
			small=Arr[i];
		if(Arr[i]>large)
			large=Arr[i];
	}

	cout<<"\nLargest element is :"<<large;
	cout<<"\nSmallest element is :"<<small;
    
	return 0;
}

Enter number of elements you want to insert 5
Enter element 1: 13
Enter element 2: 69
Enter element 3: 30
Enter element 4: 51
Enter element 5: 11

Largest element is : 69
Smallest element is : 11