EDUCBA

EDUCBA

MENUMENU
  • Free Tutorials
  • Free Courses
  • Certification Courses
  • 600+ Courses All in One Bundle
  • Login

3D Arrays in C++

Home » Software Development » Software Development Tutorials » C ++ Programming Tutorial » 3D Arrays in C++

3D Arrays in C++

Introduction to 3D Arrays in C++

C++ array is used to store the data in the form of a table of rows and columns. Here we can create single or multidimensional arrays to hold values in different scenarios. In C++, a 3d array is a multidimensional array used to store 3-dimensional information. In simple words, a three-dimensional array is an array of arrays. In three dimensional array, we have three rows and three columns. In this article, we will see what is three-dimensional array, the use of a three-dimensional array, how to access them and how to use effectively three-dimensional array in our code.

Working of 3D Arrays in C++

1. Usage of 3d array can be understood by taking the example of searching the word inside the book. We need three pieces of information to search for a word in a book.

Start Your Free Software Development Course

Web development, programming languages, Software testing & others

  • Page number.
  • Line number.
  • Word index or column in which word belongs.

2. In multidimensional arrays data in the form of a table, that is in row-major order. The general syntax of a 3-dimensional array is as below.

Syntax:

data_type array_name[size1][size2][size3];

3. Remember that the size is always a positive integer Below is the example of a three-dimensional array.

  • Example: Here 3DArray is a three-dimensional array, having a maximum of 24 elements.

int 3DArray[2][3][4];

4. The maximum number of elements contained in an array is obtained by multiplying the size of all the dimensions.

  • Example: In 3DArray[2][3][4], The maximum element is obtained by multiplying 2, 3, 4, i.e. 24.

5. Similarly 3DArray[10][10][10], can hold 1000 elements. We can visualize this as each of the 10 elements can hold 10 elements, which makes a total of 100 elements. Every 100 elements can hold another 10 elements, which makes the final count as 1000.

6. We can create a 3-dimensional array by creating a 2D array first and then extending it to the required dimension.

Initialization of a 3D Array

We Can Initialize a 3-Dimensional Array in Many Ways. Below Are the Examples for Reference.

Popular Course in this category
C++ Training (4 Courses, 5 Projects, 4 Quizzes)4 Online Courses | 5 Hands-on Projects | 37+ Hours | Verifiable Certificate of Completion | Lifetime Access | 4 Quizzes with Solutions
4.5 (4,899 ratings)
Course Price

View Course

Related Courses
Java Training (40 Courses, 29 Projects, 4 Quizzes)C Programming Training (3 Courses, 5 Project)

int 3DArray[2][2][4] = {1, 3, 6, 5, 8, 9, -2, 4, 5, 10, 34, 56, 23, -56, 10, 37};

The values in the flower braces from left to right are stored inside the array as a table from left to right. The values will be filled in the array in the following order. First 4 elements from the left in the first row, next 4 elements in the second row and so on.

The above initialization won’t give us a clear picture of the array. For better visualization, we can initialize the same array as below.

int 3DArray[2][2][4] =
{
{     {1, 3, 6, 5}, {8, 9, -2, 4}    },
{     {5, 10, 34, 56}, {23, -56, 10, 37}   }
};

  • Accessing elements in the 3D array is similar to any other array, by using the index of the element. We have to use three loops to access all the elements inside the array x[2][1][0].
  • For higher dimension arrays like 4, 5, 6, etc., the concept is quite similar, but the complexity of handling the things increases. For example, the number of loops used, a number of element searches, accessing the particular element, etc.
  • Elements of 3 dimensional or higher dimensional arrays can be moved around in different ways. This operation is similar to vectors and matrices. Different techniques like reshape, permute, and squeeze are used for the purpose of rearranging elements inside the array. These are the complex techniques which we need not worry for now.

Example with Steps

Now we will use these 3D arrays to understand how the arrays will work.

We will write a C++ code that will take input from the user and display the elements present in the 3-dimensional array.

1. First, we will write the main program for the execution.

#include <iostream>
using namespace std;
int main( )
{
}

2. Inside the main function, we will declare a 3-dimensional array which can store up to 16 elements.

int Array[2][2][4];

3. Now we will ask the user to enter 16 values he wants to store in the array.

cout << "Please enter 16 values of your choice: \n";

4. In order to store the values into the array we need three loops, i.e. each dimension uses one loop to traverse. We will take three indexes, i, j and k for the three dimensions. For a better understanding of the code, we will use for loop. First for loop represents the first dimension, second for loop for the second dimension and third for loop for the third dimension. Inside the third for loop, we are taking the input from the user.

for(int i = 0; i < 2; i++)
{
for (int j = 0; j < 2; j++)
{
for(int k = 0; k < 4; k++ )
{
cin >> Array[i][j][k];
}
}
}

5. Now since the values are stored in the array, it’s time for us to show the stored values to the user.

6. For this, again we are using the three for loops for traversal and this time cout for printing the values.

cout<<"\n Below are the values you have stored in the array"<< endl;
for(int i = 0; i < 2; i++)
{
for (int j = 0; j < 2; j++)
{
for(int k = 0; k < 4; k++)
{
cout << "[" << i << "][" << j << "][" << k << "] =" <<
Array[i][j][k] << endl;
}
}
}

Output:

3d Arrays in C++

Conclusion – 3D Arrays in C++

In this article, we have learned what is an array, what is single and multidimensional array, the significance of multidimensional array, how to initialize the array and using the multidimensional array in the program based on our needs.

Recommended Articles

This is a guide to 3D Arrays in C++. Here we discuss the introduction and working of 3D arrays in C++ along with example and steps. You may also look at the following articles to learn more –

  1. C++ Array Functions
  2. Overriding in C++
  3. Constructor and Destructor in C++
  4. Overriding in C++

C++ Training (4 Courses, 3 Projects, 4 Quizzes)

4 Online Courses

5 Hands-on Projects

37+ Hours

Verifiable Certificate of Completion

Lifetime Access

4 Quizzes with Solutions

Learn More

0 Shares
Share
Tweet
Share
Primary Sidebar
C plus plus Programming Tutorial
  • Array
    • Arrays in C++
    • 2D Arrays in C++
    • 3D Arrays in C++
    • Multi-Dimensional Arrays in C++
    • C++ Array Functions
    • String Array in C++
    • C++ Length of Array
    • C++ arraylist
  • Basic
    • Introduction To C++
    • What is C++
    • Features of C++
    • Applications of C++
    • Best C++ Compiler
    • C++ Data Types
    • C++ Double
    • C++ unsigned int
    • User Defined Data Types in C++
    • Variables in C++
    • C++ Keywords
    • Pointers in C++
    • C++ Void Pointer
    • Function Pointer in C++
    • Iterator in C++
    • C++ Commands
    • Object in C++
    • C++ Literals
    • C++ Reference
    • C++ Undefined Reference
    • String in C++
    • C++ Programming Language (Basics)
    • C++ Identifiers
    • C++ Header Files
    • Type Casting in C++
    • C++ Formatter
  • Operators
    • C++ Operators
    • Arithmetic Operators in C++
    • Assignment Operators in C++
    • Bitwise Operators in C++
    • Relational Operators in C++
    • Boolean Operators in C++
    • Unary Operators in C++
    • C++ Operator[]
    • Operator Precedence in C++
    • C++ operator=()
  • Control Statements
    • Control Statement in C++
    • if else Statement in C++
    • Else If in C++
    • Nested if in C++
    • Continue Statement in C++
    • Break Statement in C++
    • Switch Statement in C++
    • goto Statement in C++
    • C++ Struct
    • Loops in C++
    • Do While Loop in C++
    • Nested Loop in C++
  • Functions
    • C++ String Functions
    • Math Functions in C++
    • Friend Function in C++
    • Recursive Function in C++
    • Virtual Functions in C++
    • strcat() in C++
    • swap() in C++
    • strcmp() in C++
    • ceil function in C++
    • C++ begin()
    • size() in C++
    • C++ test()
    • C++ any()
    • C++ Bitset
    • C++ find()
    • C++?Aggregation
    • C++?String append
    • C++ String Copy
    • C++ end()
    • C++ endl
    • C++ push_back
    • C++ shuffle()
    • malloc() in C++
    • C++ reserve()
    • C++ unique()
    • C++ sort()
    • C++ find_if()
    • Reflection in C++
    • C++ replace()
    • C++ search()
    • C++ Memset
    • C++ size_t
    • C++ Substring
    • C++ Max
    • C++ absolute value
    • C++ memcpy
    • C++ wchar_t
    • C++ free()
    • C++ sizeof()
    • C++ Move Semantics
  • Constuctor and Destructor
    • Constructor and Destructor in C++
    • Constructor in C++
    • Destructor in C++
    • Copy Constructor in C++
    • Parameterized Constructor in C++
  • Overloading and overriding
    • Overloading and Overriding in C++
    • Overloading in C++
    • Overriding in C++
    • Function Overloading in C++
    • Function Overriding in C++
    • Method Overloading in C++
  • Inhertiance
    • Types of Inheritance in C++
    • Single Inheritance in C++
    • Multiple Inheritance in C++
    • Hierarchical Inheritance in C++
    • Multilevel Inheritance in C++
    • Hybrid Inheritance in C++
  • Sorting
    • Sorting in C++ 
    • Heap Sort in C++
    • C++ Vector Sort
    • Insertion Sort in C++
    • Selection Sort in C++
  • Advanced
    • C++ namespace
    • Encapsulation in C++
    • Access Modifiers in C++
    • Abstract Class in C++
    • C++ Class and Object
    • What is Template Class in C++?
    • C++ Algorithm
    • Data Structures and Algorithms C++
    • C++ Garbage Collection
    • Virtual Keyword in C++
    • Access Specifiers in C++
    • Storage Class in C++
    • Call by Value in C++
    • Multimap in C++
    • C++ Multiset
    • C++ Lambda Expressions
    • Stack in C++
    • C++ Static
    • C++ static_cast
    • Deque in C++
    • C++ Vector Functions
    • C++ 2D Vector
    • C++ List
    • C++ Mutable
    • Enum in C++
    • Abstraction in C++
    • Signal in C++
    • C++ Queue
    • Priority Queue in C++
    • Regular Expressions in C++
    • C++ Hash Table
    • File Handling in C++
    • C++ Stream
    • ifstream in C++
    • C++ ofstream
    • C++ fstream
    • C++ Read File
    • C++ iomanip
    • Macros in C++
    • Templates in C++
    • C++ setprecision
    • C++ Int to String
    • C++ thread( )
    • C++ Thread Pool
    • C++ thread_local
  • Programs
    • Patterns in C++
    • Star Patterns In c++
    • Swapping in C++
    • Reverse Number in C++
    • Palindrome Program in C++
    • Palindrome in C++
    • Factorial Program in C++
    • Fibonacci Series in C++
    • Square Root in C++
    • Random Number Generator in C++
    • Prime Number in C++
    • Leap Year Program in C++
    • Anagram in C++
    • Armstrong Number in C++
    • Reverse String in C++
    • Socket Programming in C++
    • Matrix Multiplication in C++
    • C++ using vs typedef
    • C++ vector vs list
    • C++ vector vs array
  • Interview question
    • C++ Interview Questions
    • Multithreading Interview Questions C++

Related Courses

C++ Training Course

Java Training Course

C Programming Course

Footer
About Us
  • Blog
  • Who is EDUCBA?
  • Sign Up
  • Corporate Training
  • Certificate from Top Institutions
  • Contact Us
  • Verifiable Certificate
  • Reviews
  • Terms and Conditions
  • Privacy Policy
  •  
Apps
  • iPhone & iPad
  • Android
Resources
  • Free Courses
  • Java Tutorials
  • Python Tutorials
  • All Tutorials
Certification Courses
  • All Courses
  • Software Development Course - All in One Bundle
  • Become a Python Developer
  • Java Course
  • Become a Selenium Automation Tester
  • Become an IoT Developer
  • ASP.NET Course
  • VB.NET Course
  • PHP Course

© 2020 - EDUCBA. ALL RIGHTS RESERVED. THE CERTIFICATION NAMES ARE THE TRADEMARKS OF THEIR RESPECTIVE OWNERS.

EDUCBA
Free Software Development Course

Web development, programming languages, Software testing & others

*Please provide your correct email id. Login details for this Free course will be emailed to you
Book Your One Instructor : One Learner Free Class

Let’s Get Started

This website or its third-party tools use cookies, which are necessary to its functioning and required to achieve the purposes illustrated in the cookie policy. By closing this banner, scrolling this page, clicking a link or continuing to browse otherwise, you agree to our Privacy Policy

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA Login

Forgot Password?

EDUCBA
Free Software Development Course

Web development, programming languages, Software testing & others

*Please provide your correct email id. Login details for this Free course will be emailed to you

Special Offer - C++ Training (4 Courses, 3 Projects, 4 Quizzes) Learn More