C++ Programming Code Examples
C++ > Arrays and Matrices Code Examples
Programming Code for Three Dimensional (3D) Array
/* Programming Code for Three Dimensional (3D) Array
Three dimensional (3D) array contains three for loops in programming. So, to initialize and print three dimensional array, you have to use three for loops. Third for loop (the innermost loop) forms 1D array, Second for loop forms 2D array and the third for loop (the outermost loop) forms 3D array, as shown here in the following program.
A three dimensional (3D) array can be thought of as an array of arrays of arrays. Following is a simple C++ program to initialize three-dimensional (3D) array of dimensions 3*4*2, then it will access some elements present in the array and display the element on the screen :
C++ Program - Three Dimensional Array Program */
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int arr[3][4][2] = {
{
{2, 4},
{4, 8},
{3, 9},
{5, 2}
},
{
{8, 6},
{2, 4},
{5, 3},
{2, 1}
},
{
{4, 9},
{3, 2},
{5, 4},
{5, 1}
}
};
cout<<"arr[0][0][0] = "<<arr[0][0][0]<<"\n";
cout<<"arr[0][2][1] = "<<arr[0][2][1]<<"\n";
cout<<"arr[2][3][1] = "<<arr[2][3][1]<<"\n";
getch();
}
A program shall contain a global function named main, which is the designated start of the program in hosted environment. main() function is the entry point of any C++ program. It is the point at which execution of program is started. When a C++ program is executed, the execution control goes directly to the main() function. Every C++ program have a main() function.
#include is a way of including a standard or user-defined file in the program and is mostly written at the beginning of any C/C++ program. This directive is read by the preprocessor and orders it to insert the content of a user-defined or system header file into the following program. These files are mainly imported from an outside source into the current program. The process of importing such files that might be system-defined or user-defined is known as File Inclusion. This type of preprocessor directive tells the compiler to include a file in the source code program.
The getch() is a predefined non-standard function that is defined in conio.h header file. It is mostly used by the Dev C/C++, MS- DOS's compilers like Turbo C to hold the screen until the user passes a single value to exit from the console screen. It can also be used to read a single byte character or string from the keyboard and then print. It does not hold any parameters. It has no buffer area to store the input character in a program. The getch() function does not accept any parameter from the user. It returns the ASCII value of the key pressed by the user as an input.
An array is a collection of data items, all of the same type, accessed using a common name. A one-dimensional array is like a list; A two dimensional array is like a table; The C++ language places no limits on the number of dimensions in an array, though specific implementations may. Some texts refer to one-dimensional arrays as vectors, two-dimensional arrays as matrices, and use the general term arrays when the number of dimensions is unspecified or unimportant. (2D) array in C++ programming is also known as matrix. A matrix can be represented as a table of rows and columns. In C/C++, we can define multi dimensional arrays in simple words as array of arrays. Data in multi dimensional arrays are stored in tabular form (in row major order).
An array is defined as the collection of similar type of data items stored at contiguous memory locations. Arrays are the derived data type in C++ programming language which can store the primitive type of data such as int, char, double, float, etc. It also has the capability to store the collection of derived data types, such as pointers, structure, etc. The array is the simplest data structure where each data element can be randomly accessed by using its index number. C++ array is beneficial if you have to store similar elements. For example, if we want to store the marks of a student in 6 subjects, then we don't need to define different variables for the marks in the different subject. Instead of that, we can define an array which can store the marks in each subject at the contiguous memory locations.
In computer programming, loops are used to repeat a block of code. For example, when you are displaying number from 1 to 100 you may want set the value of a variable to 1 and display it 100 times, increasing its value by 1 on each loop iteration. When you know exactly how many times you want to loop through a block of code, use the for loop instead of a while loop. A for loop is a repetition control structure that allows you to efficiently write a loop that needs to execute a specific number of times.
The cout is a predefined object of ostream class. It is connected with the standard output device, which is usually a display screen. The cout is used in conjunction with stream insertion operator (<<) to display the output on a console. On most program environments, the standard output by default is the screen, and the C++ stream object defined to access it is cout. The "c" in cout refers to "character" and "out" means "output". Hence cout means "character output". The cout object is used along with the insertion operator << in order to display a stream of characters.
It is a predefined function in "conio.h" (console input output header file) used to clear the console screen. It is a predefined function, by using this function we can clear the data from console (Monitor). Using of clrscr() is always optional but it should be place after variable or function declaration only. It is often used at the beginning of the program (mostly after variable declaration but not necessarily) so that the console is clear for our output.
Read the comments in the following program to understand each part of the program. This is a "default constructor" of the class, you do note that it's name is same as class name and