Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Example program
- #include <iostream>
- #include <string>
- #include <vector>
- using namespace std;
- //struct MyVector for Task 10
- template <typename T>
- struct MyVector
- {
- vector<T> v1 = {};
- };
- //pair fot test in Task 10
- struct Pair
- {
- int x = 0;
- int y = 0;
- };
- //Overloading operator < and << for struct Pair
- bool operator<(const Pair& left, const Pair& right) {
- return (left.x + left.y) < (right.x + right.y);
- }
- ostream& operator<<(ostream& o, const Pair& p)
- {
- o << p.x << " " << p.y << " | " << endl;
- return o;
- }
- //value of x makes no sense because it is always takse y value
- void task1(bool x, bool y)
- {
- cout <<"Case: x = " << x << "| y = " << y << endl;
- cout <<"Just y result " << y << endl;
- cout <<"x = y ? x : y result " << (x = y ? x : y) << endl;
- }
- //in case that next element always is prev element ^ 2
- double task2 (int x, double n)
- {
- double res = x;
- double prev = x;
- for(int i = 0; i < n; i++)
- {
- prev *= prev;
- res += prev;
- }
- return res;
- }
- //recorsive function that implements i + 1/((i+1)/...)
- double task3(int n, int i)
- {
- i < 1? i = 1 : false;
- if(i <= n)
- {
- return i + 1/(task3(n, i + 1));
- }
- else
- {
- return n + 1;
- }
- }
- //calculate next element while not n
- int task4(int n, int a1, int a2, int a3)
- {
- int res = 0;
- for(int i = 3; i < n; i++ ) {
- res = -2 * a3 + a2 + 2 * a1;
- a1 = a2;
- a2 = a3;
- a3 = res;
- }
- return res;
- }
- //fast pow for task 5
- double fastPow(int x, int n)
- {
- double result = 1;
- for(; n >= 1; n/=2)
- if(n%2 == 1)
- {
- result*=x;
- x*=x;
- }
- else x*=x;
- return result;
- }
- // calculate n-th element with fastPow speed
- int task5(int n)
- {
- int y;
- n % 2 == 0 ? y = 1 : y = -1;
- return fastPow(-2, n) + y + 1;
- }
- // recursive calculation of n-th element
- int task6(int n, int a1, int a2, int a3)
- {
- if(n <= 3) return a3;
- return task6(n-1, a2, a3, -2*a3 + a2 + 2 *a1);
- }
- //multiply i-th element on x
- void *task8(int a[], int n, int x)
- {
- for(int i = 0; i < n; i++)
- a[i] *= x;
- }
- //sum of i-th elemnts form a[] and b[]
- void *task9(int a[], int n, int b[], int m)
- {
- n < m ? n : n = m;
- for(int i = 0; i < n; i++)
- a[i] += b[i];
- }
- //function to print array
- void outArray(int a[], int n)
- {
- for(int i = 0; i < n; i++)
- cout << a[i] << " ";
- cout << endl;
- }
- //bubl sort
- template <typename T>
- void sort(MyVector<T>& v)
- {
- for(int i = 0; i < v.v1.size(); i++)
- for(int j = 0; j < v.v1.size(); j++)
- if(v.v1[i] < v.v1[j])
- {
- T temp = v.v1[i];
- v.v1[i] = v.v1[j];
- v.v1[j] = temp;
- }
- }
- //out vector
- template <typename T>
- void outVector(MyVector<T> mv)
- {
- for(int i = 0; i < mv.v1.size(); i++)
- cout << mv.v1[i] << " ";
- cout << endl;
- }
- int main()
- {
- cout << "Task1" << endl;
- task1(true, false);
- task1(false, true);
- task1(false, false);
- task1(true, true);
- cout << "Task3: " << task3(3, 10) << endl;
- cout << "Task3: " << task3(1, 1) << endl;
- cout << "Task4: " << task4(4, -2, 6, -8) << endl;
- cout << "Task5: " << task5(4) << endl;
- cout <<"Task6: " << task6(4, -2, 6, -8) << endl;
- cout << "Task8: " << endl;
- int test[] = {3, 4, 5};
- task8(test, 3, 2); outArray(test, 3);
- cout << "Task9: " << endl;
- int a[] = {1, 2, 3}, b[] = {4, 5, 6};
- task9(a, 3, b, 3); outArray(a, 3);
- cout << "Task10: " << endl;
- MyVector<int> mv1;
- MyVector<string> mv2;
- MyVector<Pair> mv3;
- cout << "Integer test: " << endl;
- mv1.v1 = {7, 5, 16, 8};
- sort(mv1);
- outVector(mv1);
- cout << "Pair test: " << endl;
- mv2.v1 = {"ab", "bb", "ba", "aa"};
- sort(mv2);
- outVector(mv2);
- cout << "Pair test: " << endl;
- Pair a1, b1, c1, d1;
- a1.x = 2; a1.y = 2;
- b1.x = 2; b1.y = 3;
- c1.x = 3; c1.y = 4;
- d1.x = 5; d1.y = 6;
- mv3.v1 = {b1, a1, d1, c1};
- sort(mv3);
- outVector(mv3);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement