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

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 :: argsort c++ 
Cpp :: define for loop c++ 
Cpp :: decemal representation 
Cpp :: Calculating Function codeforces in c++ 
Cpp :: open url from dev cpp 
Cpp :: A Subtask Problem codechef solution in cpp 
Cpp :: why wont a function stop C++ 
Cpp :: c++ constructor inheritance 
Cpp :: private access specifier in c++ program 
Cpp :: Operatore ternario c++ 
Cpp :: CPPDEVELOPER 
Cpp :: PCL normal specific point 
Cpp :: entering char in int c++ avoid loop 
Cpp :: estimateaffine3d example c++ 
Cpp :: split 2d array into chunks in c++ 
Cpp :: distinct numbers cses 
Cpp :: cocos2d c++ linux 
Cpp :: semi colon in argument list c++ 
Cpp :: std remove example 
Cpp :: displaying m images m windows opencv c++ 
Cpp :: find number of 1s in a binary cv::mat image 
Cpp :: how to user input in string to open files in c++ 
Cpp :: program in c++ for simple interest rate 
Cpp :: c++ void pointer 
Cpp :: appdivind c++ stuctures 
Cpp :: Error: C++14 standard requested but CXX14 is not defined 
Cpp :: +++++++++ 
Cpp :: npm wasm 
Cpp :: Swift if...else Statement 
Cpp :: C++ bool 
ADD CONTENT
Topic
Content
Source link
Name
7+9 =