Question
/** CS 150 PARTIALLY FILLED ARRAYS Follow the instructions on your handout to complete the requested function. You may not use any library functions or
/**
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 ///////////////////////
///////////////// 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
const size_t CAP = 30;
{
int a[CAP] = {72, 13, 32, 48, 29, 75, 68}; // insert in the middle
size_t size = 7;
cout
cout
cout "
bool result = insertTimes(a, size, 10, 5, 3, CAP);
cout "
"
cout [72, 13, 32, 48, 29, 10, 10, 10, 75, 68], result->true ";
}
{
int a[CAP] = {72, 13, 32, 48, 29, 75, 68}; // insert in the middle
size_t size = 7;
cout
cout
cout "
bool result = insertTimes(a, size, 10, 7, 3, CAP);
cout "
"
cout [72, 13, 32, 48, 29, 75, 68, 10, 10, 10], result->true ";
}
{
int a[CAP] = {72, 13, 32, 48, 29, 75, 68}; // insert in the middle
size_t size = 7;
cout
cout
cout "
bool result = insertTimes(a, size, 10, 8, 3, CAP);
cout "
"
cout [72, 13, 32, 48, 29, 75, 68], result->false ";
}
{
int a[CAP] = {72, 13, 32, 48, 29, 75, 68}; // insert in the middle
size_t size = 7;
cout
cout
cout "
bool result = insertTimes(a, size, 10, 5, 25, CAP);
cout "
"
cout [72, 13, 32, 48, 29, 75, 68], result->false ";
}
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();
}
4 THE insertTimes PROBLEM Write the function insertTimes() which inserts a given number at a given index for a given number of times. For instance, given the array and function call below, which inserts the value 100 into the array a, repeating it 4 times. The capacity of the array is 50. int a[50] 10, 20, 30, 40, 50); size_t size - 5; bool ok-insertTimes (a, size, 100, 2, 4, 50); Input-output params As you can see, the function takes 6 arguments: the array of int and the size of the array. Both of these may be modified. the value to insert (100), the position to insert at (2), the number of times to insert the value (4), and the capacity of the array (50) All of these should be type size t except for the value which is inserted into the array (and the array elements themselves), which should be of type int. The function returns true if the elements can be inserted and false if they cannot. The insertion could fail because the insert position appeared past the size (although it should be possible to insert immediately after the last element), or if adding the elements would exceed the capacity of the arrayStep by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started