Answer to Question #239139 in C++ for Azan

Question #239139
Create a single class which
1)implements the functions of one and two dimension array
2)insert elements in the array until user presses - 1
3)delete the elements until user presses - 1 or when array is empty
4)display the data after each insertion and deletion of element
5)if array is full create a larger array and copy the elements to there
6)display elements one by one as they will be copied and deleted after copying

NOTE:
1)create dynamic 1d and 2d arrays
2) test the functions in main
3)user should have the ability to choose whether he wants to create 1d or 2d array
1
Expert's answer
2021-09-20T16:46:28-0400
#include <iostream>
using namespace std;
class _1DArray{
    int size, *arr, index;
    public:
    _1DArray(){
        this->size = 10;
        this->index = 0;
        arr = new int[size];
    }
    void insert(int n){
        if(index < size){
            arr[index++] = n;
        }
        else{
            size *= 2;
            arr = (int *) realloc(arr, size * sizeof(int));
            arr[index++] = n;
        }
    }
    void remove(){
        if(size > 0){
            cout<<"Deleting "<<arr[index - 1]<<endl;
            arr = (int*) realloc(arr, index * sizeof(int));
            size = index;
            index--;
        }
    }
    void show(){
        for(int i = 0; i < index; i++){
            cout<<arr[i]<<" ";
        }
        cout<<endl;
    }
};
class _2DArray{
    int size, (*arr)[10], i, j;
    public:
    _2DArray(){
        this->size = 10;
        this->i = 0;
        this->j = 0;
        arr = new int[size][10];
    }
    void insert(int n){
        if(j == 10){
            j = 0;
            i++;
        }
        if(j < 10 && i < size){
            arr[i][j++] = n;
            return;
        }
        if(i >= size){
            size *= 2;
            arr = (int (*)[10]) realloc(arr, size * sizeof(int[10]));
            arr[i][j] = n;
        }
    }
    void remove(){
        if(size > 0){
            j--;
            if(j < 0){
                j = 9;
                i--;
            }
            if(i < 0) return;
            if(i >= 0){
                size = i + 1;
                cout<<"Deleting "<<arr[i][j]<<endl;
                arr[i][j] = -1;
                arr = (int (*)[10]) realloc(arr, size * sizeof(int[10]));
            }
        }
    }
    void show(){
        for(int k = 0; k < i + 1; k++){
        for(int l = 0; l < 10; l++){
            cout<<arr[k][l]<<" ";
        }
        cout<<endl;
        }
    }
};
int main(){
    cout<<"Choose 1D or 2D array: (1 or 2)\n";


    int c; cin>>c;
    if(c == 1){
        _1DArray arr;
        cout<<"Inserting items\n";
        while(c != -1){
            cin>>c;
            if(c != -1){
                arr.insert(c);
                cout<<"Contents\n";
                arr.show();
            }
        }
        cout<<"Deleting items\n";
        cout<<"Input -1 to stop\n";
        do{
            cin>>c;
            if(c != -1){
                arr.remove();
                cout<<"Contents\n";
                arr.show();
            }
        }while(c != -1);
    }
    else if(c == 2){
        _2DArray arr;
        cout<<"Inserting items\n";
        while(c != -1){
            cin>>c;
            if(c != -1){
                arr.insert(c);
                cout<<"Contents\n";
                arr.show();
            }
        }
        cout<<"Deleting items\n";
        cout<<"Input -1 to stop\n";
        do{
            cin>>c;
            if(c != -1){
                arr.remove();
                cout<<"Contents\n";
                arr.show();
            }
        }while(c != -1);
    }
    return 0;
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS