# 5. Source Code

`//============================================================================// Name        : skew-tent-map.cpp// Author      : Fajar Purnama 152-D8713// Version     : 0.9// Copyright   : Free to distributed with credits to the author.// Description : Source Code for Skew Tent Map in C++//============================================================================#include <iostream>using namespace std;int main() {    cout << "!!!Hello World!!!" << endl; // prints !!!Hello World!!!    int N = 10000*3, bin[N]; float x[N], c = 0.499999; x[0] = 0.3;    // N = number of bits (1 block = 3 bits), c = critical point, x[0] = initial value, x[N] = chaotic sequence, and bin[N] = binary sequence    // From here is 2nd Assignment    for (int i = 0; i < N; ++i){        if (x[i] >= 0 && x[i] < c) {            x[i+1] = x[i]/c;            bin[i] = 0;        } else {            x[i+1] = (1-x[i])/(1-c);            bin[i] = 1;        }    }    // Insert bit parity on every 4th bit    int j = 0, bin_par[N+(N/3)];    for (int i = 0; i < N+(N/3); i += 4){        bin_par[i] = bin[j];        bin_par[i+1] = bin[j+1];        bin_par[i+2] = bin[j+2];        bin_par[i+3] = bin_par[i]^bin_par[i+1]^bin_par[i+2];        j += 3;    }    // Generate error sequence    int bin_err[N+(N/3)]; float p;    cout << "Enter error probability: "; cin >> p;    float c_err = 1-p, x_err[N+(N/3)]; x_err[0]=0.500001;    for (int i = 0; i < N+(N/3); ++i){        if (x_err[i] >= 0 && x_err[i] < c_err) {            x_err[i+1] = x_err[i]/c_err;            bin_err[i] = 0;        } else {            x_err[i+1] = (1-x_err[i])/(1-c_err);            bin_err[i] = 1;        }    }    // Generate receiver side = parity sequence XOR error sequence    int bin_r[N+(N/3)];    for (int i = 0; i < N+(N/3); ++i){        bin_r[i] = bin_par[i] ^ bin_err[i];    }    for (int i = 0; i < N+(N/3); ++i){        cout << bin_err[i];    }    cout << endl;    for (int i = 0; i < N+(N/3); ++i){        cout << bin_par[i];    }    cout << endl;    for (int i = 0; i < N+(N/3); ++i){        cout << bin_r[i];    }    cout << endl << endl;    // total error, detected error, undetected error    float total_error = 0, detected_error = 0, undetected_error = 0;    float p_theory = ((6*p*p)*((1-p)*(1-p))+(p*p*p*p));    for (int i = 0; i < N+(N/3); i += 4){        if((bin_par[i] != bin_r[i])||(bin_par[i+1] != bin_r[i+1])||(bin_par[i+2] != bin_r[i+2])||(bin_par[i+3] != bin_r[i+3])){            ++total_error;        }        if((bin_r[i]^bin_r[i+1]^bin_r[i+2]^bin_r[i+3])==1){            ++detected_error;        }    }    undetected_error = total_error - detected_error;    float p_total_error = total_error/(N/3);    float p_detected_error = detected_error/(N/3);    float p_undetected_error = undetected_error/(N/3);    cout << "total error = " << total_error << endl;    cout << "detected error = " << detected_error << endl;    cout << endl;    cout << "undetected error = " << undetected_error << endl;    cout << "probability of total error = " << p_total_error << endl;    cout << "probability of detected error = " << p_detected_error << endl;    cout << "probability of undetected error = " << p_undetected_error << endl;    cout << "theory = " << p_theory << endl;    return 0;}//============================================================================// Name        : 43-single-parity-check-codes-large.cpp// Author      : Fajar Purnama 152-D8713// Version     : 0.9// Copyright   : Free to distributed with credits to the author.// Description : Source Code for PWL Tent Map in C++//============================================================================#include <iostream>using namespace std;int main() {        int n;    cout << "Enter number of blocks: "; cin >> n;    int N = n*3; float p1, p2, c, c1, c2, d1, d2, a, a1, a2, a3;    c = 0.499999; cout << "Enter value of p2: "; cin >> p2;    p1 = (1-c)*p2/c;    a = 1/(1-(p1+p2));        int* bin = new int[N]; // need to do this because int main cannot hold large numbers, a stack overflow occurs so need to allocate to new heap        float* x = new float[N]; x[0] = 0.1;    // N = number of bits (1 block = 3 bits), c = critical point, x[0] = initial value, x[N] = chaotic sequence, and bin[N] = binary sequence    // From here is 3rd Assignment    if (p1+p2 < 1){        c1 = c-(c/a);    c2 = c+((1-c)/a );    d1 = c1*(1-c);    d2 = 1-((1-c2)*c);    a1 = -c/(c1-d1);    a2 = a;    a3 = (c-1)/(d2-c2);    for (int i = 0; i < N; ++i){        if (x[i] >= 0 && x[i] < c1) {            x[i+1] = (a1*(x[i]-d1))+c;        } else if(x[i] >= c1 && x[i] < c2) {            x[i+1] = a2*(x[i]-c1);        } else {            x[i+1] = (a3*(x[i]-c2))+1;        }        if (x[i] < c){            bin[i] = 0;        } else {            bin[i] = 1;        }    }    } else if (p1+p2 > 1){        c1 = c-((c-1)/a);    c2 = c-(c/a);    d1 = c1*(1-c);    d2 = 1-((1-c2)*(1-c));    a1 = -c/(c1-d1);    a2 = a;    a3 = c/(d2-c2);    for (int i = 0; i < N; ++i){        if (x[i] >= 0 && x[i] < c1) {            x[i+1] = (a1*(x[i]-d1))+c;        } else if(x[i] >= c1 && x[i] < c2) {            x[i+1] = (a2*(x[i]-c1))+1;        } else {            x[i+1] = a3*(x[i]-c2);        }        if (x[i] < c){            bin[i] = 0;        } else {            bin[i] = 1;        }    }    } else {        cout << "you can't do this" << endl;    }    // Insert bit parity on every 4th bit    int j = 0; int* bin_par = new int[N+(N/3)];    for (int i = 0; i < N+(N/3); i += 4){        bin_par[i] = bin[j];        bin_par[i+1] = bin[j+1];        bin_par[i+2] = bin[j+2];        bin_par[i+3] = bin_par[i]^bin_par[i+1]^bin_par[i+2];        j += 3;    }    // Generate error sequence    int* bin_err = new int[N+(N/3)]; float p, p1_err;    float* x_err = new float[N+(N/3)]; x_err[0]=x[0];    cout << "Enter error probability :"; cin >> p;    float c_err = 1-p;    p1_err = (1-c_err)*p2/c_err;    a = 1/(1-(p1_err+p2));    if (p1_err+p2 < 1){        c1 = c_err-(c_err/a);    c2 = c_err+((1-c_err)/a );    d1 = c1*(1-c_err);    d2 = 1-((1-c2)*c_err);    a1 = -c_err/(c1-d1);    a2 = a;    a3 = (c_err-1)/(d2-c2);    for (int i = 0; i < N+(N/3); ++i){        if (x_err[i] >= 0 && x_err[i] < c1) {            x_err[i+1] = (a1*(x_err[i]-d1))+c_err;        } else if(x_err[i] >= c1 && x_err[i] < c2) {            x_err[i+1] = a2*(x_err[i]-c1);        } else {            x_err[i+1] = (a3*(x_err[i]-c2))+1;        }        if (x_err[i] < c_err){            bin_err[i] = 0;        } else {            bin_err[i] = 1;        }    }    } else if (p1_err+p2 > 1){        c1 = c_err-((c_err-1)/a);    c2 = c_err-(c_err/a);    d1 = c1*(1-c_err);    d2 = 1-((1-c2)*(1-c_err));    a1 = -c_err/(c1-d1);    a2 = a;    a3 = c_err/(d2-c2);    for (int i = 0; i < N+(N/3); ++i){        if (x_err[i] >= 0 && x_err[i] < c1) {            x_err[i+1] = (a1*(x_err[i]-d1))+c_err;        } else if(x_err[i] >= c1 && x_err[i] < c2) {            x_err[i+1] = (a2*(x_err[i]-c1))+1;        } else {            x_err[i+1] = a3*(x_err[i]-c2);        }        if (x_err[i] < c_err){            bin_err[i] = 0;        } else {            bin_err[i] = 1;        }    }    } else {        cout << "you can't do this" << endl;    }    // Generate receiver side = parity sequence XOR error sequence    int* bin_r = new int[N+(N/3)];    for (int i = 0; i < N+(N/3); ++i){        bin_r[i] = bin_par[i] ^ bin_err[i];    }        /* Debugging Purpose    for (int i = 0; i < N+(N/3); ++i){        cout << bin_err[i];    }    cout << endl;    for (int i = 0; i < N+(N/3); ++i){        cout << bin_par[i];    }    cout << endl;    for (int i = 0; i < N+(N/3); ++i){        cout << bin_r[i];    }    cout << endl << endl; */    // total error, detected error, undetected error    float total_error = 0, detected_error = 0, undetected_error = 0;    float p_theory = (c_err*(((1-p1_err)*p1_err*(1-p2))+(p1_err*(1-p2)*p2)+(p1_err*p2*p1_err)))+((1-c_err)*((p2*(1-p1_err)*p1_err)+(p2*p1_err*p2)+((1-p2)*p2*(1-p1_err))+((1-p2)*(1-p2)*(1-p2))));    //float p_theory = (c*(((1-p1)*p1*(1-p2))+(p1*(1-p2)*p2)+(p1*p2*p1)))+((1-c)*((p2*(1-p1)*p1)+(p2*p1*p2)+((1-p2)*p2*(1-p1))+((1-p2)*(1-p2)*(1-p2))));    for (int i = 0; i < N+(N/3); i += 4){        if((bin_par[i] != bin_r[i])||(bin_par[i+1] != bin_r[i+1])||(bin_par[i+2] != bin_r[i+2])||(bin_par[i+3] != bin_r[i+3])){            ++total_error;        }        if((bin_r[i]^bin_r[i+1]^bin_r[i+2]^bin_r[i+3])==1){            ++detected_error;        }    }    undetected_error = total_error - detected_error;    float p_total_error = total_error/(N/3);    float p_detected_error = detected_error/(N/3);    float p_undetected_error = undetected_error/(N/3);    cout << "total error = " << total_error << endl;    cout << "detected error = " << detected_error << endl;    cout << endl;    cout << "undetected error = " << undetected_error << endl;    cout << "probability of total error = " << p_total_error << endl;    cout << "probability of detected error = " << p_detected_error << endl;    cout << "probability of undetected error = " << p_undetected_error << endl;    cout << "theory = " << p_theory << endl;    return 0;}`

# Mirrors

this blog contains all my articles licensed under creative commons attribution customized sharealike (cc-by-sa) where you can sell but mention the open one here

## More from Fajar Purnama

this blog contains all my articles licensed under creative commons attribution customized sharealike (cc-by-sa) where you can sell but mention the open one here

## Single number: coding problems under constant space

Get the Medium app