Reputation: 3
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.
Upvotes: 0
Views: 1310
Reputation: 310930
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
Upvotes: 0
Reputation: 238311
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).
Upvotes: 2
Reputation: 105
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.
Upvotes: 1