cout<< *max_element(arr.begin(), arr.end());
cout<< *min_element(arr.begin(), arr.end());
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;
}
Code Example |
---|
Cpp :: reverse c++ string |
Cpp :: typedef vector c++ |
Cpp :: c++ map loop through key value |
Cpp :: if even number c++ |
Cpp :: cpp bubble sort |
Cpp :: Appending a vector to a vector in C++ |
Cpp :: conditional operator in cpp |
Cpp :: how to split a string into words c++ |
Cpp :: 2d vector cpp |
Cpp :: c++ reverse integer |
Cpp :: appending int to string in cpp |
Cpp :: sort stl |
Cpp :: c++ construnctor |
Cpp :: how to install boost c++ on windows |
Cpp :: memcpy c++ usage |
Cpp :: C++ Swap 2 Variables Without Using 3rd Variable |
Cpp :: c++ typeid |
Cpp :: cpp init multidimensional vector |
Cpp :: how to do sets in cpp |
Cpp :: how to convert string into lowercase in cpp |
Cpp :: increment c++ |
Cpp :: index string c++ |
Cpp :: continue c++ |
Cpp :: cin.getline |
Cpp :: c++ array rev pointer |
Cpp :: sort vector struct c++ |
Cpp :: sizeof operator in c++ |
Cpp :: min heap priority queue c++ |
Cpp :: pragma cpp |
Cpp :: erase element from vector c++ |