//MapGeneric.h #ifndef MAPGENERIC_H #define MAPGENERIC_H #include #include using namespace std; clas

Business, Finance, Economics, Accounting, Operations Management, Computer Science, Electrical Engineering, Mechanical Engineering, Civil Engineering, Chemical Engineering, Algebra, Precalculus, Statistics and Probabilty, Advanced Math, Physics, Chemistry, Biology, Nursing, Psychology, Certifications, Tests, Prep, and more.
Post Reply
answerhappygod
Site Admin
Posts: 899604
Joined: Mon Aug 02, 2021 8:13 am

//MapGeneric.h #ifndef MAPGENERIC_H #define MAPGENERIC_H #include #include using namespace std; clas

Post by answerhappygod »

//MapGeneric.h
#ifndef MAPGENERIC_H
#define MAPGENERIC_H
#include <iostream>
#include <vector>
using namespace std;
class MapGeneric{
private:
int filter_x(int x){
return x;
}
public:
virtual vector<int> map(vector<int> vec) =
0;
void displayList(vector<int> vecValues);
};
class MapSquare : public MapGeneric{
private:
int filter_x(int x){
return x * x;
}
public:
void map(vector<int> *v, int i){
if (i <(*v).size()){
int value =
filter_x((*v));
(*v) = value;
i = i + 1;
map(v, i);
}
}
vector<int> map(vector<int> vec){
vector<int> v;
v = vec;
map(&v, 0);
return v;
}
};
class MapTriple : public MapGeneric{
private:
int filter_x(int x){
return 3 * x;
}
public:
void map(vector<int> *v, int i){
if (i <(*v).size()){
int value =
filter_x((*v));
(*v) = value;
i = i + 1;
map(v, i);
}
}
vector<int> map(vector<int> vec){
vector<int> v;
v = vec;
map(&v, 0);
return v;
}
};
class MapAbsoluteValue : public MapGeneric{
private:
int filter_x(int x){
if (x < 0)
return -1 * x;
return x;
}
public:
void map(vector<int> *v, int i){
if (i <(*v).size()){
int value =
filter_x((*v));
(*v) = value;
i = i + 1;
map(v, i);
}
}
vector<int> map(vector<int> vec){
vector<int> v;
v = vec;
map(&v, 0);
return v;
}
};
#endif
//end of MapGeneric.h
//FilterGeneric.h
#ifndef FILTERGENERIC_H
#define FILTERGENERIC_H
#include <iostream>
#include <vector>
using namespace std;
class FilterGeneric
{
private:
bool ger(int value)
{
return 0;
}
public:
virtual vector<int> filter(vector<int>
vec) = 0;
};
class Filter_Odd : public FilterGeneric
{
private:
bool ger(int x)
{
return (x % 2) != 0;
}
public:
void filter(vector<int> vec, int i,
vector<int> *v)
{
if (i < (vec).size())
{
bool oddFlag =
ger((vec));
if (oddFlag)

(*v).push_back((vec));
i = i + 1;
filter(vec, i,
v);
}
}
vector<int> filter(vector<int>
vec)
{
vector<int> v;
int i = 0;
filter(vec, i, &v);
return v;
}
};
class FilterNonPositive : public FilterGeneric
{
private:
bool ger(int x)
{
return x < 0;
}
public:
void filter(vector<int> vec, int i,
vector<int> *v)
{
if (i < (vec).size())
{
bool oddFlag =
ger((vec));
if (oddFlag)

(*v).push_back((vec));
i = i + 1;
filter(vec, i,
v);
}
}
vector<int> filter(vector<int>
vec)
{
vector<int> v;
int i = 0;
filter(vec, i, &v);
return v;
}
};
class FilterForTwoDigitPositive : public FilterGeneric
{
private:
bool ger(int x)
{
if ((x > 9 && x < 100)
&& x != -x)
return true;
else
return false;
}
public:
void filter(vector<int> vec, int i,
vector<int> *v)
{
if (i < (vec).size())
{
bool twoDigitPosFlag =
ger((vec)[i]);
if
(twoDigitPosFlag)

(*v).push_back((vec)[i]);
i = i + 1;
filter(vec, i,
v);
}
}
vector<int> filter(vector<int>
vec)
{
vector<int> v;
int i = 0;
filter(vec, i, &v);
return v;
}
};
#endif
//end of FilterGeneric.h
//ReduceGeneric.h
#ifndef REDUCEGENERIC_H
#define REDUCEGENERIC_H
#include <iostream>
#include <vector>
using namespace std;
class ReduceGeneric
{
private:
int binaryOperator(int x, int y)
{
return 0;
}
public:
virtual int reduce(vector<int> vec) = 0;
};
class ReduceMinimum : public ReduceGeneric
{
private:
int binaryOperator(int x, int y)
{
if (x < y)
return x;
else
return y;
}
public:
void reduce(vector<int> vec, int i, int
*min)
{
if (i < (vec).size())
{
*min =
binaryOperator(*min, vec[i]);
i = i + 1;
reduce(vec, i,
min);
}
}
int reduce(vector<int> vec)
{
int minVal = vec[0], i = 0;
reduce(vec, i, &minVal);
return minVal;
}
};
class ReduceGCD : public ReduceGeneric
{
private:
int binaryOperator(int x, int y)
{
if (x == 0)
return y;
return binaryOperator(y % x,
x);
}
public:
void reduce(vector<int> vec, int i, int
*gcdValue)
{
if (i < (vec).size())
{
*gcdValue =
binaryOperator(*gcdValue, vec[i]);
i = i + 1;
reduce(vec, i,
gcdValue);
}
}
int reduce(vector<int> vec)
{
int gcd = vec[0], i = 0;
reduce(vec, i, &gcd);
return gcd;
}
};
#endif
//end of ReduceGeneric.h
//Map_Filter_Reduce.cpp
#include <iostream>
#include <vector>
#include <string>
#include <time.h>
#include "MapGeneric.h"
#include "FilterGeneric.h"
#include "ReduceGeneric.h"
using namespace std;
void displayList(vector<int> vecValues)
{
for (int i = 0; i < vecValues.size(); ++i)
{
cout << vecValues[i] << "
";
}
cout << endl << endl;
}
void testCaseWithRandomSampleList(vector<int> list_L, int
size)
{
cout << "The list of "<< size <<"
integers are: " << endl;
displayList(list_L);
MapAbsoluteValue mapAbs;
list_L = mapAbs.map(list_L);
cout << "List of integers after applying
absolute: " << endl;
displayList(list_L);
MapTriple mapTriple;
list_L = mapTriple.map(list_L);
cout << "List of integers after applying
triple map: " << endl;
displayList(list_L);
FilterForTwoDigitPositive filTwoDigit;
list_L = filTwoDigit.filter(list_L);
cout << "List of integers with two digit
values: " << endl;
displayList(list_L);
Filter_Odd filtOdd;
list_L = filtOdd.filter(list_L);
cout << "List of integers that are odd from
existing list: " << endl;
displayList(list_L);
ReduceMinimum redMin;
int minValue = redMin.reduce(list_L);
cout << "The minimum value in the list is: "
<< minValue << endl << endl;
ReduceGCD redGCD;
int gcdValue = redGCD.reduce(list_L);
cout << "The gcd value of the list is: "
<< gcdValue << endl << endl;
}
void values_of_RequestedSampleList(vector<int> list_L, int
length)
{
cout << "The list of "<<length<<"
sample integers are: " << endl;
displayList(list_L);
MapAbsoluteValue mapAbs;
list_L = mapAbs.map(list_L);
MapTriple mapTriple;
list_L = mapTriple.map(list_L);
FilterForTwoDigitPositive filTwoDigit;
list_L = filTwoDigit.filter(list_L);
Filter_Odd filtOdd;
list_L = filtOdd.filter(list_L);
ReduceMinimum redMin;
int minValue = redMin.reduce(list_L);
ReduceGCD redGCD;
int gcdValue = redGCD.reduce(list_L);
cout << "Output: " << minValue
<<" "<< gcdValue << endl << endl;
}
//end of Map_Filter_Reduce.cpp
Thank you.
Join a community of subject matter experts. Register for FREE to view solutions, replies, and use search function. Request answer by replying!
Post Reply