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

Map, Filter, and Reduce are three functions commonly used in functional programm

ID: 3873229 • Letter: M

Question

Map, Filter, and Reduce are three functions commonly used in functional programming. For this assignment you will be implementing all three, along with three minor functions that can be passed to them.

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.

Sample Input { 1, 2, 3, 4 }

Expected Output 24

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

Minor Functions

You will also need to implement these three additional functions to be used with map, filter, and reduce respectively.

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

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

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

#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();


int main ( ) {

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

}

Explanation / Answer

Solution====================

#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();

void test(int (*fxn) (int)){
}


int main(int argc, char** argv)
{
   vector<int> in;
   in.push_back(10);
   in.push_back(2);
   
   
   
   
   vector<int> *res= map(square,&in);
   int size = res->size();
   for(int i=0;i<size;i++){
   cout<<res->at(i)<<" ";
   }

   return 0;
}

int square(int x){
   return x*x;
}

bool isEven(int x){
   if(x%2==0) return true;
   return false;
}

int product(int x, int y){
   return x*y;
}

vector<int> * map ( int (*fxn) (int), vector<int> * vec ){
   static vector<int> resVector;//new vector..
   int size = vec->size();
   for(int i=0; i< size;i++){
       int res = fxn(vec->at(i));
       resVector.push_back(res);
   }
   vector<int> *p =&resVector;//creating pointer ...points to new vector

   return p;//returning pointer to a new vector..
}

vector<int> * filter ( bool (*fxn) (int), vector<int> * vec ){
   static vector<int> resVector;
   for(int i=0; i< vec->size();i++){
       if(fxn(vec->at(i))){
       resVector.push_back(vec->at(i));
       }
   }
   vector<int> *p =&resVector;

   return p;//returning pointer to a new vector..
}


int reduce ( int (*fxn) (int, int), vector<int> * vec ){
   if(vec->size()==0) return 0;
   int accumulator = vec->at(0);
   for(int i=1; i< vec->size();i++){
       accumulator+=fxn(accumulator,vec->at(i));
   }
   return accumulator;
}

Let me know, if any issues...