Question

Map, Filter, and Reduce are three functions commonly used in functional programming. For this assignment you...

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.

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

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

I have created unit tests for all six functions. 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<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 */

}

0 0
Add a comment Improve this question Transcribed image text
Answer #1

#include <iostream>
#include <stdlib.h>
#include <vector>
using namespace std;

vector<int> v;
bool isEven (int);  

int square (int d){
return d*d;
}
bool isEven (int e){

return (e%2)==0;
} // For use with filter
int product (int a,int b){
return a*b;

}
vector<int> * map ( int (*fxn) (int), vector<int> * vec ){
v.clear();
v = *vec;
for(int i = 0 ;i<v.size();++i){
int d = fxn(v[i]);
(v[i]) = d;
}
  
return &v;
  
}

vector<int> * filter ( bool (*fxn) (int), vector<int> * vec ){
v.clear();
vector<int> v1 = *vec;
for(int i = 0 ;i<v1.size();++i){
bool d = fxn(v1[i]);
if(d)
v.push_back(v1[i]);
}
  
return &v;
  
}
int reduce ( int (*fxn) (int, int), vector<int> * vec ){
vector<int> v1 = *vec;
int d = 1;
for(int i = 0 ;i<v1.size();++i){
d = fxn(d , v1[i]);
}
  
return d;
}

// For use with map

// For use with reduce

bool testPassed();


int main ( ) {

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

vector<int>vec;
vec.push_back(1);
vec.push_back(3);
vec.push_back(5);
vec.push_back(7);
vec.push_back(9);
int (*fun_ptr)(int) = &square;


vector<int> * m = map(fun_ptr, &vec);
vector<int> v = *m;
cout<<"After calling map method: "<<endl;
for(int i = 0 ;i<v.size();++i){
cout<< v[i] <<" ";
}
cout<<endl;
bool (*fun)(int) = &isEven;
  
vector<int>vec1;
vec1.push_back(1);
vec1.push_back(2);
vec1.push_back(3);
vec1.push_back(4);
vec1.push_back(5);
vector<int> * p = filter(fun, &vec1);
vector<int> p1 = *p;
cout<<"After calling filter method: "<<endl;
for(int i = 0 ;i<p1.size();++i){
cout<< p1[i] <<" ";
}
  
cout<<endl;
vector<int>prod;
prod.push_back(1);
prod.push_back(2);
prod.push_back(3);
prod.push_back(4);
int (*fun1)(int, int) = &product;
int val = reduce(fun1, &prod);
cout<<"Product is: " <<val<<endl;
  

}

=======================================================================
See Output
After calling map method: 1 9 25 49 81 After calling filter method: 3 vector<int>map int (*fxn) (int), vector<int» * vec )1 v


Thanks, let me know if there is any concern. Comment and I will surely respond for any doubts/clarification. Please rate if you think its helpful

Add a comment
Know the answer?
Add Answer to:
Map, Filter, and Reduce are three functions commonly used in functional programming. For this assignment you...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Write C++ Code in single source file (.cpp file) containing following user defined functions: 1. IsEven-...

    Write C++ Code in single source file (.cpp file) containing following user defined functions: 1. IsEven- takes an integer input (one argument of type int) and return true or false. 2. IsPositive- takes a float input (one argument of type double) and return a Boolean value. The function returns the true if its argument is positive and false if its argument is zero or negative. 3. IsPrime- takes an integer input and return true or false. 4. NumOfDigits- takes an...

  • Using Racket Recursion, tail-recursion, high-order functions and functional programming. 1. Modify our filter function so that...

    Using Racket Recursion, tail-recursion, high-order functions and functional programming. 1. Modify our filter function so that it is tail-recursive. You may use the letrec form but do not use any additional forms and functions besides those we've talked about in class. (define filter (lambda (input-list func)     (cond ((null? input-list) '())           ((func (car input-list))            (cons (car input-list) (filter (cdr input-list) func)))           (else            (filter (cdr input-list) func))))) 2. Test your filter function on '(25 -22 44 56...

  • 20. [5 points] Rewrite the following ML. function using patterns fun factn. ifn-0 then 1 else n fact (n-1) 21. [S points) Using map function, write an ML function int2real of type int list real l...

    20. [5 points] Rewrite the following ML. function using patterns fun factn. ifn-0 then 1 else n fact (n-1) 21. [S points) Using map function, write an ML function int2real of type int list real list that takes a list of integers and returns the same numbers converted to real. For example, if the input is [1,2,3], the output should be like [1.0,2.0,3.0 22. [5 points] Using foldr function, write a function duplist of type·a list 'a list that takes...

  • 3460:209 Assignment 9-B Assignment9-B: The Element Shifter The purpose of this assignment is to help gauge...

    3460:209 Assignment 9-B Assignment9-B: The Element Shifter The purpose of this assignment is to help gauge your skills in writing small programs that involve pointers. The program also contains functions and may perform input, output, files and file processing, use arrays and vectors and/or c-string/string arrays, flow of control, and/or calculations. PROGRAM SPECIFICATION For this program, we are going to expand a standard array by dynamically allocating a new one with a larger footprint. The program will use a function...

  • Use scheme to solve this: MUST USE A COMBINATION OF MAP AND FILTER or MAP AND...

    Use scheme to solve this: MUST USE A COMBINATION OF MAP AND FILTER or MAP AND APPLY: THIS ANSWER IS NOT WHAT I NEED: (define perfect-square-helper (lambda (x y) (if (null? x) (reverse y) (if (integer? (sqrt (car x))) (perfect-square-helper (cdr x) (cons (car x) y)) (perfect-square-helper (cdr x) y))))) (define perfect-square (lambda (x) (perfect-square-helper x '()))) (define x '(1 2 9 16 5 64)) (perfect-square x) Map/Apply/Filter 7. Perfect Squares (10 Points) Using a combination of map, apply and/or...

  • Note: None of these functions should use cout. It is OK to use cout while debugging...

    Note: None of these functions should use cout. It is OK to use cout while debugging to check how your function is working, but your final submission should not contain cout in any function but main. Head ==== Name the source file for this section head.cpp. Write a function named "head" which takes an array of integers as an argument, and returns the first element in the array. Write a function named main which outputs the result of testing your...

  • **IN C*** * In this lab, you will write a program with three recursive functions you...

    **IN C*** * In this lab, you will write a program with three recursive functions you will call in your main. For the purposes of this lab, keep all functions in a single source file: main.c Here are the three functions you will write. For each function, the output example is for this array: int array[ ] = { 35, 25, 20, 15, 10 }; • Function 1: This function is named printReverse(). It takes in an array of integers...

  • Concepts tested by the program: Working with one dimensional parallel arrays Use of functions Use of...

    Concepts tested by the program: Working with one dimensional parallel arrays Use of functions Use of loops and conditional statements Description The Lo Shu Magic Square is a grid with 3 rows and 3 columns shown below. The Lo Shu Magic Square has the following properties: The grid contains the numbers 1 – 9 exactly The sum of each row, each column and each diagonal all add up to the same number. s is shown below: Write a program that...

  • IntList Recursion Assignment Specifications: You will add some additional recursive functions to your IntList class as...

    IntList Recursion Assignment Specifications: You will add some additional recursive functions to your IntList class as well as make sure the Big 3 are defined. IntNode class I am providing the IntNode class you are required to use. Place this class definition within the IntList.h file exactly as is. Make sure you place it above the definition of your IntList class. Notice that you will not code an implementation file for the IntNode class. The IntNode constructor has been defined...

  • PLEASE HELP!!! C PROGRAMMING CODE!!! Please solve these functions using the prototypes provided. At the end...

    PLEASE HELP!!! C PROGRAMMING CODE!!! Please solve these functions using the prototypes provided. At the end please include a main function that tests the functions that will go into a separate driver file. Prototypes int R_get_int (void); int R_pow void R Jarvis int start); void R_fill_array(int arrayll, int len); void R_prt_array (int arrayl, int len) void R-copy-back (int from[], int to [], int len); int R_count_num (int num, int arrayll, int len) (int base, int ex) 3. Build and run...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT