cout<< *max_element(arr.begin(), arr.end());
cout<< *min_element(arr.begin(), arr.end());
#include <iostream>
using namespace std;
//How to find max element in C++
int main() {
int n;
cout <<"How many element yo will enter?"<<endl;
cin >> n;
int arr[n];
for(int i=0;i<n;i++){
cout << "Your "<< i+1 <<" number : ";
cin >> arr[i];
}
for(int i=0;i<n;i++){
if(arr[0]<arr[i]){
arr[0]=arr[i];
}
}
cout << "The max element is :"<<arr[0];
return 0;
}
class findMaxMinFromArray
{
public:
//! Find the maximum element from an array
void arrayInput(int arr[], int size)
{
arr[size] = {0};
for (int i = 0; i < size; i++)
{
cin >> arr[i];
}
}
int getMaxUsingSort(int arr[], int size)
{
arrayInput(arr, size);
sort(arr, arr + size, greater<int>());
int max_element = arr[0];
return max_element;
}
void getMaxMin(int num[], int size)
{
arrayInput(num, size);
int max_num = INT32_MIN, min_num = INT32_MAX;
for (int i = 0; i < size; i++)
{
if (num[i] > max_num)
max_num = num[i];
if (num[i] < min_num)
min_num = num[i];
}
cout << "Max number of the array: " << max_num << endl;
cout << "Minimum number of the array: " << min_num << endl;
}
void getMaxMinUsingSTL(int num[], int size)
{
//* Using STL
arrayInput(num, size);
cout << "Maximum element: " << *max_element(num, num + size) << endl;
cout << "Minimum element: " << *min_element(num, num + size) << endl;
}
};
#include <iostream>
#include <climits>
#include <algorithm>
using namespace std;
int main()
{
int arr[] = { 4, 2, 1, 6, -8, 5 };
int min = INT_MAX, max = INT_MIN;
for (int i: arr)
{
if (i < min) {
min = i;
}
if (i > max) {
max = i;
}
}
std::cout << "The min element is " << min << std::endl;
std::cout << "The max element is " << max << std::endl;
return 0;
}
#include <iostream>
#include <time.h>
using namespace std;
int main()
{
double array[6];
double min=array[6];
double max=array[0];
int indexOfMin=0;
int indexOfMax=0;
srand (time(0));
for(int i=0;i<6;i++){
array[i] = rand()%30;
cout<<"Element "<<i<<": ";
cout<<array[i]<<endl;
if(array[i]>max){
max=array[i];
indexOfMax=i;
}
if(array[i]<min){
min=array[i];
indexOfMin=i;
}
}
cout<<"The minimum value is "<<min<<endl;
cout<<"The index of the minimum value is "<<indexOfMin<<endl;
cout<<"The maximum value is "<<max<<endl;
cout<<"The index of the maximum value is "<<indexOfMax<<endl;
}