Algorithm: Mutating Sequence Operations

#include <iostream>
#include <iomanip>
#include <algorithm> //copy, copy_backward, swap, iter_swap, swap_ranges, transform, replace, replace_if, replace_copy, replace_copy_if, fill, fill_n, generate, generate_n, remove, remove_if, remove_copy, remove_copy_if, unique, unique_copy, reverse, reverse_copy, rotate, rotate_copy, random_shuffle, partition, stable_partition
#include <utility>
#define LIMIT 6
#define LIMIT2 8
#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
int add1(const int&); // add value to each array element but return a value
int mul1(const int&, const int&); // multiply two integers
bool isNeg(const int&); // check if the argument is negative
int gogo();
/*
Main function
*/
int main() {
    int* pt;
    int arr[]={2,3,5,6,7,8};
    int arr2[]={1,2,3,4,5,6,7,8};
    int arr3[]={-6,-5,-4,-3,-2,-1};
    int arr4[]={1,2,3,4,5,6};
    cout<<"Two original arrays:"<<endl;
    print(arr,LIMIT);
    print(arr2,LIMIT2);
    //copy
    cout<<"COPY OPERATION"<<endl;
    copy(arr,arr+LIMIT,arr2);
    cout<<"Two modified arrays:"<<endl;
    print(arr,LIMIT);
    print(arr2,LIMIT2);
    //copy_backward
    cout<<"COPY_BACKWARD OPERATION"<<endl;
    cout<<"Two original arrays:"<<endl;
    print(arr,LIMIT);
    print(arr2,LIMIT2);
    copy_backward(arr,arr+LIMIT,arr2+LIMIT2);
    cout<<"Two modified arrays:"<<endl;
    print(arr,LIMIT);
    print(arr2,LIMIT2);
    //swap
    cout<<"SWAP OPERATION"<<endl;
    cout<<"Two original arrays:"<<endl;
    print(arr3,LIMIT);
    print(arr4,LIMIT);
    swap(arr3,arr4);
    cout<<"Two modified arrays:"<<endl;
    print(arr3,LIMIT);
    print(arr4,LIMIT);
    //iter_swap
    cout<<"ITER_SWAP OPERATION"<<endl;
    cout<<"Two original arrays:"<<endl;
    print(arr3,LIMIT);
    print(arr4,LIMIT);
    iter_swap(arr3+2,arr4+2);
    cout<<"Two modified arrays:"<<endl;
    print(arr3,LIMIT);
    print(arr4,LIMIT);
    //swap_ranges
    cout<<"SWAP_RANGES OPERATION"<<endl;
    cout<<"Two original arrays:"<<endl;
    print(arr3,LIMIT);
    print(arr4,LIMIT);
    swap_ranges(arr3,arr3+2,arr4+3);
    cout<<"Two modified arrays:"<<endl;
    print(arr3,LIMIT);
    print(arr4,LIMIT);
    //transform
    cout<<"TRANSFORM OPERATION"<<endl;
    cout<<"The original array:"<<endl;
    print(arr3,LIMIT);
    print(arr4,LIMIT);
    cout<<"The first implementation of transform operation"<<endl;
    transform(arr3,arr3+LIMIT,arr3,add1);
    cout<<"The modified array:"<<endl;
    print(arr3,LIMIT);
    cout<<"The second implementation of transform operation"<<endl;
    transform(arr3,arr3+LIMIT,arr4,arr3,mul1);
    cout<<"The modified array:"<<endl;
    print(arr3,LIMIT);
    //replace
    cout<<"REPLACE OPERATION"<<endl;
    cout<<"The original array:"<<endl;
    print(arr3,LIMIT);
    replace(arr3,arr3+LIMIT,-3,4);
    cout<<"The modified array:"<<endl;
    print(arr3,LIMIT);
    //replace_if
    cout<<"REPLACE_IF OPERATION"<<endl;
    cout<<"The original array:"<<endl;
    print(arr3,LIMIT);
    replace_if(arr3,arr3+LIMIT,isNeg,300);
    cout<<"The modified array:"<<endl;
    print(arr3,LIMIT);
    //replace_copy
    cout<<"REPLACE_COPY OPERATION"<<endl;
    cout<<"The original array:"<<endl;
    print(arr3,LIMIT);
    print(arr4,LIMIT);
    replace_copy(arr3,arr3+LIMIT,arr4,4,1000);
    cout<<"The modified array:"<<endl;
    print(arr3,LIMIT);
    print(arr4,LIMIT);
    // fill
    cout<<"FILL OPERATIOM"<<endl;
    cout<<"The original array:"<<endl;
    print(arr3,LIMIT);
    fill(arr3,arr3+3,1986);
    cout<<"The modified array:"<<endl;
    print(arr3,LIMIT);
    // fill_n
    cout<<"FILL_N OPERATIOM"<<endl;
    cout<<"The original array:"<<endl;
    print(arr3,LIMIT);
    fill_n(arr3+3,3,1991);
    cout<<"The modified array:"<<endl;
    print(arr3,LIMIT);
    // generate
    cout<<"GENERATE OPERATION"<<endl;
    cout<<"The original array:"<<endl;
    print(arr3,LIMIT);
    generate(arr3,arr3+LIMIT,gogo);
    cout<<"The modified array:"<<endl;
    print(arr3,LIMIT);
    // unique
    int arr5[]={1,1,3,4,4,5};
    cout<<"UNIQUE OPERATION"<<endl;
    cout<<"The original array:"<<endl;
    print(arr5,LIMIT);
    pt=unique(arr5,arr5+LIMIT);
    cout<<"The modified array:"<<endl;
    print(arr5,LIMIT);
    cout<<pt-arr5+1<<"position has been checked for their duplication"<<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;
}
int add1(const int& i){
    return i+3;
}
int mul1(const int& i, const int& j){
    return i*j;
}
bool isNeg(const int& i){
    return i<0;
}
int gogo(){
    return 1212;
}
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s