Search
 
SCRIPT & CODE EXAMPLE
 

CPP

what is the short cut way to find the max and min element in an array in c++

cout<< *max_element(arr.begin(), arr.end());

cout<< *min_element(arr.begin(), arr.end());
Comment

max element in array c++ stl

*max_element (first_index, last_index);
ex:- for an array arr of size n
*max_element(arr, arr + n);
Comment

c++ max of array

cout << " max element is: " << *max_element(array , array + n) << endl;
Comment

find max element in array c++

#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;
}
Comment

array max and minimum element c++

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;
    }
};
Comment

minimum or maximum in array c++

#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;
}
Comment

minimum or maximum in array c++

#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;
}

Comment

PREVIOUS NEXT
Code Example
Cpp :: iterate vector in reverse c++ 
Cpp :: index string c++ 
Cpp :: c++ default parameters 
Cpp :: Xor implementation C++ 
Cpp :: c++ get type name of object 
Cpp :: calloc c++ 
Cpp :: size of array 
Cpp :: c++ vectors 
Cpp :: naive pattern matching algorithm 
Cpp :: string to uint64_t c++ 
Cpp :: convert unsigned long to string c++ 
Cpp :: remove from vector by value c++ 
Cpp :: loop through array c++ 
Cpp :: sort a vector c++ 
Cpp :: overload of << c++ 
Cpp :: how to reverse a string in c++ 
Cpp :: remove space in string c++ 
Cpp :: c++ get line 
Cpp :: cpp return array 
Cpp :: erase element from vector c++ 
Cpp :: c pre-processor instructions 
Cpp :: how to make Dijkstra in c++ 
Cpp :: sum of a matrix c++ 
Cpp :: how to print in new lines in C++ 
Cpp :: c++ convert const char* to int 
Cpp :: vector iterating in c++ 
Cpp :: c++ client service ros 
Cpp :: is anagram c++ 
Cpp :: C++ sum a vector of digits 
Cpp :: c++ fonksion pointer 
ADD CONTENT
Topic
Content
Source link
Name
8+7 =