Prerequisite: Arrays
Arrays are used to create lists of elements with the same data type and store them in adjacent regions of memory. Arrays are just the grouping of elements of the same data type. 1-Dimensional arrays, 2-Dimensional arrays, and 3-Dimensional arrays are just a few of the several types of arrays that can be declared.
What is Wiring?
Wiring is the concept of visualizing the elements of the array connected, It helps us to visualize the multidimensional into a flattened form.
Wiring in the 2-D Array
An array that can store multiple arrays inside it is known as a 2-D array or 2-Dimensional array. A 2-dimensional array of elements can be accessed the same as a normal array with the help of Indexes. To know more about 2D arrays refer to multidimensional arrays for Java and for C/C++.
Visualizing a 2D array as a collection of multiple 1D arrays can be quite easy with the help of wiring. Below is the implementation of the concept with the 2-D array.
Example:
C
// C Program to implement // Wiring 2D array #include <stdio.h> int main() { // Declaration of 2-Dimenssional array in the form // of wiring int arr[3][3] = { { 1, 2, 3 }, { 4, 5, 6 }, { 7, 8, 9 } }; for ( int i = 0; i < 3; i++) { for ( int j = 0; j < 3; j++) { printf ( "%d " , arr[i][j]); } printf ( "\n" ); } printf ( "Wiring:\n" ); // traversing all the elements of wiring array for ( int i = 0; i < 3; i++) { for ( int j = 0; j < 3; j++) { // printing all elements printf ( "%d " , arr[i][j]); } if (i == 2) break ; printf ( " , " ); } return 0; } |
C++
// C++ Program to implement // Wiring in 2D array #include <iostream> using namespace std; int main() { // Declaration of 2-Dimenssional array in the form // of wiring int arr[3][3] = { { 1, 2, 3 }, { 4, 5, 6 }, { 7, 8, 9 } }; for ( int i = 0; i < 3; i++) { for ( int j = 0; j < 3; j++) { cout << arr[i][j] << " " ; } cout << endl; } cout << "Wiring:" << endl; for ( int i = 0; i < 3; i++) { for ( int j = 0; j < 3; j++) { cout << arr[i][j] << " " ; } if (i == 2) break ; cout << " , " ; } return 0; } |
Java
// Java Program to implement // Concept of wiring import java.io.*; class GFG { // Java Program t0 implement 2-D // array using concept of wiring public static void main(String[] args) { // Declaration of 2-Dimenssional array in the form // of wiring int Arr[][] = { { 1 , 2 , 3 }, { 4 , 5 , 6 }, { 7 , 8 , 9 } }; for ( int i = 0 ; i < Arr.length; i++) { for ( int j = 0 ; j < Arr[ 0 ].length; j++) { // printing all elements // in the form of matrix System.out.print(Arr[i][j] + " " ); } System.out.println( " " ); } System.out.println( "Wiring:" ); // traversing all the elements of wiring array for ( int i = 0 ; i < Arr.length; i++) { for ( int j = 0 ; j < Arr[ 0 ].length; j++) { // printing all elements // in the form of matrix System.out.print(Arr[i][j] + " " ); } if (i == Arr.length - 1 ) break ; System.out.print( " , " ); } } } |
Python3
#Python code for the above approach # Declaration of 2-Dimensional array in the form of wiring arr = [[ 1 , 2 , 3 ], [ 4 , 5 , 6 ], [ 7 , 8 , 9 ]] for row in arr: for val in row: # printing all elements #in the form of matrix print (val, end = " " ) print () print ( "Wiring:" ) #traversing all the elements of wiring array for i, row in enumerate (arr): for val in row: #printing all elements in the form of matrix print (val, end = " " ) if i = = 2 : break print ( "," ,end = " " ) #This code is contributed by Potta Lokesh |
C#
// C# Program to implement // Wiring in 2D array using System; using System.Collections.Generic; class GFG { static public void Main() { // Declaration of 2-Dimenssional array in the form // of wiring int [,] arr = { { 1, 2, 3 }, { 4, 5, 6 }, { 7, 8, 9 } }; for ( int i = 0; i < 3; i++) { for ( int j = 0; j < 3; j++) { Console.Write(arr[i,j] + " " ); } Console.WriteLine(); } Console.Write( "Wiring:" + "\n" ); for ( int i = 0; i < 3; i++) { for ( int j = 0; j < 3; j++) { Console.Write(arr[i,j] + " " ); } if (i == 2) break ; Console.Write( " , " ); } } } |
Javascript
// JS Program to implement // Wiring in 2D array // Declaration of 2-Dimenssional array in the form // of wiring let arr = [[ 1, 2, 3 ], [ 4, 5, 6 ], [ 7, 8, 9 ]]; for (let i = 0; i < 3; i++) { for (let j = 0; j < 3; j++) { console.log(arr[i][j] + " " ); } console.log( "<br>" ); } console.log( "Wiring:" + "<br>" ); for (let i = 0; i < 3; i++) { for (let j = 0; j < 3; j++) { console.log(arr[i][j] + " " ); } if (i == 2) break ; console.log( " , " ); } |
1 2 3 4 5 6 7 8 9 Wiring: 1 2 3 , 4 5 6 , 7 8 9
Wiring Related to a 3-D Array
A 3-Dimensional array is a group of 2-dimensional arrays that are successively stored at different locations within the array.
The wiring’s initial purpose was to connect objects, and in a three-dimensional array, it serves the same purpose by connecting the array to create a new three-dimensional structure. The array’s blocks and elements are all interconnected. In this post, we’ll talk about 3-dimensional arrays that are wired together. These three-dimensional arrays in wiring function similarly to an array’s matrix declarations.
Example:
C
// C Program to illustrate concept // of Wiring #include <stdio.h> int main() { // Declaration of 3-Dimenssional array in the form // of wiring int Arr[4][4][4] = { { { 9, 8, 7, 5 },{ 4, 5, 6, 8 },{ 7, 8, 9, 1 },{ 5, 4, 7, 9 } }, { { 20, 19, 30, 31 },{ 40, 50, 60, 20 }, { 70, 80, 90, 50 },{ 20, 30, 40, 50 } }, { { 10, 11, 12, 13 },{ 14, 15, 16, 28 },{ 50, 17, 18, 19 },{ 90, 80, 70, 50 } }, { { 20, 21, 22, 23 },{ 54, 24, 25, 26 },{ 38, 27, 28, 29 },{ 20, 54, 78, 88 } }, }; int m= sizeof (Arr)/ sizeof (Arr[0]); int n= sizeof (Arr[0])/ sizeof (Arr[0][0]); int o= sizeof (Arr[0][0])/ sizeof (Arr[0][0][0]); for ( int i = 0; i < m; i++) { for ( int j = 0; j < n; j++) { for ( int k = 0; k < o; k++) { // printing all elements // in the form of matrix printf ( "%d " ,Arr[i][j][k]); } printf ( "\n" ); } printf ( "\n" ); } return 0; } |
C++
// C++ Program to illustrate concept // of Wiring #include <iostream> using namespace std; int main() { // Declaration of 3-Dimenssional array in the form // of wiring int Arr[4][4][4] = { { { 9, 8, 7, 5 },{ 4, 5, 6, 8 },{ 7, 8, 9, 1 },{ 5, 4, 7, 9 } }, { { 20, 19, 30, 31 },{ 40, 50, 60, 20 }, { 70, 80, 90, 50 },{ 20, 30, 40, 50 } }, { { 10, 11, 12, 13 },{ 14, 15, 16, 28 },{ 50, 17, 18, 19 },{ 90, 80, 70, 50 } }, { { 20, 21, 22, 23 },{ 54, 24, 25, 26 },{ 38, 27, 28, 29 },{ 20, 54, 78, 88 } }, }; int m= sizeof (Arr)/ sizeof (Arr[0]); int n= sizeof (Arr[0])/ sizeof (Arr[0][0]); int o= sizeof (Arr[0][0])/ sizeof (Arr[0][0][0]); int i=0,j=0,k=0; // printing all elements // in the form of matrix for ( int i = 0; i < m; i++) { for ( int j = 0; j < n; j++) { for ( int k = 0; k < o; k++) { // printing all elements // in the form of matrix cout<<Arr[i][j][k]<< " " ; } cout<<endl; } cout<<endl; } return 0; } |
Java
// Java Program to implement // Concept of wiring import java.io.*; class GFG { // Java Program t0 implement 3-D // array using concept of wiring public static void main(String[] args) { // Declaration of 3-Dimenssional array in the form // of wiring int wiringArr[][][] ={ { { 9 , 8 , 7 , 5 },{ 4 , 5 , 6 , 8 },{ 7 , 8 , 9 , 1 },{ 5 , 4 , 7 , 9 } }, { { 20 , 19 , 30 , 31 },{ 40 , 50 , 60 , 20 }, { 70 , 80 , 90 , 50 },{ 20 , 30 , 40 , 50 } }, { { 10 , 11 , 12 , 13 },{ 14 , 15 , 16 , 28 },{ 50 , 17 , 18 , 19 },{ 90 , 80 , 70 , 50 } }, { { 20 , 21 , 22 , 23 },{ 54 , 24 , 25 , 26 },{ 38 , 27 , 28 , 29 },{ 20 , 54 , 78 , 88 } }, }; // traversing all the elements of wiring array for ( int i = 0 ; i < wiringArr.length; i++) { for ( int j = 0 ; j < wiringArr[ 0 ].length; j++) { for ( int k = 0 ; k < wiringArr[ 0 ][ 0 ].length; k++) { // printing all elements // in the form of matrix System.out.print(wiringArr[i][j][k] + " " ); } System.out.println(); } System.out.println(); } } } |
Python3
# Python Program to illustrate concept # of Wiring Arr = [[[ 9 , 8 , 7 , 5 ], [ 4 , 5 , 6 , 8 ], [ 7 , 8 , 9 , 1 ], [ 5 , 4 , 7 , 9 ]], [[ 20 , 19 , 30 , 31 ], [ 40 , 50 , 60 , 20 ], [ 70 , 80 , 90 , 50 ], [ 20 , 30 , 40 , 50 ]], [[ 10 , 11 , 12 , 13 ], [ 14 , 15 , 16 , 28 ], [ 50 , 17 , 18 , 19 ], [ 90 , 80 , 70 , 50 ]], [[ 20 , 21 , 22 , 23 ], [ 54 , 24 , 25 , 26 ], [ 38 , 27 , 28 , 29 ], [ 20 , 54 , 78 , 88 ]]] m = len (Arr) n = len (Arr[ 0 ]) o = len (Arr[ 0 ][ 0 ]) # printing all elements # in the form of matrix for i in range (m): for j in range (n): for k in range (o): # printing all elements # in the form of matrix print (Arr[i][j][k], end = ' ' ) print () print () # This code is contributed by akashish__ |
C#
// C# Program to illustrate concept // of Wiring using System; using System.Collections.Generic; class Gfg { public static void Main( string [] args) { // Declaration of 3-Dimenssional array in the form // of wiring int [,,] Arr = { { { 9, 8, 7, 5 },{ 4, 5, 6, 8 },{ 7, 8, 9, 1 },{ 5, 4, 7, 9 } }, { { 20, 19, 30, 31 },{ 40, 50, 60, 20 }, { 70, 80, 90, 50 },{ 20, 30, 40, 50 } }, { { 10, 11, 12, 13 },{ 14, 15, 16, 28 },{ 50, 17, 18, 19 },{ 90, 80, 70, 50 } }, { { 20, 21, 22, 23 },{ 54, 24, 25, 26 },{ 38, 27, 28, 29 },{ 20, 54, 78, 88 } }, }; int m=Arr.GetLength(0); int n=Arr.GetLength(1); int o=Arr.GetLength(2); int i=0,j=0,k=0; // printing all elements // in the form of matrix for ( i = 0; i < m; i++) { for ( j = 0; j < n; j++) { for ( k = 0; k < o; k++) { // printing all elements // in the form of matrix Console.Write(Arr[i,j,k]+ " " ); } Console.Write( "\n" ); } Console.Write( "\n" ); } } } // This code is contributed by imruhrbf8. |
Javascript
// Javascript Program to illustrate concept // of Wiring let Arr = [ [ [9, 8, 7, 5], [4, 5, 6, 8], [7, 8, 9, 1], [5, 4, 7, 9] ], [ [20, 19, 30, 31], [40, 50, 60, 20], [70, 80, 90, 50], [20, 30, 40, 50] ], [ [10, 11, 12, 13], [14, 15, 16, 28], [50, 17, 18, 19], [90, 80, 70, 50] ], [ [20, 21, 22, 23], [54, 24, 25, 26], [38, 27, 28, 29], [20, 54, 78, 88] ] ]; let m = Arr.length; let n = Arr[0].length; let o = Arr[0][0].length; // printing all elements // in the form of matrix for (let i = 0; i < m; i++) { for (let j = 0; j < n; j++) { for (let k = 0; k < o; k++) { // printing all elements // in the form of matrix process.stdout.write(Arr[i][j][k] + " " ); } console.log(); } console.log(); } // This code is contributed by akashish__ |
9 8 7 5 4 5 6 8 7 8 9 1 5 4 7 9 20 19 30 31 40 50 60 20 70 80 90 50 20 30 40 50 10 11 12 13 14 15 16 28 50 17 18 19 90 80 70 50 20 21 22 23 54 24 25 26 38 27 28 29 20 54 78 88
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!