C++ program to find min max element in an array - coderforcoders

Search

Wednesday, 13 March 2019

C++ program to find min max element in an array

#include<iostream>
using namespace std;
class minmax{
public:
int i,min,max;
void findMinMax(int array[],int n)
{
min=array[i];
max=array[i];
for(i=0;i<n;i++)
{
if(array[i]<min)
min=array[i];
if(array[i]>max)
max=array[i];
}
cout<<"Minimum value in the array is "<<min<<endl;
cout<<"Maximum value in the array is "<<max<<endl;
}
};
int main()
{
minmax obj1;
int array[100],n,j;
cout<<"Please enter the length of the array\n";
cin>>n;
cout<<"Please enter array elements\n";
for(j=0;j<n;j++)
cin>>array[j];
obj1.findMinMax(array,n);
cout<<endl;
return 0;
}


Output 1:
Please enter the length of the array
5
Please enter array elements
1 2 3 4 5
Minimum value in the array is 1
Maximum value in the array is 5

Output 2:
Please enter the length of the array
5
Please enter array elements
5 4 3 2 1
Minimum value in the array is 1
Maximum value in the array is 5

Output 3:
Please enter the length of the array
5
Please enter array elements
43 23 7 89 34
Minimum value in the array is 7
Maximum value in the array is 89



No comments:

Post a Comment