Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write the function catReven() which concatenates (appends) all the even numbers

ID: 3709903 • Letter: W

Question

Write the function catReven() which concatenates (appends) all the even numbers of the
source array to the destination array, in reversed order.

/**

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 src[CAP] = {1, 2, 3, 4, 5, 6};

int dest[CAP] = {98, 45, 18, 72};

size_t dSize = 4;

cout << " (1) catReven(dest, dSize, CAP, src, 6)" << endl;

cout << " src->" << toString(src, 6) << endl;

cout << " dest before->" << toString(dest, dSize) << endl;

bool ok = catReven(dest, dSize, CAP, src, 6);

cout << " dest after->" << toString(dest, dSize) << ", return->" << ok << endl;

cout << " expected->[98, 45, 18, 72, 6, 4, 2], return->true"<< endl;

}

{

const int CAP = 5;

int src[CAP] = {2, 3, 4};

int dest[CAP] = {98, 45, 18, 72};

size_t dSize = 4;

cout << " (2) catReven(dest, dSize, CAP, src, 3) [FAIL, no room, CAP=5]" << endl;

cout << " src->" << toString(src, 3) << endl;

cout << " dest before->" << toString(dest, dSize) << endl;

bool ok = catReven(dest, dSize, CAP, src, 3);

cout << " dest after->" << toString(dest, dSize) << ", return->" << ok << endl;

cout << " expected->[98, 45, 18, 72], return->false"<< endl;

}

{

const int CAP = 5;

int src[CAP] = {};

int dest[CAP] = {98, 45, 18, 72};

size_t dSize = 4;

cout << " (3) catReven(dest, dSize, CAP, src, 0) [FAIL, src empty]" << endl;

cout << " src->" << toString(src, 0) << endl;

cout << " dest before->" << toString(dest, dSize) << endl;

bool ok = catReven(dest, dSize, CAP, src, 0);

cout << " dest after->" << toString(dest, dSize) << ", return->" << ok << endl;

cout << " expected->[98, 45, 18, 72], 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();

}

7 THE catReven PROBLEM Write the function catReven() which concatenates (appends) all the even numbers of the source array to the destination array, in reversed order. Here's a short example: int src[50]-1, 2, 3, 4, 5, 6]; int dest[50]98, 45, 18, 72); size t dSize - 4; bol o catReven(dest, dSize, 50, src, 6); As you can see the function takes 5 arguments: The destination array and its size, which may both be modified. .The destination capacity, the source array and the source size, which are not modified. In the example shown dSize would be changed to 7 and the destination array would contain (98, 45, 18, 72, 6, 4, 2). The function returns true if successful and false if the destination array does not have the space to add the new elements, or if the source array is empty

Explanation / Answer

#include <cstddef> // size_t for sizes and indexes

///////////////// WRITE YOUR FUNCTION BELOW THIS LINE ///////////////////////

bool catReven(int destArr[], size_t &dSize, const int capacity, int srcArr[], int srcSize)
{
int j=dSize;
//If source array is empty return false
if (srcSize == 0)
return false;
for(int i=0; i<=srcSize;i++)
{
//check whether current element in source array is even number, if it is append to destination array if it has capacity
if((srcArr[i]%2==0) && (j < capacity))
{
destArr[j]=srcArr[i];
j++;
}
//if the appending an element results exceeds the capacity of destination array return false
else if(j >= capacity)
return false;
}
j--; // gives total number of elements in the destination array after appending from source array
  
//to reverse the even numbers that are appended to the destination array
int start=dSize,end=j-1;
while (start < end)
{
int temp = destArr[start];
destArr[start] = destArr[end];
destArr[end] = temp;
start++;
end--;
}
  
dSize=j; //size of destination array after appending from source array
return true;

}

///////////////// 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 src[CAP] = {1, 2, 3, 4, 5, 6};

int dest[CAP] = {98, 45, 18, 72};

size_t dSize = 4;

cout << " (1) catReven(dest, dSize, CAP, src, 6)" << endl;

cout << " src->" << toString(src, 6) << endl;

cout << " dest before->" << toString(dest, dSize) << endl;

bool ok = catReven(dest, dSize, CAP, src, 6);

cout << " dest after->" << toString(dest, dSize) << ", return->" << ok << endl;

cout << " expected->[98, 45, 18, 72, 6, 4, 2], return->true"<< endl;

}

{

const int CAP = 5;

int src[CAP] = {2, 3, 4};

int dest[CAP] = {98, 45, 18, 72};

size_t dSize = 4;

cout << " (2) catReven(dest, dSize, CAP, src, 3) [FAIL, no room, CAP=5]" << endl;

cout << " src->" << toString(src, 3) << endl;

cout << " dest before->" << toString(dest, dSize) << endl;

bool ok = catReven(dest, dSize, CAP, src, 3);

cout << " dest after->" << toString(dest, dSize) << ", return->" << ok << endl;

cout << " expected->[98, 45, 18, 72], return->false"<< endl;

}

{

const int CAP = 5;

int src[CAP] = {};

int dest[CAP] = {98, 45, 18, 72};

size_t dSize = 4;

cout << " (3) catReven(dest, dSize, CAP, src, 0) [FAIL, src empty]" << endl;

cout << " src->" << toString(src, 0) << endl;

cout << " dest before->" << toString(dest, dSize) << endl;

bool ok = catReven(dest, dSize, CAP, src, 0);

cout << " dest after->" << toString(dest, dSize) << ", return->" << ok << endl;

cout << " expected->[98, 45, 18, 72], 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();

return 0;
}

Output:

-------------------------------------------------------------                                                                    

                                                                                                                                 

(1) catReven(dest, dSize, CAP, src, 6)                                                                                           

            src->[1, 2, 3, 4, 5, 6]                                                                                              

    dest before->[98, 45, 18, 72]                                                                                                

     dest after->[98, 45, 18, 72, 6, 4, 2], return->true                                                                         

       expected->[98, 45, 18, 72, 6, 4, 2], return->true                                                                         

                                                                                                                                 

(2) catReven(dest, dSize, CAP, src, 3) [FAIL, no room, CAP=5]                                                                    

            src->[2, 3, 4]                                                                                                       

    dest before->[98, 45, 18, 72]                                                                                                

     dest after->[98, 45, 18, 72], return->false                                                                                 

       expected->[98, 45, 18, 72], return->false                                                                                 

                                                                                                                                 

(3) catReven(dest, dSize, CAP, src, 0) [FAIL, src empty]                                                                         

            src->[]                                                                                                              

    dest before->[98, 45, 18, 72]                                                                                                

     dest after->[98, 45, 18, 72], return->false                                                                                 

       expected->[98, 45, 18, 72], return->false                                                                                 

                                                                                                                                 

--done--