Thursday, July 4, 2024
HomeData ModellingData Structure & AlgorithmHow to pass and return a 3-Dimensional Array in C++?

How to pass and return a 3-Dimensional Array in C++?

In C++ a 3-dimensional array can be implemented in two ways:

  • Using array (static)
  • Using vector (dynamic)

Passing a static 3D array in a function: Using pointers while passing the array. Converting it to the equivalent pointer type.

char ch[2][2][2];
void display(char (*ch)[2][2]) {
    . . .
}

Program to pass a static 3D array as a parameter:

C++




// C++ code to demonstrate the above method
 
#include <bits/stdc++.h>
using namespace std;
 
// Function to show how to pass
// a 3D character array to a function
void display(char (*ch)[2][2])
{
    for (int i = 0; i < 2; i++) {
        for (int j = 0; j < 2; j++) {
            for (int k = 0; k < 2; k++) {
                cout << "ch[" << i << "][" << j << "]["
                     << k << "] = " << ch[i][j][k] << endl;
            }
        }
    }
}
 
// Driver code
int main()
{
    char ch[2][2][2] = { { { 'a', 'b' }, { 'c', 'd' } },
                         { { 'e', 'f' }, { 'g', 'h' } } };
 
    // Function call with 3D array as parameter
    display(ch);
    return 0;
}


Output

ch[0][0][0] = a
ch[0][0][1] = b
ch[0][1][0] = c
ch[0][1][1] = d
ch[1][0][0] = e
ch[1][0][1] = f
ch[1][1][0] = g
ch[1][1][1] = h

Time Complexity:   O(n3)

Auxiliary Space: O(1)

Passing 3D vector (dynamic array): When a vector is passed to a function, it can either be passed by value, where a copy of the vector is stored, or by reference, where the address of the vector is passed.

  • Pass by value: 

void function(vector <vector <vector < char >>> ch) {
    . . .
}

  • Pass by reference (Better):

void function(vector< vector < vector < char>>> &ch) {
    . . .
}

Program to pass a dynamic 3D array as a parameter:

C++




// C++ code to demonstrate the initialization
// and passing as a parameter (passed as reference)
 
#include <bits/stdc++.h>
using namespace std;
 
// Parameter passed as reference
void display(vector<vector<vector<char> > >& ch)
{
    for (int i = 0; i < 2; i++) {
        for (int j = 0; j < 2; j++) {
            for (int k = 0; k < 2; k++) {
                cout << "ch[" << i << "][" << j << "]["
                     << k << "] = " << ch[i][j][k] << endl;
            }
        }
    }
}
 
int main()
{
    vector<vector<vector<char> > > ch
        = { { { 'a', 'b' }, { 'c', 'd' } },
            { { 'e', 'f' }, { 'g', 'h' } } };
 
    // Function to call function by passing as parameter
    display(ch);
    return 0;
}


Output

ch[0][0][0] = a
ch[0][0][1] = b
ch[0][1][0] = c
ch[0][1][1] = d
ch[1][0][0] = e
ch[1][0][1] = f
ch[1][1][0] = g
ch[1][1][1] = h

Time Complexity:  O(n3)

Auxiliary Space: O(1)

Returning a 3D array: A static array cannot be returned from a function in C++. So we have to pass a 3D vector from a function to get the functionality of returning a 3D array.

vector <vector< vector <char>>> fun() {
    vector <vector< vector <char>>> ch;
    . . .
    return ch;
}

Feeling lost in the world of random DSA topics, wasting time without progress? It’s time for a change! Join our DSA course, where we’ll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!

Shaida Kate Naidoo
am passionate about learning the latest technologies available to developers in either a Front End or Back End capacity. I enjoy creating applications that are well designed and responsive, in addition to being user friendly. I thrive in fast paced environments. With a diverse educational and work experience background, I excel at collaborating with teams both local and international. A versatile developer with interests in Software Development and Software Engineering. I consider myself to be adaptable and a self motivated learner. I am interested in new programming technologies, and continuous self improvement.
RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments