cout<< *max_element(arr.begin(), arr.end());
cout<< *min_element(arr.begin(), arr.end());
#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;
}
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 :: c language all keywords in string |
Cpp :: valid parentheses in c++ |
Cpp :: memcpy in cpp |
Cpp :: insertion overloading in c++ |
Cpp :: converting int to string c++ |
Cpp :: enum in c++ |
Cpp :: flutter text direction auto |
Cpp :: c++ function overloading |
Cpp :: compile and run cpp file on mac c++ |
Cpp :: pause the console c++ |
Cpp :: c++ stl |
Cpp :: c++ add everything in a vector |
Cpp :: vsearch c program stdlib |
Cpp :: c++ void poiinter |
Cpp :: c++ string replace |
Cpp :: how to do if command in c++ |
Cpp :: uint16_t in c++ |
Cpp :: vector int initialize with increasing numbers |
Cpp :: error c4001 site:docs.microsoft.com |
Cpp :: convert string to double arduino |
Cpp :: c++ stoi binary negative number string to decimal |
Cpp :: c++ if |
Cpp :: c++ over load oprator to print variable of clas |
Cpp :: nothrow new in cpp |
Cpp :: Hash Sort in C++ |
Cpp :: assegnare valori in c++ |
Cpp :: deliberation meaning |
Cpp :: for llop in c++ |
Cpp :: 28+152+28+38+114 |
Cpp :: c ++ loop |