Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Buy a domain name - Register cheap domain names from $0.99 - Namecheap

Scalable Hosting That Grows With You

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Semrush - keyword research tool

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

40,026 questions

51,982 answers

573 users

How to find the min and max of an int array in C++

3 Answers

0 votes
#include <iostream>
#include <climits>

int main() {
    int arr[] = {3, 14, 4, 1, 5, 90, 2, 6, 89, 3, 7};
    int size = sizeof(arr) / sizeof(arr[0]);

    int min = INT_MAX;
    int max = INT_MIN;

    for (int i = 0; i < size; ++i) {
        if (arr[i] < min) {
            min = arr[i];
        }
        if (arr[i] > max) {
            max = arr[i];
        }
    }

    std::cout << "Minimum: " << min << std::endl;
    std::cout << "Maximum: " << max << std::endl;
}



/*
run:

Minimum: 1
Maximum: 90

*/

 



answered Jan 16, 2025 by avibootz
0 votes
#include <iostream>
#include <algorithm>

int main() {
    int arr[] = {3, 14, 4, 1, 5, 90, 2, 6, 89, 3, 7};
    int size = sizeof(arr) / sizeof(arr[0]);

    int min = *std::min_element(arr, arr + size);
    int max = *std::max_element(arr, arr + size);

    std::cout << "Minimum: " << min << std::endl;
    std::cout << "Maximum: " << max << std::endl;
}



/*
run:

Minimum: 1
Maximum: 90

*/

 



answered Jan 16, 2025 by avibootz
0 votes
#include <iostream>
#include <utility>

std::pair<int, int> findMinMax(int arr[], int size) {
    int min = arr[0];
    int max = arr[0];

    for (int i = 1; i < size; ++i) {
        if (arr[i] < min) {
            min = arr[i];
        }
        if (arr[i] > max) {
            max = arr[i];
        }
    }

    return std::make_pair(min, max);
}

int main() {
    int arr[] = {3, 14, 4, 1, 5, 90, 2, 6, 89, 3, 7};
    int size = sizeof(arr) / sizeof(arr[0]);

    std::pair<int, int> result = findMinMax(arr, size);

    std::cout << "Minimum: " << result.first << std::endl;
    std::cout << "Maximum: " << result.second << std::endl;
}



/*
run:

Minimum: 1
Maximum: 90

*/

 



answered Jan 16, 2025 by avibootz

Related questions

1 answer 92 views
1 answer 95 views
3 answers 135 views
1 answer 77 views
1 answer 84 views
1 answer 74 views
1 answer 73 views
...