Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

how to delete an element from array

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;    
}  

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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]<<" ";
    }
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading