The function should remove all the occurrences of an integer (say, removeItem) from an array. If the value does not exist or the array is empty, ,output an appropriate message.
My program is able to print the array without all the occurences of removeItem , but I don't know how to output appropriate message if the value doesn't exist or the array is empty. Any help will be greatly appreciated .

#include<iostream>

using namespace std;

const int ARRAY_SIZE = 10;

void removeAll(int list[], int ARRAY_SIZE, int removeItem);

int main()
{
     int list[ARRAY_SIZE];
     int index, removeItem;
     
     cout << "Enter " << ARRAY_SIZE << " integers:" << endl;
     for(index= 0; index < ARRAY_SIZE; index++)
         cin >> list[index];
     cin.ignore(100, '\n');
      
     cout << "Enter the number to be removed: " << endl;
     cin >> removeItem;         
       
     cout << "After removing " << removeItem 
          << " the list is: " << endl;  
     
     removeAll(list, ARRAY_SIZE, removeItem);                
             
     cout << endl;
     system("PAUSE");
     return 0;
}

void removeAll(int list[], int ARRAY_SIZE, int removeItem)
{
     int loc;
     
     for(loc = 0; loc < ARRAY_SIZE; loc++)
         if(list[loc] != removeItem)
            cout << list[loc] << " ";   
}

Recommended Answers

All 2 Replies

1. You don't remove items from an array, you print all elements except removeItem. It's a solution of another problem.
2. Count removeItem occurences then print a proper message(s) if counter == 0 or counter == ARRAY_SIZE. Let removeAll returns this counter value or a new number of array elements (ARRAY_SIZE - counter).

Thanks ArkM. Your suggestion is very helpful. I have changed my program and it works now.
Here is my updated program. I hope it will be helpful to others as well.

#include<iostream>

using namespace std;

const int ARRAY_SIZE = 10;

int removeAll(int list[], int ARRAY_SIZE, int removeItem);

int main()
{
     int list[ARRAY_SIZE];
     int counter, index, removeItem;
     
     cout << "Enter " << ARRAY_SIZE << " integers:" << endl;
     for(index= 0; index < ARRAY_SIZE; index++)
         cin >> list[index];
     cin.ignore(100, '\n');
      
     cout << "Enter the number to be removed: " << endl;
     cin >> removeItem;         
     
     counter = removeAll(list, ARRAY_SIZE, removeItem);
     if(counter == 0)
       cout << "The number " << removeItem << " is not in the list. " << endl;
     else 
     {   
           cout << "After removing " << removeItem 
                << " the list is: " << endl;  
           
           for(index = 0; index < ARRAY_SIZE; index++)
           {   
              if(list[index] != removeItem)
              cout << list[index] << " ";    
           }  
      }               
             
     cout << endl;
     system("PAUSE");
     return 0;
}

int removeAll(int list[], int ARRAY_SIZE, int removeItem)
{
    int counter = 0;
    int loc;
     
     for(loc = 0; loc < ARRAY_SIZE; loc++)
         if(list[loc] == removeItem)
            counter++;
     return counter;          
    
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.