0

I need to delete all minimum and maximum values from an array, but I cannot figure out how to do it.

First I create an array, consisting of 5 numbers, for example. Then I enter the numbers, say, 1, 2, 3, 4, 5. Then I find the minimum and maximum values, which are 1 and 5, simple. Then I need to delete them and this is where I am stuck.

Here is the code: (second to last for cycle is the relevant part)

int main()
    {
        int i;
        int n;
        int min=999999;
        int max=-999999;

        cout<<"Enter how many elements there will be in the array"<<endl;
        cin>>n;

        int array[n];

        for(i=0;i<n;i++)
        {
        cout<<"Enter element "<<i+1<<endl;
        cin>>array[i];
        }

        for(i=0;i<n;i++)
        {
            if(array[i]<min)
            {
                min=array[i];
            }
            if(array[i]>max)
            {
                max=array[i];
            }
        }

        for(i=0;i<n-1;i++)
        {

            if(array[i]==min||array[i]==max)
            {
                for(i=0;i<n-1;i++)
                {
                array[i]=array[i+1];
                }
                array[n-1]=0;
                n--;
            }
        }
        for(i=0;i<n;i++)
                {
                    cout<<"Array after min and max values deleted: "<<array[i]<<endl;
                }

        return 0;
    }

What I'm thinking is it should go through the array with a for cycle and if it finds either min or max value, it should delete it, but for some reason it doesn't work that way. It only deletes the first value and that's it. Any help would be appreciated.

6
  • 2
    Use a std::vector, rather than a C style array. Use Erase-Remove idiom. Commented Nov 15, 2019 at 17:42
  • ` int array[n];` is wrong, should be int *array = new int[n]; Commented Nov 15, 2019 at 17:44
  • 2
    You can't delete an item in an array. You can overwrite it. Overwriting an element in an array doesn't change the capacity of the array. Commented Nov 15, 2019 at 17:44
  • also for(i=0;i<n-1;i++) loop is wrong should be for(k=i;k<n-1;k++) Commented Nov 15, 2019 at 17:46
  • What is "k" here? Commented Nov 15, 2019 at 17:47

3 Answers 3

2

It is not possible to delete value from an array. An array has a constant number of elements throughout its lifetime.

What you can do, is shift around elements so that the important values are at the beginning. For example, your array could contain [2, 3, 4, x, y] where x and y are some values that you don't care about.

There are standard algorithm to do this: std::remove, and std::remove_if. Example:

auto is_min_or_max = [=](int val) {
    return val == min
        || val == max;
};
std::remove_if(array, array + n, is_min_or_max);

Furthermore, there is standard algorithm for finding the minimum and maximum values in an array as well: std::minmax_element.

P.S. array[n] is ill-formed because n is not a compile time constant. You need to allocate the array dynamically in order to have a runtime size. I recommend using std::vector<int>. Using vector, it is also possible to erase the removed elements (the memory is not deallocated, but std::vector::size reports the reduced size).

Sign up to request clarification or add additional context in comments.

2 Comments

Yes, that is what I meant by "delete". Perhaps I worded it wrong. I am not familiar with std::remove, and std::remove_if, but I'll try Google to look for some examples of how they work.
@grt.dan I added an example.
1

There's two ways you can go about this. The easy way or the hard way. The easy way would be to use a std::vector<int> instead of an array since then you can resize your vector. The other way would be to create a new array every time you delete an item of n-1 size and copy all the old values into the resized array.

2 Comments

there is no need to create a new array every time an item is removed. It would be sufficient to create a new array once
there is no need to create a new array every time an item is removed Think of how std::vector is implemented with size and capacity. These need not be the same and thus you need not allocate or free each time you need to add or remove an item from a vector.
0

For starters variable length arrays is not a standard C++ feature.

Either use an array with a fixed size and ask the user to specify the array size not greater than the fixed size of the array or use the standard class template std::vector.

To found the minimum and the maximum values in an array or vector use the standard algorithm std::minmax_element. To remove elements that are equal to the minimum or maximum use the standard algorithm std::remove_if. You can remove element from an array but the algorithm shifts the actual elements to the beginner of the array and returns pointer to the element after the last actual element.

Here is a demonstrative program

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


int main() 
{
    int a[] = { 1, 2, 3, 4, 5 };

    auto minmax = std::minmax_element( std::begin( a ), std::end( a ) );

    auto it = std::remove_if( std::begin( a ), std::end( a ),
                              [&minmax]( const int &item )
                              {
                                return item == *minmax.first or item == *minmax.second;
                              } );

    size_t n = std::distance( std::begin( a ), it );

    for ( size_t i = 0; i < n; i++ )
    {
        std::cout << a[i] << ' ';
    }

    std::cout << '\n';

    std::vector<int> v;

    std::cout << "Enter how many elements there will be in the vector: ";

    size_t size;

    std::cin >> size;

    v.resize( size );

    for ( std::vector<int>::size_type i = 0; i < v.size(); i++ )
    {
        std::cout << "Enter element " << i + 1 << ": ";

        std::cin >> v[i];
    }

    auto minmax2 = std::minmax_element( std::begin( v ), std::end( v ) );

    v.erase( std::remove_if( std::begin( v ), std::end( v ),
                             [&minmax2]( const int &item )
                             {
                                return item == *minmax2.first or item == *minmax2.second; 
                             } ), std::end( v ) );

    for ( const auto &item : v )
    {
        std::cout << item << ' ';
    }

    std::cout << '\n';

    return 0;
}

Its output might look like

2 3 4 
Enter how many elements there will be in the vector: 5
Enter element 1: 1
Enter element 2: 2
Enter element 3: 3
Enter element 4: 4
Enter element 5: 5
2 3 4 

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.