diff options
| author | Santo Cariotti <santo@dcariotti.me> | 2021-02-06 19:56:36 +0100 | 
|---|---|---|
| committer | Santo Cariotti <santo@dcariotti.me> | 2021-02-06 19:56:36 +0100 | 
| commit | d2edbc38cac8da52f58c5cd3da6c0c625fa05736 (patch) | |
| tree | a51e9a4e56fc9d4c7c9e37576dceedca3a0c72b4 /Year_1/Programming_1 | |
| parent | 98f34040820dc3a964b7be59a698323e8cc6c8a3 (diff) | |
conf: rename
Diffstat (limited to 'Year_1/Programming_1')
40 files changed, 2334 insertions, 0 deletions
| diff --git a/Year_1/Programming_1/ex01.cc b/Year_1/Programming_1/ex01.cc new file mode 100644 index 0000000..9183dec --- /dev/null +++ b/Year_1/Programming_1/ex01.cc @@ -0,0 +1,42 @@ +#include <iostream> +#include <algorithm> +#include <vector> +#define K 2 +#define N 3 + +using namespace std; + +template<int KK, int NN> +bool func(int (&a)[KK][NN][NN], int w) { +	vector<int> list; +	for(int i = 0; i < KK; ++i) { +		list.clear(); +		for(int j = 0; j < NN; ++j) +			list.push_back(a[i][j][j]); +			 +		auto mm = minmax_element(begin(list), end(list)); +		float media = (static_cast<float>(*mm.first)+static_cast<float>(*mm.second))/2; +		if(media <= w) +			return true; +	} +	 +	return false; +} + +int main() { +	int a[K][N][N] = { +		{ +			{1, 2, 30}, +			{1, 80, 3}, +			{1, 2, 3}, +		}, +		{ +			{1, 2, 30}, +			{1, 20, 3}, +			{1, 2, 3}, +		}}; +		 +	cout << func(a, 5); + +    return 0; +} diff --git a/Year_1/Programming_1/ex08.cc b/Year_1/Programming_1/ex08.cc new file mode 100644 index 0000000..fdfea92 --- /dev/null +++ b/Year_1/Programming_1/ex08.cc @@ -0,0 +1,36 @@ +#include <iostream> +#include <stdio.h> +#define N 3  +#define M 5 + +using namespace std; + +bool func(int A[N][M], short k, short w) {  +    short counter = 0; +    for(int i = 0; i < M; ++i) { +        short t_counter = 0; +        for(int j = 1; j < N; ++j) { +            if(A[j][i] < A[j-1][i]) +                break; +            else  +                ++t_counter; +        } + +        if(t_counter >= k) +            ++counter; +    } + +    return counter >= w; +} + +int main() { +    int array[N][M] = { +        {5, 7, 9, 10, 5}, +        {10, 2, 3, 4, 10}, +        {16, 34, 21, 23, 1} +    }; + +    cout << func(array, 2, 1) << endl; + +    return 0; +} diff --git a/Year_1/Programming_1/ex10_07_18.cc b/Year_1/Programming_1/ex10_07_18.cc new file mode 100644 index 0000000..092b184 --- /dev/null +++ b/Year_1/Programming_1/ex10_07_18.cc @@ -0,0 +1,45 @@ +#include<iostream> +#include<vector> + +#define n 3 + +using namespace std; + +bool func(char (*A)[n]) { +    bool check{false}; +    char str[n]; + +    for(int i = 0; i < n; ++i) { +        str[i] = A[i][i]; +    } + +    short n_check{0}; +    for(int i = 0; i < n; ++i) { +        short index{0}; +        for(int j = 0; j < n; ++j) { +            if(A[j][i] != str[index++]) { +                break; +            } +            ++n_check; +        } +        if(n_check == n) { +            check = true; +            break; +        } +    } + +    return check; +} + + +int main() { +    char A[3][3] = { +        {'a', 'b', 'c'}, +        {'b', 'b', 'c'}, +        {'c', 'b', 'c'}, +    }; + +    cout << func(&A[0]); + +    return 0; +} diff --git a/Year_1/Programming_1/ex12.cc b/Year_1/Programming_1/ex12.cc new file mode 100644 index 0000000..ab20cef --- /dev/null +++ b/Year_1/Programming_1/ex12.cc @@ -0,0 +1,50 @@ +#include <iostream> +#include <vector> +#define N 3 +using namespace std; + +string* func(string a[N][N], short k, string s) { +    string* sn = new string[N]; +    vector<string> l; + +    // Diagonale secondaria +    for(int i = N-1, j = 0; i >= 0; --i, ++j) { +        l.push_back(a[i][j]); +    }  + +    bool d2 = true; +    for(auto const& i : l) { +        if(i.length() < k || i.find(s) != 0) { +            d2 = false; +            break; +        } +    } + +    if(d2) { +        for(int i = 0; i < N; ++i) { +            sn[i] = l.at(i); +        } +    } else { +        for(int i = 0; i < N; ++i) { +            sn[i] = a[i][i]; +        } +    } + +    return sn;  +} + +int main() { +    string a[N][N] = { +        {"aab", "bbc", "4de"}, +        {"xyb", "bbc", "yz3"}, +        {"47x", "1y_", "23g"}, +    }; +    string* x = func(a, 1, "4"); + +    for(int i = 0; i < N; ++i) +        cout << x[i] << ' '; +     +    cout << endl; + +    return 0; +} diff --git a/Year_1/Programming_1/ex13.cc b/Year_1/Programming_1/ex13.cc new file mode 100644 index 0000000..f9f19fa --- /dev/null +++ b/Year_1/Programming_1/ex13.cc @@ -0,0 +1,45 @@ +#include <iostream> +#include <memory> +#include <algorithm> +#include <vector> +using namespace std; + +template<int N, int K> +unique_ptr<double[]> func(int (&A)[K][N], int (&B)[N][K]) { +    auto arr = unique_ptr<double[]>(new double{K}); +    for(int i = 0; i < K; ++i) { +        vector<int> t; +        int sum = 0; +        for(int j = 0; j < N; ++j) { +            t.push_back(B[j][i]); +            sum+=A[i][j]; +        } +         +        double media = static_cast<double>(sum)/N; +        auto min_num = min_element(begin(t), end(t)); +        arr[i] = media - *min_num; +    } + +    return arr; +} + +int main() { +    const int N2 = 3, K2 = 2; +    int A[K2][N2] = { +        {3, 7, 10}, +        {5, 12, 32}, +    }; +    int B[N2][K2] = { +        {12, 10}, +        {15, 17}, +        {8, 0}, +    }; +    auto x = func(A, B); + +    for(int i = 0; i < K2; ++i) +        cout << x[i] << ' '; +     +    cout << endl; + +    return 0; +} diff --git a/Year_1/Programming_1/ex15.cc b/Year_1/Programming_1/ex15.cc new file mode 100644 index 0000000..525882c --- /dev/null +++ b/Year_1/Programming_1/ex15.cc @@ -0,0 +1,41 @@ +#include <iostream> +using namespace std; + +template<int N, int M> +unique_ptr<string[]> func(string (&A)[N][M], short (&B)[N][M]) { +    auto arr = unique_ptr<string[]>(new string[N]); + +    for(int i = 0; i < N; ++i) { +        short sum{0}; +        string tmp{""}; +        for(int j = 0; j < M; ++j) { +            sum+=B[i][j]; +            tmp+=A[i][j]; +        } + +        arr[i] = tmp.length() >= sum ? tmp : ""; +    } + +    return arr; +} + +int main() { +    string A[2][3] = { +        {"aab", "aA", "314"}, +        {"1334", "5715", "__"}, +    }; + +    short B[2][3] = { +        {2, 3, 0}, +        {1, 15, 7} +    }; +     +    auto x = func(A, B); + +    for(int i = 0; i < 2; ++i) +        cout << x[i] << ' '; +     +    cout << endl; + +    return 0; +} diff --git a/Year_1/Programming_1/ex16.cc b/Year_1/Programming_1/ex16.cc new file mode 100644 index 0000000..4c2b74a --- /dev/null +++ b/Year_1/Programming_1/ex16.cc @@ -0,0 +1,41 @@ +#include <iostream> + +using namespace std; + +template<int N, int M> +short func(string (&S)[N][M], short (&B)[M]) { +    short index{0}; +    short ex_count{0}; +    short i{0}; + +    for(auto const& row : S) { +        short count{0}; +        if(i > M) +            break; + +        for(auto const& col : row) { +            if(col.length() <= B[i]) +                ++count;             +        } + +        if(count > ex_count) { +            index = i; +            ex_count = count; +        } +        ++i; +    } + +    return index; +} + +int main() { +    string t[2][3]{ +        {"greeeeee", "aaaaaaa", "bbbbbb"}, +        {"abc", "sooca", "icsdi"}, +    }; +    short bb[3]{3, 10}; +    short x = func(t, bb); + +    cout << x << endl; +    return 0; +} diff --git a/Year_1/Programming_1/ex17.cc b/Year_1/Programming_1/ex17.cc new file mode 100644 index 0000000..9eee61d --- /dev/null +++ b/Year_1/Programming_1/ex17.cc @@ -0,0 +1,43 @@ +#include <iostream> + +using namespace std;  + +template<int N, int M> +short* func(string (&S)[N][M], char (&C)[N], char (&D)[N]) { +    short* a = new short[N]; + +    for(int i = 0; i < N; ++i) { +        short counter = 0; +        for(auto const& rows_string : S[i]) { +            if(rows_string.length() > 2) { +                if(rows_string.at(0) == C[i] +                    && rows_string.at(rows_string.length()-1) == D[i]) +                        ++counter; +            } +        } + +        a[i] = counter; +    }     + + +    return a; +} + +int main() { +    string t[3][3]{ +        {"suca", "we", "soia"}, +        {"omg", "ooohygodg", "onopg"}, +        {"", "mii", ""}, +    }; + +    char c[3] = {'s', 'o', 'a'}; +    char d[3] = {'a', 'g', 'a'}; + +    short* n = func(t, c, d); + +    for(int i = 0; i < 3; ++i) { +        cout << n[i] << ' '; +    } + +    return 0; +} diff --git a/Year_1/Programming_1/ex1_03_12_19.cc b/Year_1/Programming_1/ex1_03_12_19.cc new file mode 100644 index 0000000..8f9bed7 --- /dev/null +++ b/Year_1/Programming_1/ex1_03_12_19.cc @@ -0,0 +1,65 @@ +#include <iostream> + +using namespace std; +/* +2 3 4 5 +1 2 3 4 +1 2 3 0 + +3 x 4 + +5 2 1 0 +*/ + +template<int N, int M> +bool* func(unsigned int (&K)[N][M], double (&D)[M]) { +    bool* A = new bool[M]; +    for(int i = 0; i < M; ++i)  +        A[i] = false; + +    for(int i = 0; i < M; ++i) { +        int sum = 0; +        for(int j = 0; j < N; ++j) { +            sum+=K[j][i]; +        } + +        if(static_cast<double>(sum)/static_cast<double>(M) > D[i]) { +            A[i] = true; +        } +    } + +    return A; +} + +int main() { +    // TODO: test +    unsigned int A[3][4] = { +        {2, 3, 4, 5}, +        {1, 2, 3, 4}, +        {1, 2, 3, 0} +    };    + +    double D[4] = {5., 2.2, 1.1, 0.50}; + +    auto a = func(A, D); +    for(int i = 0; i < 4; ++i)  +        cout << a[i] << ' '; + +    cout << endl; +     +    unsigned int A2[3][4] = { +        {2, 3, 4, 5}, +        {1, 2, 0, 4}, +        {1, 2, 0, 0} +    };    + +    double D2[4] = {5., 2.2, 15.1, 18.50}; + +    auto a2 = func(A2, D2); +    for(int i = 0; i < 4; ++i)  +        cout << a2[i] << ' '; + +    cout << endl; + +    return 0; +} diff --git a/Year_1/Programming_1/ex1_04_12_19.cc b/Year_1/Programming_1/ex1_04_12_19.cc new file mode 100644 index 0000000..3d352f1 --- /dev/null +++ b/Year_1/Programming_1/ex1_04_12_19.cc @@ -0,0 +1,39 @@ +#include <iostream> + +using namespace std; + +template<int N, int M> +short func(int (&A)[N][M], double d1, double d2, short s) { +    short result{0}; + +    for(int i = 0; i < M; ++i) { +        short temp_result{0}; +        for(int j = 0; j < N-1; ++j) { +            if(temp_result >= s) +                continue; + +            double div = static_cast<double>(A[j][i]) / static_cast<double>(A[j+1][i]); +            if(div >= d1 && div <= d2) +                temp_result++; +        } + +        if(temp_result >= s) +            result++; +    } + +    return result; +} + +int main() { +    int a1[5][2] = { +        {15, 100}, +        {15, 100}, +        {10, 100}, +        {10, 10}, +        {10, 30}, +    };   + +    cout << func(a1, 0.3, 1.9, 2) << endl; + +    return 0; +} diff --git a/Year_1/Programming_1/ex1_06_12_18.cc b/Year_1/Programming_1/ex1_06_12_18.cc new file mode 100644 index 0000000..b16748f --- /dev/null +++ b/Year_1/Programming_1/ex1_06_12_18.cc @@ -0,0 +1,35 @@ +#include<iostream> + +using namespace std; + +template<int N> +bool func(int (&A)[N][N], double w) { +    int sumDiagonale{}; +    for(int i = 0; i < N; ++i) sumDiagonale+=A[i][i]; + +    for(int i = 0; i < N; ++i) { +        int sumCol{}; +        for(int j = 0; j < N; ++j) { +            sumCol+=A[j][i]; +        } + +        if(static_cast<double>(sumCol/sumDiagonale) > w) +            return true; +    } +     + +    return false; +} + +int main() { +    int A[4][4] = { +        {1, 2, 3, 4}, +        {1, 2, 3, 4}, +        {1, 2, 3, 4}, +        {1, 2, 3, 4}, +    }; + +    cout << func(A, 4.5); +     +    return 0; +} diff --git a/Year_1/Programming_1/ex1_08_03_18.cc b/Year_1/Programming_1/ex1_08_03_18.cc new file mode 100644 index 0000000..9942714 --- /dev/null +++ b/Year_1/Programming_1/ex1_08_03_18.cc @@ -0,0 +1,33 @@ +#include<iostream> + +#define n 3 + +using namespace std; + +bool func(int (*A)[n]) { +    /* +        0    1   n-2  n-1   +      0 | x | x | x | x | +      1 | x | x | x | x | +    n-2 | x | x | x | x | +    n-1 | x | x | x | x | +    */ + +    int sum{}; // Inizializza sum a 0 +    for(int i = 1, j = n-1; j > 0; ++i, j--) { +        sum+=A[j][i]; +    } + +    return (sum % n == 0); +} + +int main() { +    int A[n][n] = { +        {1, 2, 3, }, +        {1, 2, 3, }, +        {1, 3, 0, }, +    }; + +    cout << func(&A[0]); +    return 0; +} diff --git a/Year_1/Programming_1/ex1_18_02_19.cc b/Year_1/Programming_1/ex1_18_02_19.cc new file mode 100644 index 0000000..a1bfc91 --- /dev/null +++ b/Year_1/Programming_1/ex1_18_02_19.cc @@ -0,0 +1,33 @@ +#include<iostream> + +using namespace std; + +template<int N, int M> +bool func(int (&A)[N][M], double w, double v) { +    if(w > v) return false; + +    for(int i = 0; i < M; ++i) { +        double sum{}; +        for(int j = 0; j < N; ++j) { +            sum+=A[j][i]; +        } + +        double media = sum / static_cast<double>(N); + +        if(media >= w && media <= v) return true; +    } + +    return false; +} + +int main() { +    int M[3][4] = { +        {1, 2, 3, 4}, +        {1, 2, 3, 5,}, +        {1, 2, 3, 6}, +    }; +     +    cout << func(M, 0.1, 0.2); + +    return 0; +} diff --git a/Year_1/Programming_1/ex1_23_04_19.cc b/Year_1/Programming_1/ex1_23_04_19.cc new file mode 100644 index 0000000..c977ece --- /dev/null +++ b/Year_1/Programming_1/ex1_23_04_19.cc @@ -0,0 +1,35 @@ +#include<iostream> +#define N 4 +#define M 3 + +using namespace std; + +double func(string (*S)[M], string s1, short k) { +    if(k < 1 || k > M) return -1; + +    short counter{}; +    short c_strings{}; + +    for(int i = 0; i < N; ++i) { +        for(int j = 0; j < k; ++j) { +            ++c_strings; +            if(S[i][j].length() > s1.length()) +                ++counter; +        } +    } +     +    return static_cast<double>(counter*100/c_strings); +} + +int main() { +    string A[N][M] = { +        {"red", "orange", "green"}, +        {"red", "orange", "green"}, +        {"red", "orange", "green"}, +        {"red", "orange", "green"}, +    }; + +    cout << func(&A[0], "pink", 2); +    +    return 0; +} diff --git a/Year_1/Programming_1/ex1_23_07_19.cc b/Year_1/Programming_1/ex1_23_07_19.cc new file mode 100644 index 0000000..3c6c5f3 --- /dev/null +++ b/Year_1/Programming_1/ex1_23_07_19.cc @@ -0,0 +1,28 @@ +#include<iostream> + +using namespace std; + +template<int N, int M> +bool func(double (&D)[N][M], int a) { +    for(int i = 0; i < M; ++i) { +        for(int j = 0; j < N-1; ++j) { +            if(static_cast<int>(D[j][i]+D[j+1][i]) == a) { +                return true; +            } +        } +    } + +    return false; +} + +int main() { +    double A[3][4] = { +        {40.2, 10, 10.2, 1}, +        {40.2, 10, 10.2, 1}, +        {40.2, 10, 10.2, 1}, +    }; + +    cout << func(A, 20); +     +    return 0; +} diff --git a/Year_1/Programming_1/ex1_25_06_19.cc b/Year_1/Programming_1/ex1_25_06_19.cc new file mode 100644 index 0000000..f28a09b --- /dev/null +++ b/Year_1/Programming_1/ex1_25_06_19.cc @@ -0,0 +1,45 @@ +#include <iostream> +#include <utility> +#include <vector> + +using namespace std; + +template<int N, int M> +bool func(int (&A)[N][M], short k, double x) { +    for(int i = 0; i < M; ++i) { +        vector<pair<int, int> > v; + +        for(int j = 0; j < N-1; ++j) { +            v.push_back({A[j][i], A[j+1][i]}); +        } +         +        int check_num = 0; + +        for(auto const& i : v) { +            if(i.second == 0) continue; +            double elem = static_cast<double>(i.first) / static_cast<double>(i.second); + +            if(elem < x) +                check_num++; +        } + +        if(check_num == k) +            return true; +    } + + +    return false; +} + +int main() { +    int A[4][3] = { +        {2, 3, 5}, +        {10, 20, 10}, +        {90, 19, 69}, +        {10, 0, 0}, +    }; + +    cout << func(A, 2, 0.005) << endl; // 0 +    cout << func(A, 2, 1.0) << endl; // 1 +    return 0; +} diff --git a/Year_1/Programming_1/ex1_27_01_20.cc b/Year_1/Programming_1/ex1_27_01_20.cc new file mode 100644 index 0000000..1b0e529 --- /dev/null +++ b/Year_1/Programming_1/ex1_27_01_20.cc @@ -0,0 +1,47 @@ +#include<iostream> +#define N 3 +#define M 4 +using namespace std; + +float* func(int** Q, float a, float b) { +    float* arr = new float[M]; + +    int a_i = static_cast<int>(a); +    int b_i = static_cast<int>(b); + +    for(int i = 0; i < M; ++i) { +        short counter{}; +        float sum{}; +        for(int j = 0; j < N; ++j) { +            if(Q[j][i]) { +                if(Q[j][i] >= a_i && Q[j][i] <= b_i) { +                    ++counter; +                    sum += Q[j][i]; +                } +            } +        } + +        arr[i] = (sum / static_cast<float>(counter)); +    } + +    return arr; +} + +int main() { +    int** A = new int*[N]; +    for(int i = 0; i < N; ++i) { +        A[i] = new int[M]; +        for(int j = 0; j < M; ++j) +            A[i][j] = 24; +    } + +    float* arr = func(A, 1.0, 33.0);     +     +    for(int j = 0; j < M; ++j) +        cout << arr[j] << ' '; + +    delete[] A; +    delete arr; + +    return 0; +} diff --git a/Year_1/Programming_1/ex1_28_01_19.cc b/Year_1/Programming_1/ex1_28_01_19.cc new file mode 100644 index 0000000..a027e82 --- /dev/null +++ b/Year_1/Programming_1/ex1_28_01_19.cc @@ -0,0 +1,30 @@ +#include <iostream> +#include <algorithm> +#include <vector> + +using namespace std; + +template<int N> +bool func(int (&A)[N][N], double w) { +    vector<int> l; +    for(int i = N-1, j = 0; j < N; --i, j++) { +        l.push_back(A[i][j]); +    } + +    auto min_max = minmax_element(begin(l), end(l)); + +    return (static_cast<double>(*min_max.first)/static_cast<double>(*min_max.second)) <= w; +} + +int main() { +    int A[3][3] = { +        {1, 2, 3,}, +        {67, 10, 56}, +        {10, 0, 1}, +    }; + +    cout << func(A, 0.2) << endl; // False +    cout << func(A, 0.4) << endl; // True + +    return 0; +} diff --git a/Year_1/Programming_1/ex1_tutorato_03_12_19.cc b/Year_1/Programming_1/ex1_tutorato_03_12_19.cc new file mode 100644 index 0000000..d8d61b0 --- /dev/null +++ b/Year_1/Programming_1/ex1_tutorato_03_12_19.cc @@ -0,0 +1,33 @@ +#include <iostream> +#include <cctype> + +using namespace std; + +template<int N> +bool* func(string (&A)[N]) { +    bool* a2 = new bool[N]; + +    for(int i = 0; i < N; ++i) { +        bool is_pal = true; +        for(int j = 0, z = A[i].length()-1; j < z; ++j, --z ) { +            if(tolower(A[i].at(j)) != tolower(A[i].at(z))) { +                is_pal = false; +                break; +            } +        } +        a2[i] = is_pal; +    } + +    return a2; +} + +int main() { +    string t[] = {"oslo", "yam_a_may", "AnNa"}; +    auto a = func(t); +     +    for(int i = 0; i < 3; ++i) { +        cout << a[i] << ' '; +    } + +    return 0; +} diff --git a/Year_1/Programming_1/ex1_tutorato_14_01_20.cc b/Year_1/Programming_1/ex1_tutorato_14_01_20.cc new file mode 100644 index 0000000..557b0ee --- /dev/null +++ b/Year_1/Programming_1/ex1_tutorato_14_01_20.cc @@ -0,0 +1,27 @@ +#include<iostream> +#include<algorithm> +#include<vector> + +using namespace std; + +template<int N> +bool func(int (&A)[N][N], double w) { +    vector<int> list; +    for(int i = 0, j = N-1; i < N; ++i, --j)  +        list.push_back(A[j][i]); + +    auto minmax = minmax_element(begin(list), end(list)); +    return (static_cast<double>(*minmax.first)/static_cast<double>(*minmax.second)) <= w; +} + +int main() { +    int A[3][3] = { +        {1, 2, 3}, +        {1, 2, 3}, +        {1, 2, 3}, +    }; + +    cout << func(A, 0.1); + +    return 0; +} diff --git a/Year_1/Programming_1/ex2_04_12_19.cc b/Year_1/Programming_1/ex2_04_12_19.cc new file mode 100644 index 0000000..66b2895 --- /dev/null +++ b/Year_1/Programming_1/ex2_04_12_19.cc @@ -0,0 +1,39 @@ +#include <iostream> +#include <string> + +using namespace std; + +template<int N> +bool func(string (&Q)[N][N], string s) { +    short n_presents[2] = {0}; +    for(int i = 0; i < N; ++i) { +        if(Q[i][i].find(s) != std::string::npos) +            n_presents[0]++; +    } +     +    for(int i = 0, j = N-1; i < N; ++i, --j) { +        cout << Q[j][i].find(s) << endl; +        if(Q[j][i].find(s) != std::string::npos) +            n_presents[1]++; +    } + +    return n_presents[0] > n_presents[1]; +} + +int main() { +    string a1[3][3] = { +        {"ciaoneXX", "fra", "zio"}, +        {"oh, ma che", "eXXh", "sta cosa?"}, +        {"greg", "no", "foXXrse"} +    }; +     +    string a2[3][3] = { +        {"ciaoneXX", "fra", "ziYYo"}, +        {"oh, ma che", "eXXYYh", "sta cosa?"}, +        {"gregYY", "no", "foXrse"} +    }; + +    cout << func(a1, "XX") << ' ' << func(a2, "YY") << endl; + +    return 0; +} diff --git a/Year_1/Programming_1/ex2_06_12_18.cc b/Year_1/Programming_1/ex2_06_12_18.cc new file mode 100644 index 0000000..50744ba --- /dev/null +++ b/Year_1/Programming_1/ex2_06_12_18.cc @@ -0,0 +1,34 @@ +#include<iostream> +#define N 3 +#define M 4 + +using namespace std; + +bool* func(string (*A)[M], short k, string s) { +    bool* arr = new bool[N]; + +    for(int i = 0; i < N; ++i) { +        short counter{}; +        for(int j = 0; j < M; ++j) { +            if(A[i][j].find(s) != std::string::npos) +                ++counter; +        } + +        arr[i] = (counter >= k); +    } +     +    return arr; +} + +int main() { +    string Arr[N][M] = { +        {"Ciaone", "Come one va?", "We one", ""}, +        {"Ciaoe", "Come va?", "We one", ""}, +        {"Ciaone", "Come one va?", "We one", ""}, +    }; + +    bool* v = func(Arr, 2, "one"); +    for(int i = 0; i < N; ++i) cout << v[i] << ' '; + +    return 0; +} diff --git a/Year_1/Programming_1/ex2_08_03_18.cc b/Year_1/Programming_1/ex2_08_03_18.cc new file mode 100644 index 0000000..88d55e1 --- /dev/null +++ b/Year_1/Programming_1/ex2_08_03_18.cc @@ -0,0 +1,49 @@ +#include<iostream> +using namespace std; + +template<int N, int M> +bool func(string (&A)[N][M], short w) { +    bool check{false}; +    for(int i = 0; i < N; ++i) { +        bool counter{false}; +        string str{""}; +        string str2{""}; +        for(int j = 0; j < M; ++j) { +            if(A[i][j].length() < w) { +                counter = false; +                continue; +            } + +            if(!counter) { +                str = A[i][j].substr(0, w); +                 +                counter = true; +                continue; +            }  + +            str2 = A[i][j].substr(A[i][j].length()-w, w); + +            if(str == str2) { +                check = true; +                break; +            } else { +                j--; +                counter = false; +            } +        } + +        if(check) break; +    } + +    return check; +} + +int main() { +    string A[3][3] = { +        {"parola", "allorapar", "bla",}, +        {"", "red", "red",}, +        {"blue", "", "",}, +    }; +    cout << func(A, 3); +    return 0; +} diff --git a/Year_1/Programming_1/ex2_18_02_19.cc b/Year_1/Programming_1/ex2_18_02_19.cc new file mode 100644 index 0000000..8195fb9 --- /dev/null +++ b/Year_1/Programming_1/ex2_18_02_19.cc @@ -0,0 +1,43 @@ +#include<iostream> +#include<cctype> + +using namespace std; + +short func(string (*M)[4], unsigned short k, unsigned short s) { +    short counter{}; + +    for(int i = 0; i < 4; ++i) { +        short _rowcounter{}; +        for(int j = 0; j < 3; ++j) { +            short _vocals{}; +            for(char& c : M[j][i]) { +                switch(tolower(c)) { +                    case 'a': case 'e': case 'i': case 'o': case 'u': +                        ++_vocals; +                        break; +                    default: break; +                } +            } + +            if(_vocals < s) ++_rowcounter; +        } + +        if(_rowcounter >= k) ++counter; +    } + +    return counter; +} + + +int main() { +    string M[3][4] = { +        {"HEI", "we", "ciao", "com'è?"}, +        {"HEI", "we", "ciao", "com'è?"}, +        {"HEI", "we", "ciao", "com'è?"}, +    }; +     +    cout << func(&M[0], 2, 3); + + +    return 0; +} diff --git a/Year_1/Programming_1/ex2_23_07_19.cc b/Year_1/Programming_1/ex2_23_07_19.cc new file mode 100644 index 0000000..3b0ed37 --- /dev/null +++ b/Year_1/Programming_1/ex2_23_07_19.cc @@ -0,0 +1,44 @@ +#include<iostream> + +using namespace std; + +template<int N> +bool func(string (&Q)[N][N]) { +    string stringaL = Q[0][0]; +    string stringaC = Q[0][0]; +    for(int i = 0; i < N; ++i) { +        if(Q[i][i].length() > stringaL.length()) +            stringaL = Q[i][i]; +        else if(Q[i][i].length() < stringaC.length()) +            stringaC = Q[i][i]; +    } + +    short vocaliL{}; +    short vocaliC{};  + +    for(auto& c : stringaL) { +        c = tolower(c); +        if(c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u') +            ++vocaliL; +    } +     +    for(auto& c : stringaC) { +        c = tolower(c); +        if(c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u') +            ++vocaliC; +    } +     +    return vocaliL < vocaliC; +} + +int main() { +    string A[3][3] = { +        {"lllll", "bb", "cc"}, +        {"AAA", "e", "cc"}, +        {"AAA", "bb", "cc"}, +    }; + +    cout << func(A); +     +    return 0; +} diff --git a/Year_1/Programming_1/ex2_27_01_20.cc b/Year_1/Programming_1/ex2_27_01_20.cc new file mode 100644 index 0000000..25caf5b --- /dev/null +++ b/Year_1/Programming_1/ex2_27_01_20.cc @@ -0,0 +1,57 @@ +#include<iostream> +#include<set> + +using namespace std; + +template<int N, int M> +double func(string (&A)[N][M], string x, string y, short k, short w) { +    set<char> s; +    for(char const&c : x) s.insert(c); +    x = ""; +    for(char const&c: s) x+=c; + +    s.clear(); +    for(char const&c : y) s.insert(c); +    y = ""; +    for(char const&c: s) y+=c; + +    for(int i = 0; i < N; ++i) { +        for(int j = 0; j < M; ++j) { +            s.clear(); +            for(char const&c : A[i][j]) s.insert(c); +            A[i][j] = ""; +            for(char const&c: s) A[i][j]+=c; +        } +    } + +    short counter{}; +    for(int i = 0; i < M; ++i) { +        short k_i{}; +        for(int j = 0; j < N; ++j) { +            if(A[j][i].length() < w) continue; +            short char_counter{}; +            for(char const& c: A[j][i]) { +                if(x.find(c) != std::string::npos && y.find(c) != std::string::npos) +                    ++char_counter; +            } +            if(char_counter >= w) +                ++k_i; +        } +        if(k_i >= k) +            ++counter; +    } + +    return static_cast<double>(counter*100)/static_cast<double>(M); +} + +int main() { +    string A[3][4] = { +        {"we,", "heila", "ciaone", ""}, +        {"we,", "heila", "ciaone", ""}, +        {"we,", "heila", "ciaone", ""}, +    }; + +    cout << func(A, "we", "we", 2, 2);     + +    return 0; +} diff --git a/Year_1/Programming_1/ex2_28_01_19.cc b/Year_1/Programming_1/ex2_28_01_19.cc new file mode 100644 index 0000000..7ad8398 --- /dev/null +++ b/Year_1/Programming_1/ex2_28_01_19.cc @@ -0,0 +1,36 @@ +#include <iostream> +#include <cctype> + +using namespace std; + +template<int N, int M> +bool func(string (&A)[N][M], unsigned short k, unsigned short s) { +    for(int i = 0; i < N; ++i) { +        short string_num = 0; +        for(int j = 0; j < M; ++j) { +            short upper_chars = 0; +            for(auto const& c : A[i][j]) { +                if(isupper(c)) +                    ++upper_chars; +            } + +            if(upper_chars >= s) +                ++string_num; +        } + +        if(string_num >= k)  +            return false; +    }    + +    return true; +} + +int main() { +    string A[2][3] = { +       {"suce", "oHh", "b"}, +       {"111", "HAAa", "B"} +    }; + +    cout << func(A, 2, 1) << endl; +    return 0; +} diff --git a/Year_1/Programming_1/ex2_tutorato_14_01_20.cc b/Year_1/Programming_1/ex2_tutorato_14_01_20.cc new file mode 100644 index 0000000..b54860e --- /dev/null +++ b/Year_1/Programming_1/ex2_tutorato_14_01_20.cc @@ -0,0 +1,38 @@ +#include<iostream> + +using namespace std; + +template<int N, int M> +bool func(string (&A)[N][M], unsigned short k, unsigned short s) { +    for(int i = 0; i < N; ++i) { +        short count_s{}; +        for(int j = 0; j < M; ++j) { +            short upper_cs{}; +            for(auto const& c : A[i][j]) { +                if(isupper(c)) +                    ++upper_cs; +            } +         +            if(upper_cs < s) +                ++count_s; +        } + +        if(count_s >= k) +            return true; +    } +     +    return false; +} + +int main() { +    string A[3][4] = { +        {"ProVA", "CiAone", "RAed"}, +        {"IVVA", "CiAOO", "BLUE"}, +        {"AA", "AA", "AA3#"}, +    }; + +    cout << func(A, 3, 1); +         + +    return 0; +} diff --git a/Year_1/Programming_1/h9_1.cc b/Year_1/Programming_1/h9_1.cc new file mode 100644 index 0000000..12c629d --- /dev/null +++ b/Year_1/Programming_1/h9_1.cc @@ -0,0 +1,27 @@ +#include <iostream> + +// Array NxM. Trovare media dei valori differenza tra gli elementi della diagonale principale e secondaria + +int main() { +    const short N = 4, M = 3; +    int A[N][M] = { +        {3, 1, 5}, +        {5, 3, 1}, +        {8, 7, 4}, +        {4, 7, 4}, +    }; + +    int diff1 = A[0][0], diff2 = A[N-1][0]; + +    // Condizione necessaria per matrici in cui M != N +    short cond = (N < M) ? N : M; +     +    for(int i = 1, j = N-1, j2 = 1; i < cond; ++i, --j, ++j2) { +        diff1 -= A[i][i]; +        diff2 -= A[j][j2]; +    } + +    std::cout << static_cast<float>((diff1+diff2)/2) << std::endl; + +    return 0; +} diff --git a/Year_1/Programming_1/h9_2.cc b/Year_1/Programming_1/h9_2.cc new file mode 100644 index 0000000..0e9c853 --- /dev/null +++ b/Year_1/Programming_1/h9_2.cc @@ -0,0 +1,32 @@ +#include <iostream> + +// Array NxM e numero p. Stampare le medie per ogni colonna dispari dei valori x <= p + +int main() { +    const short N = 4, M = 4; +    int p = 9; +    int A[N][M] = { +        {3, 1, 5, 5}, +        {5, 3, 1, 5}, +        {8, 7, 4, 5}, +        {4, 7, 4, 5}, +    }; +    // Condizione necessaria per matrici in cui M != N +    short cond = (N < M) ? N : M; + +    for(int i = 1; i < cond; i+=2) { +        int sum = 0, c = 0; +        for(int j = 0; j < M; ++j) { +            if(A[j][i] <= p) { +                sum += A[j][i]; +                c++; +            } +        } + +        if(c > 0) { +            std::cout << static_cast<float>(sum)/c << '\n'; +        } +    } + +    return 0; +} diff --git a/Year_1/Programming_1/h9_3.cc b/Year_1/Programming_1/h9_3.cc new file mode 100644 index 0000000..c9ef418 --- /dev/null +++ b/Year_1/Programming_1/h9_3.cc @@ -0,0 +1,31 @@ +#include <iostream> +#include <algorithm> + +// Dato array V=NxM e uno grande W, trovare il numero di elementi presenti in W che sono compresi in ogni riga di V + +int main() { +    std::cout << __cplusplus; +    const auto N = 4, M = 4, L = 3; +    int V[N][M] = { +        {3, 1, 5, 50}, +        {5, 3, 1, 5}, +        {8, 7, 4, 5}, +        {4, 7, 4, 5}, +    }; + +    int W[L] = {4, 50, 2}; + +    for(auto i = 0; i < N; ++i) { +        const auto range = std::minmax_element(std::begin(V[i]), std::end(V[i])); +        int count{0}; +        for(auto const& j : W) { +            if(j >= *range.first && j <= *range.second) +                count++; +        } + +        std::cout << count << std::endl; +    } + + +    return 0; +} diff --git a/Year_1/Programming_1/h9_4.cc b/Year_1/Programming_1/h9_4.cc new file mode 100644 index 0000000..cd534b3 --- /dev/null +++ b/Year_1/Programming_1/h9_4.cc @@ -0,0 +1,40 @@ +#include <iostream> +#include <algorithm> + +// Data matrice V di NxM, array A e numero w<M, trovare se c'è almeno una riga di V con almeno w elementi maggiori di ogni elemento di A + +const auto N{4}, M{4}, k{3}, w{2}; + +bool row_alg(int V[N][M], int* A, int w) { +    // È ovvio che sia presente almeno 0 elementi, quindi non ha senso continuare +    if(w <= 0) return true; +    auto max_element_of_A = std::max_element(A,A+k); +     +    for(auto i = 0; i < N; ++i) { +        int counts = 0; +        for(auto j = 0; j < M; ++j) { +            if(V[i][j] > *max_element_of_A) +                counts++; + +            if(counts == w) +                return true; +        } +    }  + +    return false; +} + +int main() { +    int V[N][M] = { +        {3, 59, 5, 54}, +        {5, 3, 1, 5}, +        {8, 7, 4, 5}, +        {4, 7, 4, 5}, +    }; + +    int A[k] = {4, 50, 2}; + +    std::cout << row_alg(V, A, w) << std::endl; + +    return 0; +} diff --git a/Year_1/Programming_1/h9_5.cc b/Year_1/Programming_1/h9_5.cc new file mode 100644 index 0000000..a2e3f8b --- /dev/null +++ b/Year_1/Programming_1/h9_5.cc @@ -0,0 +1,51 @@ +#include <iostream> +#include <algorithm> +#include <map> + +// Date due matrici e un numero p, stampare i valori che compaiono p volte dalla prima alla seconda matrice + +int main() { +    const auto N{4}, M{4}, L{4}, Q{4}, p{4}; +    int V[N][M] = { +        {3, 1, 5, 50}, +        {5, 3, 1, 5}, +        {8, 7, 4, 5}, +        {4, 7, 4, 5}, +    }; + +    int W[L][Q] = { +        {30, 3, 5, 50}, +        {51, 25, 12, 5}, +        {90, 12, 4, 5}, +        {4, 6, 4, -3}, +    }; + +    std::map<int, int> values; + +    for(auto i = 0; i < N; ++i) { +        for(auto j = 0; j < M; ++j) { +            if(values.find(V[i][j]) == values.end()) +                values.insert({V[i][j], 1}); +            else +                values[V[i][j]] += 1; +        } +    } + +    for(auto i = 0; i < L; ++i) { +        for(auto j = 0; j < Q; ++j) { +            auto elem = values.find(W[i][j]); +            if(elem != values.end()) { +                if(elem->second >= p) { +                    std::cout << W[i][j] << ' '; +                } else { +                    std::cout << "x "; +                } +            } else { +                std::cout << "x "; +            } +        } +        std::cout << '\n'; +    } + +    return 0; +} diff --git a/Year_1/Programming_1/h9_6.cc b/Year_1/Programming_1/h9_6.cc new file mode 100644 index 0000000..d889c43 --- /dev/null +++ b/Year_1/Programming_1/h9_6.cc @@ -0,0 +1,35 @@ +#include <iostream> +#include <algorithm> + +int main() { +    const auto N{4}, M{4}, s{50}, z{2}; +    int V[N][M] = { +        {3, 1, 5, 50}, +        {5, 3, 1, 5}, +        {8, 7, 4, 5}, +        {4, 7, 4, 5}, +    }; + +    int W[M] = {0}; + +    for(auto i = 0; i < N; ++i) { +        bool finish{false}; +        for(auto j = 0, index = 0; j < M-1; ++j, ++index) { +            int sum{0}; +            for(auto zz = index; zz < index+z; ++zz) { +                sum+=V[i][zz]; +                if(sum >= s) { +                    W[i] = 1; +                    finish = true; +                    break; +                } +            } +            if(finish) break; +        } +    } + +    for(auto i = 0; i < N; ++i) +        std::cout << W[i] << ' '; + +    return 0; +} diff --git a/Year_1/Programming_1/lab_14_12_18.cc b/Year_1/Programming_1/lab_14_12_18.cc new file mode 100644 index 0000000..02bbd69 --- /dev/null +++ b/Year_1/Programming_1/lab_14_12_18.cc @@ -0,0 +1,149 @@ +#include<iostream> +#include<cmath> +#define DIM 30 + +using namespace std; + +class A { +protected: +	double* ptr; +	short len; + +	void getPtr(ostream& os) { +		os << "ptr=["; +		for(int i = 0; i < len; ++i) {  +			os << ptr[i] << ' '; +		} + +		os << "]"; +	} +public: +	A(short n) : len{n} { +		srand(time(NULL)); +		ptr = new double[n]; +		for(int i = 0; i < n; ++i) { +			ptr[i] = (double) rand() / RAND_MAX; +		} +	} + +	~A() { +		delete ptr; +	} +	virtual double foo(short a) const = 0; +	virtual void print(ostream& os) = 0; + +	double operator[](int i) { +		return ptr[i]; +	} +}; + +class B : public A { +public: +	B(short m, double s) : A{m}, alpha{s} {} +	double foo(short b) const { +		return static_cast<double>(log(1+extract(b))); +	} +	void print(ostream& os) { +		os << "B, "; +		getPtr(os); +		os << ", alpha=" << alpha; +	} + +private: +	double alpha; +	double extract(short s) const { +		return (ptr[s%len]+alpha)/2; +	} + +}; + +template<typename T> +class C; + +template<> +class C<double> : public A { +public: +	C(short n) : A{n} { +		x = static_cast<double>(log(1+n)); +	} + +	double foo(short r) const { +		return g(r*x); +	} + +	double g(double k) const { +		return 3*k; +	} + +	void print(ostream& os) { +		os << "C<double> "; +		getPtr(os); +		os << ", x=" << x; +	} +private: +	short x; +}; + +template<> +class C<int> : public A { +public: +	C(short n) : A{n} { +		x = g(n); +	} + +	double foo(short r) const { +		return g(r*x); +	} + +	short g(short k) const { +		return 3*k; +	} + +	void print(ostream& os) { +		os << "C<short> "; +		getPtr(os); +		os << ", x=" << x; +	} +private: +	short x; +}; + +ostream& operator<<(ostream& os, A& a) { +	a.print(os); + +	return os; +} + +int main() { +	srand(328832748); +	A* vett[DIM]; +	double maxfoo{-11111111.0}; +	double g_sum{}; +	for(int i=0; i<DIM; i++) { +		short n=1+rand()%5; +		switch(rand()%3) { +			case 0: vett[i]= new B(n, n/100.0); break; +			case 1: { +				vett[i]= new C<double>(n); +				auto refC = dynamic_cast<C<double>*>(vett[i]); +				g_sum+=refC->g(5); +				break; +			} +			case 2: vett[i]= new C<int>(n); + +		} + +		double foo5 = vett[i]->foo(5); +		if(foo5 > maxfoo) +			maxfoo = foo5; +	} + +	for(int i = 0; i < DIM; ++i) { +		cout << i+1 << ")" << *(vett[i]) << ", foo(5)=" << vett[i]->foo(5) << endl; +	} + +	cout << "max(foo(5))=" << maxfoo << " sum(g(5))=" << g_sum << endl; +	cout << (*vett[2])[0] << endl; + +	return 0; +} diff --git a/Year_1/Programming_1/lab_15_02_19_D.cc b/Year_1/Programming_1/lab_15_02_19_D.cc new file mode 100644 index 0000000..b050e72 --- /dev/null +++ b/Year_1/Programming_1/lab_15_02_19_D.cc @@ -0,0 +1,159 @@ +#include<iostream> +#include<stdexcept> +#define DIM 50 + +using namespace std; + +class A { +public: +	A(short m, short k) : len{m} { +		ptr = new int[len]; +		srand(time(NULL)); +		 +		for(int i = 0; i < len; ++i) { +			ptr[i] = rand()%k + 1; +		} +	} + +	~A() { +		delete ptr; +	} + +	virtual double const f() = 0; +	void print(ostream& os) { +		os << typeid(*this).name() << ", ptr=[ "; +		for(int i = 0; i < len; ++i) +			os << ptr[i] << ' '; + +		os << "]"; +	} + +	int get(short i) { +		if(i >= len) +			throw out_of_range{"out of range"}; + +	 	return ptr[i]; +	} + +	short getLen() { +		return len; +	} + +	int& operator[](int x){  +		return ptr[x]; +	} + +	const int& operator[](int x) const {  +		return ptr[x]; +	} +	 +private: +	int* ptr; +	short len; + +}; + +class B : public A { +public: +	B(short m, short k, double y) : A{m, k}, p{y} {} +	double const f() { +		int sum{}; +		for(int i = 0; i < getLen(); ++i) { +			int t = get(i); + +			if((t%2) == 0) { +				sum+=t; +			} +		} + +		if(sum == 0) +			return 0; + +		return static_cast<double>(sum/p); +	} + +	void print(ostream& os) { +		A::print(os); +		os << ", p=" << p; +	} +private: +	double p; +}; + +class C : public A { +public: +	C(short n, short k, char c) : A{n, k}, x{c} {} + +	double const f() { +		int sum{}; +		short c{}; +		for(int i = 0; i < getLen(); ++i) { +			int t = get(i); + +			if((t%2) == 1) { +				sum+=t; +				++c; +			} +		} + +		if(c == 0) +			return 0; + +		return static_cast<double>(sum/c); +	} + +	string const g(char c) { +		string t{""}; +		t+=x; +		t+=c; + +		return t; +	} + +	void print(ostream& os) { +		A::print(os); +		os << ", x=" << x; +	} +private: +	char x; +}; + +ostream& operator<<(ostream& os, A& a) { +	a.print(os); + +	return os; +} + +int main() { +	srand(111222333); +    A *vett[DIM]; + +	string conc{""}; +    for(int i=0; i<DIM; i++){ +		short n=1+rand()%10; +		short m = 1+rand()%15; +		if(rand()%2==0) +	 	 	vett[i]= new B(n, m, rand()/(double) RAND_MAX+0.05); +		else { +	  		vett[i]= new C(n, m, (char) (rand()%('z' - 'a' + 1) + 'a')); +			auto cpnt = dynamic_cast<C*>(vett[i]); +			conc+=cpnt->g('h'); +		} +	} + +	double sum{}; +    for(int i = 0; i < DIM; i++) { +		auto fval = vett[i]->f(); +		cout << i << ") " << *(vett[i]) << ", f()=" << fval << endl;  +		sum+=fval; +	} +	 +	cout << "PUNTO 2:\n\tavg(f())=" << sum/DIM << ", g('h')=" << conc << endl; +	cout << "PUNTO 3:\n"; +	cout << (*(vett[0])).get(0) << endl; +	(*(vett[0]))[0] = 24; +	cout << (*(vett[0])).get(0) << endl; + +	 +	return 0; +} diff --git a/Year_1/Programming_1/lab_26_07_19.cc b/Year_1/Programming_1/lab_26_07_19.cc new file mode 100644 index 0000000..91822d9 --- /dev/null +++ b/Year_1/Programming_1/lab_26_07_19.cc @@ -0,0 +1,164 @@ +#include<iostream> +#include<cmath> +#include<typeinfo> + +#define DIM 30 + +using namespace std; + +class A { +public: +    A(short m, int a, int b) : len(m) { +        srand(time(NULL)); +        vec = new int[m]; +         +        for(int i = 0; i < m; ++i) { +            vec[i] = rand()%b + a; +        } +    } + +    ~A() { +        delete vec; +    } + +    virtual double foo(short a) = 0; +    short getLen() { +        return len; +    } + +    virtual void print(ostream& o) = 0; + +    int operator()(int i1, int i2) { +        if(i1 < 0) i1 = 0; +        if(i2 > len) i2 = len; + +        int sum = 0; +         +        for(int i = i1; i < i2; ++i) { +            sum+=vec[i]; +        } + +        return sum; +    } +     +protected: +    int get(short i) { +        return vec[i]; +    } +private: +    int* vec; +    short len; +}; + +class B : public A { +public: +    B(short m, int x, int y) : A(m, x, y) { +        srand(time(NULL)); +        short rand_num = rand()%m; +        p = get(rand_num); +    } +     +    double foo(short a) { +        return static_cast<double>(prod(a)) / static_cast<double>(p); +    } + +    void print(ostream& os) { +        os << "vec=["; +        for(int i = 0; i < getLen(); ++i) { +            os << get(i) << ' '; +        } +        os << "], p=" << p; +    } +protected: +    int prod(short s) { +        int index = s%getLen(); +        int _prod = 1; +        for(int i = index; i < getLen(); ++i) { +            _prod *= get(i); +        } + +        return _prod; +    } +private: +    int p; +}; + + +template<class T> class C : public A { +public: +    C(short n, int a, int b) : A(n, a, b) { +        if(typeid(T) == typeid(short)) { +            x = n; +        } else { +            x = log(1+pow(sin(n), 2)); +        } + +    } +     +    double foo(short r) { +        return g(r); +    } + +    T g(T k) { +        return 2*x*(k+1); +    } + +    void print(ostream& os) { +        os << "vec=["; +        for(int i = 0; i < getLen(); ++i) { +            os << get(i) << ' '; +        } +        os << "], x=" << x; +    } +private: +    T x; +}; + +ostream& operator<<(ostream& os, A& a) { +    os << typeid(a).name() << ", "; +    a.print(os); +    return os; +} + +int main() { +    srand (time(0));  +    A* vett[DIM]; +    double sum_cd = 0; +    short num_cd = 0; +    for(int i=0; i < DIM; ++i) { +        short n = 1+rand()%10; +        switch ( rand ()%3) { +        case 0: +            vett[i]= new B(n, rand()%5 + 1, rand()%10 + 5); +            break ; +        case 1: { +            vett[i]= new C<double>(n, rand()%5 + 1, rand()%10 + 5);  +            ++num_cd; +            C<double>& cc = dynamic_cast<C<double>& >(*(vett[i])); +            sum_cd += cc.g(5); +             +            break ; +        } +        case 2: +            vett[i]= new C<short>(n, rand()%5 + 1, rand()%10 + 5); +        } +    } + +    double max_foo = -100000; + +    for(int i = 0; i < DIM; ++i) { +        auto foo3 = vett[i]->foo(3); + +        if(foo3 > max_foo) { +            max_foo = foo3; +        } +        cout << (*(vett[i])) << ", foo(3)=" << foo3 <<endl; +    } +     +    // max_foo => Valore maggiore calcolato per foo(3) +    // sum_cd / num_cd => media valori g(5) per C<Double>  +     +    cout << (*vett[0])(0, 2) << endl; + +    return 0; +} diff --git a/Year_1/Programming_1/lab_28_02_19_A.cc b/Year_1/Programming_1/lab_28_02_19_A.cc new file mode 100644 index 0000000..ff2eaae --- /dev/null +++ b/Year_1/Programming_1/lab_28_02_19_A.cc @@ -0,0 +1,166 @@ +#include<iostream> +#include<stdexcept> +#include<cstdlib> +#include<cmath> +#define DIM 50 + +using namespace std; + +class A { +public: +	A(short m) : len{m} { +		arr = new short[m]; +		srand(time(NULL)); +		for(int i = 0; i < m; ++i) { +			arr[i] = rand()%10+1; +		} +	} + +	~A() { +		delete[] arr; +	} + +	virtual double f(short a) = 0;	 +	virtual void print(ostream& os) = 0; + +	short getLen() { +		return len; +	} + +protected: +	short get(short i) { +		if(i < 0 || i >= getLen()) +			throw out_of_range{"out of arr' range"}; + +		return arr[i]; +	} + +private: +	short* arr; +	short len; +}; + +template<typename T> +class B : public A { +public: +	B(short m, char c) : A{m} { +		if(typeid(T) == typeid(char)) { +			x = c; +		} else if(typeid(T) == typeid(string)) { +			for(int i = 0; i < m; ++i)  +				x+=c; + +		} else { +			// tipo non gestito +			throw bad_typeid{};  +		} +	} +	 +	double f(short a) override { +		double n = static_cast<double>(getLen() - a); +		double sum{}; +		for(int i = a; i < getLen(); ++i)  +			sum+=get(i); +		 +		return sum/n; +	} + +	void print(ostream& os) override { +		os << "B<" << (typeid(T) == typeid(char) ? "char" : "string") << "> arr=["; +		for(int i = 0; i < getLen(); ++i) +			os << get(i) << ' '; +		os << "], x=" << x; +	} + +	double foo(short s) { +		return log(s) + sin(f(s)); +	} +private: +	T x; +}; + +class C : public A { +public: +	C(short n, int k) : A{n}, y{k} {} + +	double f(short a) override { +		srand(time(NULL)); +		short rand_elem = get(rand()%getLen()); +		 +		return static_cast<double>(a+y)/static_cast<double>(rand_elem); +	} + +	double g(short w) { +		// sicuri che non debba convertire a double? qui sarà sempre 0, forse 1 +		return static_cast<double>(sin(w+y)); +	} + +	void print(ostream& os) override { +		os << "C arr=["; +		for(int i = 0; i < getLen(); ++i) +			os << get(i) << ' '; +		os << "], y=" << y; +	} + +	C& operator++() { +		++y; +		return *this; +	} +private: +	int y; +}; + +ostream& operator<<(ostream& os, A& a) { +	a.print(os); + +	return os; +}  + +int main() { +	srand(111222333); + +	A *vett[DIM]; + +	double max_f3{-100000000.0}; +	short c_count{}; +	double gsum{}; +	short i_to_p{-1}; +	 +	for(int i=0; i<DIM; i++) { +		short n=1+rand()%10; +		switch(rand()%3) { +		case 0:  +		{ +			vett[i]= new C(n, rand()%10 + 1); +			i_to_p = i; +			auto cref = dynamic_cast<C&>(*(vett[i])); +			gsum+=cref.g(5); +			++c_count; +	    	break; +	  	} +		case 1: +	    	vett[i]= new B<string>(n, rand()%('z' - 'a' + 1) + 'a'); +	    	break; +	  	case 2:  +	    	vett[i]= new B<char>(n, rand()%('z' - 'a' + 1) + 'a'); +		} + +		auto f3 = vett[i]->f(3); +		if(f3 > max_f3) +			max_f3 = f3; +    } + +	for(int i = 0; i < DIM; ++i) +		cout << i+1 << ')' << *(vett[i]) << ", f(3)=" << vett[i]->f(3) << endl; + +	cout << "max f(3)=" << max_f3; +	cout << " avg g(5)=" << (static_cast<double>(gsum/c_count)) << endl; +	if(i_to_p >= 0) { +		cout << *(vett[i_to_p]) << endl; +		auto cref = dynamic_cast<C*>(vett[i_to_p]); +		++(*cref); +		cout << *(vett[i_to_p]) << endl; +	} +	 +	return 0; +} diff --git a/Year_1/Programming_1/lab_28_02_19_B.cc b/Year_1/Programming_1/lab_28_02_19_B.cc new file mode 100644 index 0000000..4639f66 --- /dev/null +++ b/Year_1/Programming_1/lab_28_02_19_B.cc @@ -0,0 +1,162 @@ +#include<iostream> +#include<cstdlib> +#include<cmath> +#define DIM 50 +using namespace std; + +class A { +public: +	A(short m, char c) : len{m} { +		ptr = new char[len]; +		srand(time(NULL)); +		for(int i = 0; i < m; ++i) { +			ptr[i] = static_cast<char>(rand()%(c-'a'+1) + 'a'); +		} +	} +	A(const A& a) = default; +	~A() { +		delete ptr; +	} + +	virtual string elab(short a, char c) = 0; +	virtual void print(ostream& o) = 0; +	short getLen() { +		return len; +	} + +	char& operator[](int i) { +		return ptr[i]; +	} + +protected: +	char get(short i) { +		if(i > len) +			return ptr[0]; + +		return ptr[i]; +	} + +private: +	char* ptr; +	short len; +}; + +class B : public A { +public: +	B(short m, double y, char c) : A{m, c}, x{y} {} +	double foo(short s) { +		return sin(x+s)/log(x+s); +	} + +	string elab(short a, char c) { +		string st{}; +		for(int i = 0; i < getLen(); ++i) { +			auto ch = get(i); +			if(abs(ch-c) <= a) { +				st+=ch; +			} +		} + +		return st; +	} + +	void print(ostream& os) { +		os << "B ptr=["; +		for(int i = 0; i < getLen(); ++i)  +			os << get(i) << ' '; + +		os << "], x=" << x << ", elab(5, z)=" << elab(5, 'z'); +	} + +private: +	double x; +}; + +template<typename T> +class C : public A { +public: +	C(short n, double k, char c) : A{n, c} { +		if(typeid(T) == typeid(short)) +			y = static_cast<int>(100*k); +		else +			y = k; +	} + +	string elab(short a, char c) { +		string st{}; +		if(getLen() >= a) { +			if(a >= 1) { +				for(int i = 0; i < getLen(); st+=c, ++i); +			} +		} else { +			for(int i = 0; i < getLen(); st += get(i++)); +		} + +		return st; +	} + +	double g(short w) { +		return sin(w+y); +	} +	 +	void print(ostream& os) { +		os << "C<" << typeid(T).name() << "> ptr=["; +		for(int i = 0; i < getLen(); ++i)  +			os << get(i) << ' '; + +		os << "], y=" << y << ", elab(5, z)=" << elab(5, 'z'); +	} +private: +	T y; +}; + +ostream& operator<<(ostream& os, A& a) { +	a.print(os); + +	return os; +} + +int main() { +	srand(time(NULL)); + +	A *vett[DIM]; +	short cb{}, cc{}; +	double sumb{}, sumc{}; + +	for(int i=0; i<DIM; i++) { +  		short n=1+rand()%10; +    	switch(rand()%3) { +      	case 0:  +		{ +        	vett[i]= new B(n, (double) rand()/RAND_MAX, rand()%('z' - 'a' + 1) + 'a'); +			auto temp = dynamic_cast<B&>(*(vett[i])); +			sumb+=temp.foo(5); +			++cb; +			break; +      	} +		case 1: +			vett[i]= new C<double>(n, (double) rand()/RAND_MAX, rand()%('z' - 'a' + 1) + 'a'); +			break; +      	case 2: +		{ +			vett[i]= new C<short>(n, (double) rand()/RAND_MAX, rand()%('z' - 'a' + 1) + 'a'); +			auto temp = dynamic_cast<C<short>&>(*(vett[i])); +			sumc+=temp.g(5); +			++cc; +		} +      	} +	} + +	for(int i=0; i<DIM; i++) { +		cout << *vett[i] << endl; +	} + +	cout << "avg(foo(5))=" << sumb/cb << " ";  +	cout << "avg(g(5))=" << sumc/cc << endl;  + +	cout << endl; +	cout << *vett[0] << endl; +	(*vett[0])[0] = '$'; +	cout << *vett[0] << endl; +	return 0; +} diff --git a/Year_1/Programming_1/lab_28_02_19_C.cc b/Year_1/Programming_1/lab_28_02_19_C.cc new file mode 100644 index 0000000..ac53815 --- /dev/null +++ b/Year_1/Programming_1/lab_28_02_19_C.cc @@ -0,0 +1,185 @@ +#include<iostream> +#include<cmath> +#include<set> +#define DIM 10 + +using namespace std; + +class A { +public: +	A(short m) : len{m} { +		srand(time(NULL)); +		base = new char[len]; +		for(int i = 0; i < len; ++i) { +			base[i] = rand()%('z'-'a'+1)+'a'; +		} +	} + +	~A() { +		delete base; +	} + +	short getLen() { +		return len; +	} + +	virtual string extract(short x) = 0; +	virtual void print(ostream& os) = 0; + +protected: +	char get(short i) { +		return base[i]; +	} + +private: +	char* base; +	short len; +}; + +class B : public A { +public: +	B(short m, double p) : A{m} { +		for(int i = 0; i < m; ++i) { +			switch(get(i)) { +			case 'a': +			case 'e': +			case 'i': +			case 'o': +			case 'u': +				str+=get(i); +			} +		} +	} + +	double foo(short s) { +		return sin(k+s)/log(s+1); +	} + +	string extract(short x) override { +		string x2{""}; +		short inserted{}; +		set<char> chars_a; +		for(int i = 0; i < getLen(); ++i) +			chars_a.insert(get(i)); + +		while(inserted < x) { +			char r_char = rand()%('z'-'a'+1)+'a'; +			if(chars_a.find(r_char) != chars_a.end()) { +				x2+=r_char; +				++inserted; +			}			 +		} + +		return x2; +	} + +	void print(ostream& os) override { +		os << "B: base=["; +		for(int i = 0; i < getLen(); ++i) { +			os << get(i) << ' '; +		} +		os << "], str=" << str << ", k=" << k; +	} +	 +private: +	string str; +	double k; +}; + +class C : public A { +public: +	C(short n, int k) : A{n}, y{k} {} + +	double g(short w) { +		return sin(w+y); +	} + +	string extract(short x) override { +		string x2{""}; +		char* narr = new char[x]; +		short counter_narr{}; + +		for(int i = 0; i < getLen(); ++i) { +			switch(get(i)) { +			case 'a': +			case 'e': +			case 'i': +			case 'o': +			case 'u': +				break; +			default: +				narr[counter_narr++] = get(i); +			} +		} +		for(int i = 0; i < x; ++i) +			x2+=narr[i]; + +		delete[] narr; + +		return x2; +	}	 + +	void print(ostream& os) override { +		os << "C: base=["; +		for(int i = 0; i < getLen(); ++i) { +			os << get(i) << ' '; +		} +		os << "], y=" << y; +	} + +	C operator++(int) { +		C temp{*this}; +		 +		y++; + +		return temp; +	} +private: +	int y; +}; + +ostream& operator<<(ostream& os, A& a) { +	a.print(os); + +	return os; +} + +int main() { +	A *vett[DIM]; + +	double sum{}; +	short i_c{-1}; +	for(int i=0; i<DIM; i++) { +		short n=10+rand()%10; +		switch(rand()%2){ +	  	case 0: { +	    	vett[i]= new C(n, rand()%20 + 1); +			auto cref = dynamic_cast<C*>(vett[i]); +			sum+=cref->g(5); +			i_c = i; +	    	break; +	  	} +		case 1: { +	   	 	vett[i]= new B(n, rand()/(double) RAND_MAX); +			auto bref = dynamic_cast<B*>(vett[i]); +			sum+=bref->foo(5); +	    	break; +		} +		} +    } + +	for(int i=0; i<DIM; i++)  +		cout << *(vett[i]) << ", extract(3)=" << vett[i]->extract(3) <<  endl; + +	cout << sum/DIM << endl; + +	if(i_c >= 0) { +		cout << *vett[i_c] << endl; +		auto cref = dynamic_cast<C*>(vett[i_c]); +		(*cref)++; +		cout << *vett[i_c] << endl; +		 + +	} +	return 0; +} | 
