Array
An array serves as a data structure that holds data or values in a sequential manner, with memory allocated in a consecutive fashion. It is essential for the values stored within arrays to be of the same data type.
For example:
arr = [1,2,3,4,6] represents an integer array containing five elements, forming a one-dimensional array.
We have the ability to generate one-dimensional, two-dimensional, or multi-dimensional arrays in C or C++ programming languages.
A 2D array is essentially a collection of 1D arrays linked together in a different orientation.
In most cases, 2-D arrays are represented as matrices or grids.
For example:
arr =[ [1 2 3 4]
[5 6 7 8]
[9 10 11 12]
[13 14 15 16] ]
This serves as a demonstration of a 4X4 array containing integers in two dimensions, totaling 16 elements.
Example:
Three-dimensional arrays consist of multiple two-dimensional arrays organized in the third dimension. These structures are commonly known as cubes or cuboids.
For example:
arr=[ [[1 2] [3 4]]
[[5 6] [7 8]]
[[9 10] [11 12]]]
This serves as an illustration of a three-dimensional integer array with dimensions 2x2x3, containing a total of 12 elements, which can be determined by multiplying the dimensions (2x2x3 = 12).
When discussing four-dimensional arrays, they can be conceptualized as a series of 3-dimensional arrays linked in the fourth dimension.
For a 4-dimensional array, the typical syntax is:
Declare an array named arrayname with multiple dimensions specified by element1, element2, element3, and element4 for storing data of datatype.
For example:
int arr[4][5][1][6];
In the previously mentioned example, the 4D array is set up, containing a total of 120 elements with dimensions 4x5x1x6.
4D arrays are often considered intricate and challenging to conceptualize.
C Example:
#include <stdio.h>
int main() {
int d1,d2,d3,d4;
// declaring the size of the array
int arr[2][2][2][2];
int len=2;
arr[0][0][0][0]=1;
arr[0][0][0][1]=2;
arr[0][0][1][0]=3;
arr[0][0][1][1]=4;
arr[0][1][0][0]=5;
arr[0][1][0][1]=6;
arr[0][1][1][0]=7;
arr[0][1][1][1]=8;
arr[1][0][0][0]=9;
arr[1][0][0][1]=10;
arr[1][0][1][0]=11;
arr[1][0][1][1]=12;
arr[1][1][0][0]=13;
arr[1][1][0][1]=14;
arr[1][1][1][0]=15;
arr[1][1][1][1]=16;
for(int d1=0;d1<2;d1++){
for(int d2=0;d2<2;d2++){
for(int d3=0;d3<2;d3++){
for(int d4=0;d4<2;d4++){
printf("%d\n",arr[d1][d2][d3][d4]);
}
}
}
}
}
Output:
C++ Example:
#include <iostream>
using namespace std;
int main() {
int d1,d2,d3,d4;
// declaring the size of the array
int arr[2][2][2][2];
int len=2;
arr[0][0][0][0]=1;
arr[0][0][0][1]=2;
arr[0][0][1][0]=3;
arr[0][0][1][1]=4;
arr[0][1][0][0]=5;
arr[0][1][0][1]=6;
arr[0][1][1][0]=7;
arr[0][1][1][1]=8;
arr[1][0][0][0]=9;
arr[1][0][0][1]=10;
arr[1][0][1][0]=11;
arr[1][0][1][1]=12;
arr[1][1][0][0]=13;
arr[1][1][0][1]=14;
arr[1][1][1][0]=15;
arr[1][1][1][1]=16;
for(int d1=0;d1<2;d1++){
for(int d2=0;d2<2;d2++){
for(int d3=0;d3<2;d3++){
for(int d4=0;d4<2;d4++){
cout<<arr[d1][d2][d3][d4]<<endl;
}
}
}
}
}
Output:
Determining the Length of the Array
If we need to determine the total count of elements within an array, we can employ the 'sizeof' operator. This operator provides the total number of bytes occupied by the variable, which we can then divide by the bytes utilized by a single variable to obtain the total element count.
For example:
In the example given of a 4-dimensional array, there are a total of 16 elements present within the array. Given that each integer variable occupies 4 bytes in memory, the complete array will require a total of 64 bytes of memory to store all its elements.
The total number of elements is equal to the total size of the array in bytes divided by the size of one element in bytes.
C Example:
#include <stdio.h>
int main() {
int d1,d2,d3,d4;
// declaring the size of the array
int arr[2][2][2][2];
int len=2;
arr[0][0][0][0]=1;
arr[0][0][0][1]=2;
arr[0][0][1][0]=3;
arr[0][0][1][1]=4;
arr[0][1][0][0]=5;
arr[0][1][0][1]=6;
arr[0][1][1][0]=7;
arr[0][1][1][1]=8;
arr[1][0][0][0]=9;
arr[1][0][0][1]=10;
arr[1][0][1][0]=11;
arr[1][0][1][1]=12;
arr[1][1][0][0]=13;
arr[1][1][0][1]=14;
arr[1][1][1][0]=15;
arr[1][1][1][1]=16;
for(int d1=0;d1<2;d1++){
for(int d2=0;d2<2;d2++){
for(int d3=0;d3<2;d3++){
for(int d4=0;d4<2;d4++){
printf("%d\n",arr[d1][d2][d3][d4]);
}
}
}
}
int array_size=sizeof(arr)/sizeof(arr[0][0][0][0]);
printf("total number of elements in the given array are %d",array_size);
return 0;
}
Output:
C++ Example:
#include <iostream>
using namespace std;
int main() {
int d1,d2,d3,d4;
// declaring the size of the array
int arr[2][2][2][2];
int len=2;
arr[0][0][0][0]=1;
arr[0][0][0][1]=2;
arr[0][0][1][0]=3;
arr[0][0][1][1]=4;
arr[0][1][0][0]=5;
arr[0][1][0][1]=6;
arr[0][1][1][0]=7;
arr[0][1][1][1]=8;
arr[1][0][0][0]=9;
arr[1][0][0][1]=10;
arr[1][0][1][0]=11;
arr[1][0][1][1]=12;
arr[1][1][0][0]=13;
arr[1][1][0][1]=14;
arr[1][1][1][0]=15;
arr[1][1][1][1]=16;
for(int d1=0;d1<2;d1++){
for(int d2=0;d2<2;d2++){
for(int d3=0;d3<2;d3++){
for(int d4=0;d4<2;d4++){
cout<<arr[d1][d2][d3][d4]<<endl;
}
}
}
}
int array_size=sizeof(arr)/sizeof(arr[0][0][0][0]);
cout<<"number of elements in the given array is : "<<array_size<<endl;
}
Output:
Generally, an array can possess multiple dimensions, and an n-dimensional array can be depicted as shown below:
Where d 1 ,d 2 ….d n-1 represent the quantity of elements in the n th dimension. To determine the total number of elements in the array with n dimensions, the formula below can be applied:
In most cases, we typically deal with 1D, 2D, and 3D arrays, but in practical situations, problems can exist in multiple dimensions.