Untitled

mail@pastecode.io avatar
unknown
c_cpp
a year ago
4.7 kB
3
Indexable
Never
#include <iostream>
#include <windows.h>
#include <iomanip>
#include <random>

using namespace std;

random_device rd;
mt19937 gen(rd());
uniform_int_distribution<> dist(1, 1000000);

long long int read_QPC(){
    LARGE_INTEGER count;
    QueryPerformanceCounter(&count);
    return((long long int)count.QuadPart);
}

void showArray(int *arr, int arrSize){
    cout<<"Aktualna tablica: ";

    int i=0;
    while(i<arrSize){
        cout<<arr[i]<<" ";
        i++;
    }

    cout<<endl<<endl;
}

void pushBackArr(int *arr, int *newArr, int arrSize, int number){
    int i=0;

    while(i<arrSize-1){
        newArr[i]=arr[i];
        i++;
    }

    newArr[arrSize-1] = number;
}

void pushFrontArr(int *arr, int *newArr, int arrSize, int number){
    int i=1;

    arr[0]=number;

    while(i<arrSize){
        arr[i]=newArr[i-1];
        i++;
    }
}

void pushAtIdArr(int *arr, int *newArr, int arrSize, int number, int pos){
    int i=0, j=0;

    while(i<arrSize){
        if(i==pos){
            newArr[pos]=number;
            i++;
            continue;
        }
        newArr[i]=arr[j];
        i++;
        j++;
    }
}

void popBackArr(int *arr1, int *arr2, int arrSize){
    int i=0;

    while(i<arrSize){
        arr1[i] = arr2[i];
        i++;
    }
}

void popFrontArr(int *arr1, int *arr2, int arrSize){
    int i=0;

    while(i<arrSize){
        arr2[i]=arr1[i+1];
        i++;
    }

}

void popAtIdArr(int *arr1, int *arr2, int arrSize, int pos){
    int i=0;

    while(i<arrSize){
        if(i>=pos) arr1[i] = arr2[i+1];
        else arr1[i] = arr2[i];
        i++;
    }
}

void dynamicArray(){    int arrSize = 100000, number;
    long long int frequency, start, elapsed;

    // wpisywanie z pliku liczb tu bedzie

    int *arr = new int[arrSize];
    int i=0;

    while(i<arrSize){
        arr[i] = dist(gen);
        i++;
    }

    //showArray(arr, arrSize);

    // Dodawanie liczby z tylu

    cout<<"Podal liczbe ktora chcesz dodac z tylu: ";
    cin>>number;

    QueryPerformanceFrequency((LARGE_INTEGER *)&frequency);
    start = read_QPC();

    arrSize+=1;
    int *newArr = new int[arrSize];
    pushBackArr(arr, newArr, arrSize, number);
    delete[] arr;

    elapsed = read_QPC() - start;
    cout<< "Czas [us] = " << fixed<<setprecision(0)<< (1000000.0 * elapsed) / frequency <<endl<<endl;

    //showArray(newArr, arrSize);

    // Dodawanie z przodu

    cout<<"Podaj liczbe ktora chcesz dodac z przodu: ";
    cin>>number;

    start = read_QPC();

    arrSize+=1;
    arr = new int[arrSize];
    pushFrontArr(arr, newArr, arrSize, number);
    delete [] newArr;

    elapsed = read_QPC() - start;
    cout<<"Czas [us] = "<<fixed<<setprecision(0)<< (1000000.0 * elapsed) / frequency <<endl<<endl;

    //showArray(arr, arrSize);

    //Dodawanie na wybranej pozyjcji
    int position;
    cout<<"Podaj pozycje na ktora chcesz wsatwic liczbe: ";
    cin>>position;
    cout<<"Podaj liczbe do wstawienia: ";
    cin>>number;

    start = read_QPC();

    arrSize+=1;
    newArr = new int[arrSize];
    pushAtIdArr(arr, newArr, arrSize, number, position);
    delete [] arr;

    elapsed = read_QPC() - start;
    cout << "Time [us] = " << fixed << setprecision(0) << (1000000.0 * elapsed) / frequency << endl << endl;

    //showArray(newArr, arrSize);

    // Usuwanie liczby z tylu
    cout<<"Usuwanie liczby z tylu"<<endl;

    start = read_QPC();

    arrSize-=1;
    arr = new int[arrSize];
    popBackArr(arr, newArr, arrSize);
    delete [] newArr;

    elapsed = read_QPC() - start;
    cout<<"Czas [us] = "<<fixed<<setprecision(0)<< (1000000.0 * elapsed) / frequency <<endl<<endl;

    //showArray(arr, arrSize);

    // usuwanie liczby z przodu

     cout<<"Usuwanie liczby z przodu"<<endl;

    start = read_QPC();

    arrSize-=1;
    newArr = new int[arrSize];
    popFrontArr(arr, newArr, arrSize);
    delete [] arr;

    elapsed = read_QPC() - start;
    cout<<"Czas [us] = "<<fixed<<setprecision(0)<< (1000000.0 * elapsed) / frequency <<endl<<endl;

    //showArray(newArr, arrSize);

    // usuwanie liczby na ideksie

    cout<<"Usuwanie liczby na ineksie"<<endl;
    cout<<"Podaj pozycje na ktorej chcesz usunac liczbe: ";
    cin>>position;

    start = read_QPC();

    arrSize-=1;
    arr = new int[arrSize];
    popAtIdArr(arr, newArr, arrSize, position);
    delete [] newArr;

    elapsed = read_QPC() - start;
    cout<<"Czas [us] = "<<fixed<<setprecision(0)<< (1000000.0 * elapsed) / frequency <<endl<<endl;

    //showArray(arr, arrSize);

    delete [] arr;
}