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.
std::vector, rather than a C style array. Use Erase-Remove idiom.int *array = new int[n];for(i=0;i<n-1;i++)loop is wrong should befor(k=i;k<n-1;k++)