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 */
}
#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) = □
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
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
Map, Filter, and Reduce are three functions commonly used in functional programming. For this assignment you...
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 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 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 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
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 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 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...
for this assignment you will be creating a series of functions for manipulating an array of data. The data will consist of randomly generated doubles. You should make the following functions: generate() - This function fills an array with random doubles in a specified range. This function takes four inputs: an array of doubles, an integer representing the size of the array, and two doubles representing the lower an upper bounds of the range random values. For example, the function...
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...
In this project, the students will finish three functions that are described in Programming Project 4 (on page 536) and Programming Project 6 (on page 358). The student will also implement the main function and a print function to test these three functions. Please notice, there is a video notes for the solution of Project 6. However, your main function shall have more test than what in video notes. The student may start with the attached code. Please rename the...