Monday 10 February 2020

CPP program to find maximum element of an array with less time complexity

Source Code:
#include<iostream>
using namespace std;
int main()
{
int a[10],n,i;
cout<<"Enter the no.of elements you wish to store in an array: ";
cin>>n;
for(i=0;i<n;i++)
{
cout<<"Enter element"<<" "<<(i+1)<<":";
cin>>a[i];
}
for(i=1;i<n;i++)
{
if(a[0]<a[i])
{
a[0]=a[i];
}
}

cout<<"Therefore the maximum element of the array is:  "<<a[0];
}

Program Output:

No comments:

Post a Comment