Tuesday, February 7, 2017

Dynamic Three Dimensional Array

Develop program to build dynamic three dimensional array of integers.
That gets initialized with random numbers from 1 - 9 and displays the array elements. It must allow the user to continue this process until "N" is entered.
#include <iostream>
#include <time.h>
#include <stdlib.h>





using namespace std;
int*** myArray;
//function to create the array
void createArray(int firstD, int secondD,int thirdD)
{
    myArray = new int**[firstD];

    for(int i = 0; i < firstD; i++)
    {
        myArray[i] = new int*[secondD];
    }

    for(int i = 0; i < firstD; i++)
    {
        for(int j = 0; j < secondD; j++)
        {
            myArray[i][j] = new int[thirdD];
        }
    }

};
//function to fill the values inside the array
void sortValues(int i, int j, int z)
{
    srand((unsigned)time(NULL));
    for(int a = 0; a < i; a++)
        for(int b = 0; b < j; b++)
            for(int c = 0; c < z; c++)
                myArray[a][b][c] = rand()%10;
};
//function to display the array
void displayArray(int displayX, int displayY, int displayZ)
{
    for(int a = 0; a < displayX; a++)
    {
        cout << '\t' << '\t' << "Layer " << a + 1;
        cout << endl;
        for(int i = 0; i < displayY; i++)
            cout << '\t' << " Column " << i + 1;
        cout << endl;
        for(int k = 0; k < displayZ; k++)
        {
            cout << " Row " << k + 1 << '\t';
            for(int q = 0; q < displayY; q++)
                cout << '\t' << myArray[a][q][k] << '\t';
            cout << endl;
        }
        cout << endl;
    }
};
//function to delete the array
void dArray(int x, int y)
{
    for(int a = 0; a < x; a++)
    {
        for(int b = 0; b < y; b++)
            delete[] myArray[a][b];
        delete[] myArray[a];
    }
    delete [] myArray;
};





int main(int argc, char** argv)
{
    //declare argc condition
    if(argc < 7)
    {
        cout <<"ERROR !!!!"<<'\n';
        cout<< "Please make sure you are passing 6 command line arguments i.e example x 3 y 3 z 3\n.";
        exit(0);
    }



    string choice = "Y";
    while(choice == "Y")
    {
        int firstDimensionSize = atoi(argv[2]);
        int secondDimensionSize = atoi(argv[4]);
        int thirdDimensionSize = atoi(argv[6]);
        //crete our array
        createArray(firstDimensionSize,secondDimensionSize,thirdDimensionSize);
        //sort the values
        sortValues(firstDimensionSize, secondDimensionSize, thirdDimensionSize);

        //display the array
        displayArray(firstDimensionSize, secondDimensionSize, thirdDimensionSize);
        //destroy our array
        dArray(firstDimensionSize,secondDimensionSize);

loop:
        cout << "Do you want to run the program again, Enter Y to yes, and N to exit\n";
        cin >> choice;

        if (choice == "N"||choice=="n")
            choice = "N";
        else if (choice == "Y"||choice == "y")
        {
            choice = "Y";

            cout<<"Enter the value for the first dimension: ";
            cin>>(argv[2]);
            cout<<"Enter the value for the second dimension: ";
            cin>> (argv[4]);
            cout<<"Enter the value for the third dimension: ";
            cin>>(argv[6]);
        }

        else
        {
            cout <<" Check your input"<< endl;
            goto loop;
        }

    }
    return 0;
}






No comments:

Post a Comment