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++.
 Wiring in a 2D array
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
#include <stdio.h>
int main()
{
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" );
for ( int i = 0; i < 3; i++) {
for ( int j = 0; j < 3; j++) {
printf ( "%d " , arr[i][j]);
}
if (i == 2)
break ;
printf ( " , " );
}
return 0;
}
|
C++
#include <iostream>
using namespace std;
int main()
{
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
import java.io.*;
class GFG {
public static void main(String[] args)
{
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++) {
System.out.print(Arr[i][j] + " " );
}
System.out.println( " " );
}
System.out.println( "Wiring:" );
for ( int i = 0 ; i < Arr.length; i++) {
for ( int j = 0 ; j < Arr[ 0 ].length; j++) {
System.out.print(Arr[i][j] + " " );
}
if (i == Arr.length - 1 )
break ;
System.out.print( " , " );
}
}
}
|
Python3
arr = [[ 1 , 2 , 3 ], [ 4 , 5 , 6 ], [ 7 , 8 , 9 ]]
for row in arr:
for val in row:
print (val, end = " " )
print ()
print ( "Wiring:" )
for i, row in enumerate (arr):
for val in row:
print (val, end = " " )
if i = = 2 :
break
print ( "," ,end = " " )
|
C#
using System;
using System.Collections.Generic;
class GFG {
static public void Main()
{
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
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( " , " );
}
|
Output
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.
 Wiring in a 3-dimensional array
Example:
C
#include <stdio.h>
int main()
{
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++) {
printf ( "%d " ,Arr[i][j][k]);
}
printf ( "\n" );
}
printf ( "\n" );
}
return 0;
}
|
C++
#include <iostream>
using namespace std;
int main()
{
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;
for ( int i = 0; i < m; i++) {
for ( int j = 0; j < n; j++) {
for ( int k = 0; k < o; k++) {
cout<<Arr[i][j][k]<< " " ;
}
cout<<endl;
}
cout<<endl;
}
return 0;
}
|
Java
import java.io.*;
class GFG {
public static void main(String[] args)
{
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 } },
};
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++) {
System.out.print(wiringArr[i][j][k]
+ " " );
}
System.out.println();
}
System.out.println();
}
}
}
|
Python3
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 ])
for i in range (m):
for j in range (n):
for k in range (o):
print (Arr[i][j][k], end = ' ' )
print ()
print ()
|
C#
using System;
using System.Collections.Generic;
class Gfg
{
public static void Main( string [] args)
{
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;
for ( i = 0; i < m; i++)
{
for ( j = 0; j < n; j++)
{
for ( k = 0; k < o; k++)
{
Console.Write(Arr[i,j,k]+ " " );
}
Console.Write( "\n" );
}
Console.Write( "\n" );
}
}
}
|
Javascript
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;
for (let i = 0; i < m; i++) {
for (let j = 0; j < n; j++) {
for (let k = 0; k < o; k++) {
process.stdout.write(Arr[i][j][k] + " " );
}
console.log();
}
console.log();
}
|
Output
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
|