I need to create an array, find the max value and min value in the array and create and cout a new array without those values. just new array without 1 minV and 1 maxV
int main()
{
setlocale(LC_ALL, "ru");
srand(time(NULL));
const int LENGTH = 10;
int array[LENGTH];
for (int i = 0; i < LENGTH; i )
{
array[i] = rand() % 19;
}
for (int i = 0; i < LENGTH; i )
{
cout << array[i] << " ";
}
cout << endl;
int maxV = array[0];
for (int i = 0; i < LENGTH; i )
{
if (array[i] > maxV)
{
maxV = array[i];
}
}
int minV = array[0];
for (int i = 0; i < LENGTH; i )
{
if (array[i] < minV)
{
minV = array[i];
}
}
cout << "maxV = " << maxV << "\nminV = " << minV << endl;
}
CodePudding user response:
You cant delete element from array , but if i understood you , you want to print a array without the max and min element.
// Online C compiler to run C program online
#include <iostream>
using namespace std;
int main()
{
setlocale(LC_ALL, "ru");
srand(time(NULL));
const int LENGTH = 10;
int array[LENGTH];
int res[LENGTH-2];
for (int i = 0; i < LENGTH; i )
{
array[i] = rand() % 19;
}
for (int i = 0; i < LENGTH; i )
{
cout << array[i] << " ";
}
cout << endl;
int maxV = array[0];
for (int i = 0; i < LENGTH; i )
{
if (array[i] > maxV)
{
maxV = array[i];
}
}
int minV = array[0];
for (int i = 0; i < LENGTH; i )
{
if (array[i] < minV)
{
minV = array[i];
}
}
bool maxFlag = true , minFlag = true;
for(int i = 0 ; i < LENGTH-2 ;i )
{
if(array[i] == maxV && maxFlag){
maxFlag= false;
--i;
continue;
}
if(array[i]== minV && minFlag){
minFlag = false;
--i;
continue;
}
res[i] = array[i];
}
cout << "maxV = " << maxV << "\nminV = " << minV << endl;
for (int i = 0; i < LENGTH-2; i )
{
cout<<res[i]<<" ";
}
}
CodePudding user response:
You can't delete elements from an array, as it has a fixed size. All you can do is shuffle elements around, overwrite elements, copy elements between arrays, etc.
For example:
int main()
{
setlocale(LC_ALL, "ru");
srand(time(NULL));
const int LENGTH = 10;
int array[LENGTH];
const int LENGTH2 = 8;
int array2[LENGTH2];
for (int i = 0; i < LENGTH; i )
{
array[i] = rand() % 19;
cout << array[i] << " ";
}
cout << endl;
int idx_maxV = 0;
for (int i = 1; i < LENGTH; i)
{
if (array[i] > array[idx_maxV])
idx_maxV = i;
}
int idx_minV = 0;
for (int i = 1; i < LENGTH; i)
{
if (array[i] < array[idx_minV])
idx_minV = i;
}
cout << "maxV = " << array[idx_maxV] << "\nminV = " << array[idx_minV] << endl;
for (int i = 0, j = 0; i < LENGTH; i)
{
if (i == idx_maxV)
idx_maxV = -1;
else if (i == idx_minV)
idx_minV = -1;
else
array2[j ] = array[i];
}
for (int i = 0; i < LENGTH2; i )
{
cout << array2[i] << " ";
}
cout << endl;
}
Alternatively:
int main()
{
setlocale(LC_ALL, "ru");
srand(time(NULL));
const int MAX_LENGTH = 10;
int array[MAX_LENGTH];
for (int i = 0; i < MAX_LENGTH; i )
{
array[i] = rand() % 19;
cout << array[i] << " ";
}
cout << endl;
int idx_maxV = 0;
for (int i = 1; i < MAX_LENGTH; i)
{
if (array[i] > array[idx_maxV])
idx_maxV = i;
}
int idx_minV = 0;
for (int i = 1; i < MAX_LENGTH; i)
{
if (array[i] < array[idx_minV])
idx_minV = i;
}
cout << "maxV = " << array[idx_maxV] << "\nminV = " << array[idx_minV] << endl;
int length = MAX_LENGTH;
for (int i = idx_maxV 1; i < length; i)
{
array[i-1] = array[i];
}
--length;
if (idx_minV > idx_maxV)
--idx_minV;
for (int i = idx_minV 1; i < length; i)
{
array[i-1] = array[i];
}
--length;
for (int i = 0; i < length; i )
{
cout << array[i] << " ";
}
cout << endl;
}