简体   繁体   中英

Find the minimum and maximum of an array with N elements

I need to find min and max of an array with N elements. The fact is that my program is working but when I submit it on a website it gives me only 32 points out of 100 and I don't know what's wrong.

#include <iostream>

using namespace std;

int main() {
    int N,min,max;
    cin >> N;
    min = N;
    max = N;

    int i,x;
    for (i = 1; i <= N; ++i) {
        cin >> x;

        if ( x < min ) {
            min = x;
        }
        if (x > max) {
            max = x;
        }
    }
    cout << min <<" "<< max;
    return 0;
}

Your logic here

min = N;
max = N;

initializing them with N , is wrong. When you have the minimum number for example 0 in your user input, and your N is greater than that 0 , you never find your minimum. The same will happen for the maximum.

Initialize min with largest possible value of int and max with least possible value, like the following:

int min = std::numeric_limits<int>::max();
int max = std::numeric_limits<int>::min();

Suggestion - 1

As it looks like you do not want to save the user input to find mim and max you can use std::min and std::max functions as follows:

#include <iostream>
#include <limits>    //  std::numeric_limits<>
#include <algorithm> //  std::min, std::max

int main()
{
    // initialize like this
    int min = std::numeric_limits<int>::max();
    int max = std::numeric_limits<int>::min();
    int N;
    std::cin >> N;
    while (N--)
    {
        int x; std::cin >> x;
        min = std::min(x, min);  // use std::min
        max = std::max(x, max);  // use std::max
    }
    std::cout << min << " " << max;
    return 0;
}

Suggestion - 2

If you want to find the min-max of an already existing array , you might wanna consider use std::minmax_element instead.

#include <algorithm>   //  std::minmax_element
#include <iostream>
#include <vector>

int main()
{
    int N; std::cin >> N;
    std::vector<int> v(N);
    for(auto& element: v) std::cin >> element;
    // do something.....

    // to find min-max of the array
    auto result = std::minmax_element(v.begin(), v.end());
    std::cout << "min element is: " << *result.first << '\n';
    std::cout << "max element is: " << *result.second << '\n';
}

Side Note : Do not practice with std namespüace std; , why? see this post: Why is “using namespace std” considered bad practice?

As suggested by vivek_23 , Use first element as min and max:

#include <iostream>

using namespace std;

int main()
{
    int N,min,max;
    cin >> N;
    cin>>min;
    max = min; 

    int i,x;
    for (i = 1; i < N; ++i){
        cin >> x;

        if ( x < min ){
            min = x;
        }
        if (x > max){
            max = x;
        }
    }
    cout << min <<" "<< max;
    return 0;
}

Alternative solution: Add extra headers and use int max and min limits

#include <iostream>
#include <cmath>
#include <climits>

using namespace std;

int main()
{
    int N,min,max;
    cin >> N;
    min = INT_MAX; //take largest value
    max = INT_MIN; //take smallest value

    int i,x;
    for (i = 1; i <= N; ++i){
        cin >> x;

        if ( x < min ){
            min = x;
        }
        if (x > max){
            max = x;
        }
    }
    cout << min <<" "<< max;
    return 0;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM