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

(Refer to the directions area to see what the assignment says. The rest of this

ID: 3586722 • Letter: #

Question

(Refer to the directions area to see what the assignment says. The rest of this information are just notes to help you with the coding in C++ by the way.)

[Map]

The map function takes as arguments a function pointer and a integer vector pointer. It applies the function to every element in the vector, storing the results in order in a new vector. It then returns a pointer to a new vector. You should pass your square function into map to test it.

Sample input: {1, 3, 5, 7, 9}

Expected output: {1, 9, 25, 49, 81}

[Filter]

The filter function takes as arguments a function pointer and a integer vector pointer. It tests every element in the vector against the function, storing in a new vector those elements for which the function returns true. It then returns a pointer to the new vector. You should pass your isEven function into filter to test it.

Sample input: {1, 2, 3, 4, 5}

Expected output: {2, 4}

[Reduce]

The reduce function takes as arguments a function pointer and a integer vector pointer. The reduce function reduces a vector down to a single value by passing a running total and next vector element to the function, then storing the results as the new total. It does this for every element of the vector.

reduce(fxn, array)

    acc = array[0]

    for i in array[1…n]:

      acc = fxn(acc,i)

    return acc

Sample input: {1, 2, 3, 4}

Expected output: 24

You should pass your product function into reduce to test it.

*Minor Functions*

[square] – takes an integer and returns its square

[isEven] – takes an integer and returns true if even, otherwise false

[product] – takes two integers and returns their product.

Directions:

Function declarations for all six functions are at the top of the main file. Your assignment is to create the bodies for all six functions. Six functions had unit tests already created(hidden). The unit tests function independently of the main method and system output, so you are free to use the main method and cout << for your own development, testing, and debugging.

NB: You should never modify the vector passed into the map, filter, and reduce methods. Treat them as read only.

Code given:

#include <iostream>

#include <vector>

using namespace std;

vector * map ( int (*fxn) (int), vector * vec );

vector * filter ( bool (*fxn) (int), vector * vec );

int reduce ( int (*fxn) (int, int), vector * vec );

int square (int);       // For use with map

bool isEven (int);      // For use with filter

int product (int,int); // For use with reduce

bool testPassed();

int main ( ) {

            /* Use the main method for your own testing, debugging, etc */

}

Explanation / Answer

#include <iostream>

#include <vector>

using namespace std;

vector<int> * map(int(*fxn) (int), vector<int> * vec);

vector<int> * filter(bool(*fxn) (int), vector<int> * vec);

int reduce(int(*fxn) (int, int), vector<int> * vec);

int square(int); // For use with map

bool isEven(int); // For use with filter

int product(int, int); // For use with reduce

bool testPassed();

//written by cheggEA to print vector

void print_vector(vector<int> a);

int main()

{

/* Use the main method for your own testing, debugging, etc */

vector<int> *a;

vector<int> arr,ar;

arr.push_back(1);

arr.push_back(3);

arr.push_back(5);

arr.push_back(7);

arr.push_back(9);

//on more vector

ar.push_back(1);

ar.push_back(2);

ar.push_back(3);

ar.push_back(4);

a = map(square, &arr);

//print array after calling map function

cout << "After calling map function : " << endl;

cout << "Original vctor: ";

print_vector(arr);

cout << "Modified vctor: ";

print_vector(*a);

cout << endl;

//now test filtr function

a = filter(isEven, &ar);

//print array after calling filter function

cout << "After calling filter function : " << endl;

cout << "Original vctor: ";

print_vector(ar);

cout << "Modified vctor: ";

print_vector(*a);

cout << endl;

//now test for reduce function

int total = reduce(product, &ar);

//print array after calling reduce function

cout << "After calling reduce function : " << endl;

cout << "Original vctor: ";

print_vector(ar);

cout << "Total = " << total << endl;

cout << endl;

}

int total(int tot, int a)

{

return tot + a;

}

void print_vector(vector<int> a)

{

for (int i = 0; i < a.size(); i++)

{

cout << a[i] << " ";

}

cout << endl;

}

vector<int> * map(int(*fxn) (int), vector<int> * vec)

{

int a;

vector<int> *arr1 = new vector<int>;

for (int i = 0; i < vec->size(); i++)

{

a = fxn((*vec).at(i));

(*arr1).push_back(a);

}

return arr1;

}

vector<int> * filter(bool(*fxn) (int), vector<int> * vec)

{

vector<int> *arr1 = new vector<int>;

for (int i = 0; i < vec->size(); i++)

{

bool ret = fxn((*vec)[i]);

if (ret)

(*arr1).push_back((*vec).at(i));

}

return arr1;

}

int reduce(int(*fxn) (int a, int b), vector<int> * arr)

{

vector<int> *arr1 = new vector<int>;

int acc = (*arr)[0];

for (int i = 0; i < (*arr).size(); i++)

{

if (i + 1 < (*arr).size())

acc = fxn(acc,(*arr)[i+1]);

}

return acc;

}

int square(int a) // For use with map

{

return a*a;

}

bool isEven(int a) // For use with filter

{

if (a % 2 == 0)

return true;

else

return false;

}

int product(int a, int b) // For use with reduce

{

return a*b;

}

bool testPassed()

{

return true;

}

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

//output

After calling map function :
Original vctor: 1 3 5 7 9
Modified vctor: 1 9 25 49 81

After calling filter function :
Original vctor: 1 2 3 4
Modified vctor: 2 4

After calling reduce function :
Original vctor: 1 2 3 4
Total = 24