9.3 / 9.5 Lab and Plane

using dynamic two dimensional arrays 

Pointer Projects Guidelines

Dynamic Two Dimensional Array (with variable column count per row)

 

 

Dynamic Two Dimensional Array Functions:

int array_size(int* sizes);
void print_array(int* a);
bool index_is_valid(int* sizes, int row, int col);



//To call: int** p = allocate_twod<int>(sizes_array)  
template <class T> T** allocate_twod(int* sizes); template <class T> T** deallocate_twod(T** twod, int size); template <class T> T read_twod(T** twod, int row, int col); template <class T> void write_twod(T** twod, int row, int col, const T& item); template <class T> T& get_twod(T** twod, int row, int col); template<class T> void init_twod(T** twod, int* sizes, T init_item = T()); template<class T> bool search_twod(T** twod, int* sizes, const T& key, int& row, int& col);
//return a pointer to the item if found, nullptr if not found:
// (Ms. Barskhian)
template<class T>
T*
search_twod(T** twod, int* sizes, const T& key);
template
<class T> ostream& print_twod(T** twod, int* sizes, ostream& outs = cout);

Lab Functions:

based on the dynamic two dimensional array functions

int **init_lab(int* stations);
bool login(int** labs, int lab, int station, int id);
bool logout(int** labs, int* sizes, int id);

Plane Functions:

bool **init_plane();
bool reserve(bool **plane, int row, int seat);
bool cancel(bool** plane, int row, int seat);
void print_plane(bool **plane);

Sample Test Function (fragment)

void test_lab(){
    int lab_sizes[] = {4,3,2,-1};
    int** labs = init_lab(lab_sizes);
    print_twod(labs, lab_sizes);
    ...
    cout<<"log[i]n    log[o]ut      e[x]it"<<endl;
    cout<<":"; cin>>command;
    while(toupper(command) != 'X'){
        switch(toupper(command)){
        case 'I':
            cout<<"---- LOG IN: ------"<<endl;
            cout<<"labs: ";
            print_array(lab_sizes);
            cout<<endl;

            cout<<"id: "; cin>>id;
            cout<<"lab: "; cin>>lab;
            cout<<"station: "; cin>>station;
            if (index_is_valid(lab_sizes, lab, station)){
                if (login(labs, lab, station,id)){
                    cout<<"you are logged in in lab "<<lab<<
                          ", station: "<<station<<endl;
                }
                else{
                    cout<<"lab "<<lab<<", station "<<station<<" is occupied"<<endl;
                }
            } //valid index
            else{
                cout<<"["<<lab<<"]["<<station<<"] is invalid. "<<endl;
            }
            break;

            ...
        } //switch
        print_twod(labs, lab_sizes);

        cout<<"log[i]n    log[o]ut      e[x]it"<<endl;
        cout<<":"; cin>>command;
    }//while
}

Labs Sample Output:

===========================

after init
    0    0    0    0
    0    0    0
    0    0
log[i]n    log[o]ut      e[x]it
:i
---- LOG IN: ------
labs:     4    3    2

id: 388
lab: 0
station: 2
you are logged in in lab 0, station: 2
    0    0  388    0
    0    0    0
    0    0
log[i]n    log[o]ut      e[x]it
:i
---- LOG IN: ------
labs:     4    3    2

id: 465
lab: 1
station: 2
you are logged in in lab 1, station: 2
    0    0  388    0
    0    0  465
    0    0
log[i]n    log[o]ut      e[x]it
:i
---- LOG IN: ------
labs:     4    3    2

id: 666
lab: 2
station: 3
[2][3] is invalid. 
    0    0  388    0
    0    0  465
    0    0
log[i]n    log[o]ut      e[x]it
:i
---- LOG IN: ------
labs:     4    3    2

id: 233
lab: 2
station: 0
you are logged in in lab 2, station: 0
    0    0  388    0
    0    0  465
  233    0
log[i]n    log[o]ut      e[x]it
:o
---- LOG OUT: ------
id: 555
you are not logged in. id: 555
    0    0  388    0
    0    0  465
  233    0
log[i]n    log[o]ut      e[x]it
:o
---- LOG OUT: ------
id: 388
you have successfully logged out
    0    0    0    0
    0    0  465
  233    0
log[i]n    log[o]ut      e[x]it
:o
---- LOG OUT: ------
id: 233
you have successfully logged out
    0    0    0    0
    0    0  465
    0    0
log[i]n    log[o]ut      e[x]it
:x



===========================
Press <RETURN> to close this window...