Algorithm: non-mutating sequence operations

Non-mutating sequence operations: for_each, find, find_if, adjacent_find, count, count_if, mismatch, equal, search
#include <iostream>
#include <iomanip>
#include <algorithm> //for_each, find, find_if, adjacent_find, count, count_if, mismatch, equal, search
#include <utility>
#define LIMIT 6
#define FIND_ELEMENT 11
using namespace std;
/*
Function declaration
*/
void add(int&); //add value to each array element
void print(const int[], const int&); // print arrays
bool isOdd(const int&); // check if element is odd
bool isConsecutive(const int&, const int&); // check if two numbers are consecutive
bool isMoreThan3(const int&,const int&); // check if twi numbers are more or less than 3
/*
Main function
*/
int main() {
	int arr[] = { 2,4,6,6,11,8 };
	int arr2[] = { 2,4,6,4,8,12 };
	int arr3[] = { 2,4,5,4,9,12 };
	int arr4[] = { 6,4,8 };
	int *pt;
	cout << "Original array:" << endl;
	print(arr, LIMIT);
	cout << "Add 3 to each array element:" << endl;
	//for_each(first,last,functor)
	for_each(arr, arr + LIMIT, add);
	print(arr, LIMIT);
	//find(first,last,value)
	pt = find(arr, arr + LIMIT, FIND_ELEMENT);
	if (pt != arr + LIMIT) cout << FIND_ELEMENT << " is found in the given array and it stands at " << pt - arr + 1 << " position of the array" << endl;
	else cout << FIND_ELEMENT << " is NOT found in the given array" << endl;
	//find if(first,last,predicate)
	pt = find_if(arr, arr + LIMIT, isOdd);
	if (pt != arr + LIMIT) cout << "The first odd element stands at " << pt - arr + 1 << " position of the array" << endl;
	else cout << "There is not any odd element in the given array" << endl;
	//adjacent_find (first,last,[predicate]);
	pt = adjacent_find(arr, arr + LIMIT);
	if (pt != arr + LIMIT) cout << "There are some adjacent elements that are equal, the first element is at " << pt - arr + 1 << " position of the array" << endl;
	else cout << "There are not any adjacent elements that are equal in the given array" << endl;
	pt = adjacent_find(arr, arr + LIMIT, isConsecutive);
	if (pt != arr + LIMIT) cout << "There are some adjacent elements that are less or greater another 1, the first element is at " << pt - arr + 1 << " position of the array" << endl;
	else cout << "There are not any adjacent elements that are less or greater another 1 in the given array" << endl;
	//count
	cout << "There are " << count(arr, arr + LIMIT, 9) << " that equal(s) to 9 in the given array" << endl;
	cout << "There are " << count_if(arr, arr + LIMIT, isOdd) << " odd elements in the given array" << endl;
	// mismatch (first 1,last 1, first 2, [predicate])
	cout<<"Two compared arrays:"<<endl;
	print(arr2,LIMIT);
	print(arr3,LIMIT);
	pair<int*, int*> pa;
	pa = mismatch(arr2, arr2 + LIMIT, arr3);
	if (pa.first==arr2+LIMIT) cout<<"Two given arrays are the same. They are totally matched"<<endl;
	else cout<<"Two given arrays are mismatched. The different members are: "<<*pa.first<<" in the first and "<<*pa.second<<" in the second one"<<endl;
	pa = mismatch(arr2, arr2 + LIMIT, arr3,isMoreThan3);
	if (pa.first==arr2+LIMIT) cout<<"Two given arrays are not far than 3."<<endl;
	else cout<<"Two given arrays are mismatched in limit of 3. The different members are: "<<*pa.first<<" in the first and "<<*pa.second<<" in the second one"<<endl;
	// equal
	if (equal(arr2,arr2+LIMIT,arr3)) cout<<"Two arrays are totally matched"<<endl;
	else cout<<"Two arrays are different"<<endl;
	// search
	cout<<"The third array is:"<<endl;
	print(arr4,3);
	pt=search(arr3,arr3+LIMIT,arr4,arr4+3);
	if (pt!=arr3+LIMIT) cout<<"The partern is found in the first array at "<<pt-arr3+1<<" position"<<endl;
	else cout<<"The partern is NOT found"<<endl;
    pt=search(arr3,arr3+LIMIT,arr4,arr4+3,isMoreThan3);
    if (pt!=arr3+LIMIT) cout<<"The partern with a predication is found in the first array at "<<pt-arr3+1<<" position"<<endl;
    else cout<<"The partern is NOT found"<<endl;
}
/*
Function definition1
*/
void add(int& i) {
	i += 3;
}
void print(const int arr[], const int& SIZE) {
	cout << "[";
	for (unsigned i = 0; i<SIZE; ++i) {
		cout << setw(2) << arr[i];
		if (i != SIZE - 1) cout << ",";
	}
	cout << "]" << endl;
}
bool isOdd(const int& i) {
	return i % 2;
}
bool isConsecutive(const int& i, const int& j) {
	return abs(i - j) == 1 ? true : false;
}
bool isMoreThan3(const int& i, const int& j){
    return abs(i-j)<3?true:false;
}
OUTPUT
Original array:
[ 2, 4, 6, 6,11, 8]
Add 3 to each array element:
[ 5, 7, 9, 9,14,11]
11 is found in the given array and it stands at 6 position of the array
The first odd element stands at 1 position of the array
There are some adjacent elements that are equal, the first element is at 3 position of the array
There are not any adjacent elements that are less or greater another 1 in the given array
There are 2 that equal(s) to 9 in the given array
There are 5 odd elements in the given array
Two compared arrays:
[ 2, 4, 6, 4, 8,12]
[ 2, 4, 5, 4, 9,12]
Two given arrays are mismatched. The different members are: 6 in the first and 5 in the second one
Two given arrays are not far than 3.
Two arrays are different
The third array is:
[ 6, 4, 8]
The partern is NOT found
The partern with a predication is found in the first array at 3 position
Advertisements