简体   繁体   中英

List minimum and maximum value in array

I need to list down the minimum and maximum value from array. For example I have 10 arrays, so I need to list down the 5 minimum and 5 maximum from the array. I want make my program can do this.. example

{1.2, 3.5, 4.5, 8.9, 2.4, 6.5, 7.8, 3.3, 6.5, 9.5} {1.2 < 3.5, 4.5 < 8.9, 2.4 < 6.5, 7.8 < 3.3, 6.5 < 9.5}

If the number is small then win Win: 1.2 4.5 2.4 3.3 6.5 Lose: 3.5 8.9 6.5 7.8 9.5

I've already tried many loop style and boolean in code.

#include <iostream>
using namespace std;

int main(){
    //double arr[2] = {1.2, 3.5};
    double arr[10] = {1.2, 3.5, 4.5, 8.9, 2.4, 6.5, 7.8, 3.3, 6.5, 9.5};
    double min = arr[0];
    double max = arr[0];
    double Sdata[5] = {0};
    double Bdata[5] = {0};
    int counterMin = 0;
    int counterMax = 0;

    for(int i = 1; i<10; i++){

        if(arr[i]<max || arr[i]> max){
            max = arr[i];
            if(counterMax<5){
                Bdata[counterMax] = max;
                counterMax++;
            }
            max = arr[i+1];
        }

        if(arr[i]<min || arr[i]> min){
            //min = arr[i];
            if(counterMin<5){
                Sdata[counterMin] = min;
                counterMin++;
            }
            min = arr[i+1];
        }



    }

    //Output
    cout<<"Minimum: "<<endl;
    for(int w = 0; w<5; w++){
        cout<<Sdata[w]<<" ";
    }

    cout<<endl;

    cout<<"Maximum: "<<endl;

    for(int j = 0; j<5; j++){
        cout<<Bdata[j]<<" ";
    }

    return 0;
}


Output from the program is:
Minimum:
1.2 4.5 2.4 7.8 6.5
Maximum:
3.5 8.9 6.5 3.3 9.5

I expect the output from the program is:
Minimum:
1.2 4.5 2.4 3.3 6.5
Maximum:
3.5 8.9 6.5 7.8 9.5

C++ actually offers algorithms that help you find the minimum and maximum elements in a container/array:

#include <algorithm>
#include <iterator>
#include <iostream>

int main()
{
  const double arr[] = {1.2, 3.5, 4.5, 8.9, 2.4, 6.5, 7.8, 3.3, 6.5, 9.5};
  auto [min, max] = std::minmax_element(std::begin(arr), std::end(arr));
  std::cout << "Min: " << *min << "\n";
  std::cout << "Max: " << *max << "\n";
}

You can use std::min_element() , std::max_element() in case you only need one of them.

First of, as the question is tagged as C++, I think you should be better of just using std::vector instead of arrays.
Aside from that, if you'd want to sort the array pair wise you can use something like this.

#include <vector>

int main()
{
    std::vector<double> arr{1.2, 3.5, 4.5, 8.9, 2.4, 6.5, 7.8, 3.3, 6.5, 9.5};
    std::vector<double> maxs(arr.size()/2);
    std::vector<double> mins(arr.size()/2);
    double max, min;

    for (unsigned i = 0; i < arr.size()/2; ++i)
    {
        min = arr[2*i];
        max = arr[2*i + 1];

        if (min > max) std::swap(min, max);

        maxs[i] = max;
        mins[i] = min;
    }

    return 0;
}



This is just a simple snippet, without error/bound checks etc...
You could also check if your original vector contains an even number of elements, and then go on accordingly.

If I didn't understand something correctly or made any mistakes, feel free to correct me!

I suggest to use std::minmax_element . It does not need to iterate twice over the array as a approach with std::min_element and std::max_element would need to.

Moreover, I suggest to use std::array instead of POD C array

#include <algorithm>
#include <array>
#include <iterator>
#include <iostream>

int main() {
   std::array<double,10> arr {1.2, 3.5, 4.5, 8.9, 2.4, 6.5, 7.8, 3.3, 6.5, 9.5};
   const auto [min, max] = std::minmax_element(std::begin(arr), std::end(arr));
   std::cout << "min = " << *min << ", max = " << *max << '\n';
}

If all you want to do is list the min/max pair of adjacent numbers, then the following is all that needs to be done:

#include <iostream>
#include <algorithm>

int main()
{
    double arr[10] = { 1.2, 3.5, 4.5, 8.9, 2.4, 6.5, 7.8, 3.3, 6.5, 9.5 };
    const int sz = (sizeof(arr) / sizeof(arr[0])); // number of elements
    const int outsize = sz / 2; // number of elements in output array 
    double Sdata[outsize] = { 0 };
    double Bdata[outsize] = { 0 };

    // loop for each pair
    for (int i = 0; i < sz - 1; i += 2) 
    {
            // minimum in Sdata, maximum in Bdata
        Sdata[i / 2] = std::min(arr[i], arr[i + 1]);
        Bdata[i / 2] = std::max(arr[i], arr[i + 1]);
    }

    //Output
    std::cout << "Minimum: \n";
    for (int w = 0; w < 5; w++) {
        std::cout << Sdata[w] << " ";
    }

    std::cout << "\n";
    std::cout << "Maximum: \n";

    for (int j = 0; j < 5; j++) {
        std::cout << Bdata[j] << " ";
    }
}

Output:

Minimum: 
1.2 4.5 2.4 3.3 6.5 
Maximum: 
3.5 8.9 6.5 7.8 9.5

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