Question: Write the function removeIfFound() which removes all copies of a particular value from an array. The function returns the number of values removed. We are
Write the function removeIfFound() which removes all copies of a particular value from an array. The function returns the number of values removed.
We are currently working on Arrays and Algorithms and I would appreciate some help with the function and below the picture is the support file.
/**
CS 150 PARTIALLY FILLED ARRAYS
Follow the instructions on your handout to complete the
requested function. You may not use any library functions
or include any headers, except for
*/
#include
///////////////// WRITE YOUR FUNCTION BELOW THIS LINE ///////////////////////
// function here
///////////////// WRITE YOUR FUNCTION ABOVE THIS LINE ///////////////////////
// These are OK after the function
#include
#include
#include
using namespace std;
string toString(const int a[], size_t size);
void studentTests()
{
cout
cout
cout
const int CAP = 30;
size_t size = 0;
{
int a[CAP] = {10, 54, 81, 45, 95, 25, 10, 95};
size = 8;
cout
cout "
int removed = removeIfFound(a, size, 10);
cout " "
cout [54, 81, 45, 95, 25, 95], return->2"
}
{
int a[CAP] = {2, 1, 1, 1, 2};
size = 5;
cout
cout "
int removed = removeIfFound(a, size, 1);
cout " "
cout [2, 2], return->3"
}
{
int a[CAP] = {2, 1, 1, 1, 2};
size = 5;
cout
cout "
int removed = removeIfFound(a, size, 2);
cout " "
cout [1, 1, 1], return->2"
}
{
int a[CAP] = {2, 1, 1, 1, 2};
size = 5;
cout
cout "
int removed = removeIfFound(a, size, 3);
cout " "
cout [2, 1, 1, 1, 2], return->0"
}
cout
cout
}
string toString(const int a[], size_t size)
{
ostringstream out;
out
if (size > 0)
{
out
for (size_t i = 1; i
out
}
out
return out.str();
}
int main()
{
studentTests();
}
8 THE removeIfFound PROBLEM Write the function removeIfFound () which removes all copies of a particular value from an array. The function returns the number of values removed. Here's a short example: int al50]110, 54, 81, 45, 95, 25, 10, 95j; size t size- 8; int removed-removeIfFound(a, size, 10); The example shown here removes all copies of the value 10 from the array a. Both a and size are modified by the function. In this case, since there are two copies of 10, size is changed to 6 and the function returns 2. Be especially careful when writing your code that you don't skip values that are contiguous in the array. For instance, if you remove 2 from 11, 2, 2, 2, 1}, you should end up with (1, 1)
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
