Write the function removeN() which removes \"N\" elements from the array startin
ID: 3709902 • Letter: W
Question
Write the function removeN() which removes "N" elements from the array starting at a given
position.
/**
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 <cstddef> for size_t.
*/
#include <cstddef> // size_t for sizes and indexes
///////////////// WRITE YOUR FUNCTION BELOW THIS LINE ///////////////////////
// function here
///////////////// WRITE YOUR FUNCTION ABOVE THIS LINE ///////////////////////
// These are OK after the function
#include <iostream>
#include <sstream>
#include <string>
using namespace std;
string toString(const int a[], size_t size);
void studentTests()
{
cout << "Student testing. You may add more code in this function." << endl;
cout << "-------------------------------------------------------------" << endl;
cout << boolalpha;
{
const int CAP = 30;
int a[CAP] = {10, 54, 81, 45, 95, 25, 10, 95};
size_t size = 8;
cout << " (1) removeN(a, size, 3, 2) [remove 3 starting at 2]" << endl;
cout << " before->" << toString(a, size) << endl;
bool ok = removeN(a, size, 3, 2);
cout << " found->" << toString(a, size) << ", return->" << ok << endl;
cout << " expected->[10, 54, 25, 10, 95], return->true"<< endl;
}
{
const int CAP = 30;
int a[CAP] = {20, 10, 95};
size_t size = 3;
cout << " (2) removeN(a, size, 2, 1) [remove 2 starting at 1]" << endl;
cout << " before->" << toString(a, size) << endl;
bool ok = removeN(a, size, 2, 1);
cout << " found->" << toString(a, size) << ", return->" << ok << endl;
cout << " expected->[20], return->true"<< endl;
}
{
const int CAP = 30;
int a[CAP] = {20, 10, 95};
size_t size = 3;
cout << " (3) removeN(a, size, 3, 1) [remove 3 starting at 1, not enough]" << endl;
cout << " before->" << toString(a, size) << endl;
bool ok = removeN(a, size, 3, 1);
cout << " found->" << toString(a, size) << ", return->" << ok << endl;
cout << " expected->[20, 10, 95], return->false"<< endl;
}
cout << endl;
cout << "--done--" << endl;
}
string toString(const int a[], size_t size)
{
ostringstream out;
out << '[';
if (size > 0)
{
out << a[0];
for (size_t i = 1; i < size; i++)
out << ", " << a[i];
}
out << ']';
return out.str();
}
int main()
{
studentTests();
}
6 THE removeN PROBLEM Write the function removeN() which removes "N" elements from the array starting at a given position. Here is an example where we remove 3 elements, starting at position 2. int a[50] 10, 54, 81, 45, 95, 25, 10, 95}; size size - 8; bool ok removeN(a, size, 3, 2); t The function takes four parameters: the array and its effective size, which may both be modified, along with the number of elements to remove and the position to start removing. (Be careful with these, that you don't reverse them.) The function returns true if it succeeds and false otherwise. It can fail if there are not N elements remaining (measured from the starting position), or, if position is out of bounds.Explanation / Answer
ScreenShot
-----------------------------------------------------------------------------------------------------------------
Program
//Header files
#include <iostream>
#include <sstream>
#include <string>
using namespace std;
//Array removal function declaration
bool removeN(int a[], int size, int numOfElem, int start);
//Array printing function declaration
string toString(const int a[], size_t size);
//Student test method
void studentTests()
{
//Prompt display
cout << "Student testing. You may add more code in this function." << endl;
cout << "-------------------------------------------------------------" << endl;
cout << boolalpha;
{
//Array intialization
const int CAP = 30;
int a[CAP] = { 10, 54, 81, 45, 95, 25, 10, 95 };
size_t size = 8;
//Prompt the array removal message
cout << " (1) removeN(a, size, 3, 2) [remove 3 starting at 2]" << endl;
//Display array before removal
cout << " before->" << toString(a, size) << endl;
//Call removal function and display output
bool ok = removeN(a, size, 3, 2);
//display message found the element and expected array
cout << " found->" << toString(a, size) << ", return->" << ok << endl;
cout << " expected->[10, 54, 25, 10, 95], return->true" << endl;
}
//Next array check
{
const int CAP = 30;
int a[CAP] = { 20, 10, 95 };
size_t size = 3;
cout << " (2) removeN(a, size, 2, 1) [remove 2 starting at 1]" << endl;
cout << " before->" << toString(a, size) << endl;
bool ok = removeN(a, size, 2, 1);
cout << " found->" << toString(a, size) << ", return->" << ok << endl;
cout << " expected->[20], return->true" << endl;
}
//Next array check
{
const int CAP = 30;
int a[CAP] = { 20, 10, 95 };
size_t size = 3;
cout << " (3) removeN(a, size, 3, 1) [remove 3 starting at 1, not enough]" << endl;
cout << " before->" << toString(a, size) << endl;
bool ok = removeN(a, size, 3, 1);
cout << " found->" << toString(a, size) << ", return->" << ok << endl;
cout << " expected->[20, 10, 95], return->false" << endl;
}
//end message
cout << endl;
cout << "--done--" << endl;
}
//Display array function definition
string toString(const int a[], size_t size)
{
ostringstream out;
out << '[';
if (size > 0)
{
out << a[0];
for (size_t i = 1; i < size; i++)
out << ", " << a[i];
}
out << ']';
return out.str();
}
//Main method
int main()
{
studentTests();
}
//Removal method definition
bool removeN(int a[], int size, int numOfElem, int start) {
if ((start + numOfElem) <=size) {
return true;
}
else { return false; }
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.