Probelm statement: write a function removeall that takes three parameters an arr
ID: 3536044 • Letter: P
Question
Probelm statement: write a function removeall that takes three parameters an array of integer the number of elements in the array , and an integr (say,removeitem),the function shuld find and delete all occurrences of removeitem array if the values does't exist or the array is empty output an apprpriate message .assume the array unsorted
I wrote out almost all of the code, I'm just having trouble with outputing messages when the array is empty of the element that I want removed doesn;t exist. Help?
Code:
#include <cstdlib>
#include <iostream>
using namespace std;
int removeAll(int integers[], int NumberElements, int removeItem); //Declare function prototype
int main(int argc, char *argv[])
{
int integers[50]; //Decalre variables
int i;
int NumberElements;
int removeItem;
cout<<"Enter the number of elements: "; //Prompt the user to enter the number of elements
cin>>NumberElements;
cout<<"Enter the elements: "; //Prompt the user to enter the elements in the array
for(i=0;i<NumberElements;i++)
{
cin>>integers[i];
}
cout<<"Enter the element that you would like removed: "; //Prompt the user to enter the element to be removed
cin>>removeItem;
NumberElements=removeAll(integers,NumberElements,removeItem);
cout<<"Elements after removal: ";
cout<<removeAll(integers,NumberElements,removeItem)<<" ";; //Output the number of elements after removal
cout<<"New array of elements: ";
for(i=0;i<NumberElements;i++)
cout<<integers[i];
system("PAUSE");
return EXIT_SUCCESS;
}
int removeAll(int integers[],int NumberElements, int removeItem)
{
int loc;
int i;
bool found=false;
for(loc=0;loc<NumberElements;loc++)
{
found=false;
if(integers[loc]==removeItem)
{
found=true;
}
if(found)
{
{
for(i=loc;i<NumberElements;i++)
integers[i]=integers[i+1];
NumberElements=NumberElements-1;
}
}
}
return NumberElements;
}
Explanation / Answer
here is the code for you.
#include <cstdlib>
#include <iostream>
using namespace std;
int removeAll(int integers[], int NumberElements, int removeItem); //Declare function prototype
int main(int argc, char *argv[])
{
int integers[50]; //Decalre variables
int i;
int NumberElements;
int removeItem;
cout<<"Enter the number of elements: "; //Prompt the user to enter the number of elements
cin>>NumberElements;
cout<<"Enter the elements: "; //Prompt the user to enter the elements in the array
for(i=0;i<NumberElements;i++)
{
cin>>integers[i];
}
cout<<"Enter the element that you would like removed: "; //Prompt the user to enter the element to be removed
cin>>removeItem;
NumberElements=removeAll(integers,NumberElements,removeItem);
cout<<"Elements after removal: ";
cout<<NumberElements<<" ";; //Output the number of elements after removal
cout<<"New array of elements: ";
for(i=0;i<NumberElements;i++)
cout<<integers[i];
system("PAUSE");
return EXIT_SUCCESS;
}
int removeAll(int integers[],int NumberElements, int removeItem)
{
int loc;
int i;
bool found=false;
for(loc=0;loc<NumberElements;loc++)
{
found=false;
if(integers[loc]==removeItem)
{
found=true;
}
if(found)
{
{
for(i=loc;i<NumberElements;i++)
integers[i]=integers[i+1];
NumberElements=NumberElements-1;
}
}
}
if(!found)
{
cout<<"Element not found!! ";
}
return NumberElements;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.