Array Types


Array Types

Array with only one dimension

Array of two dimensions

Array in three dimensions

Multi-dimensional arrays include two-dimensional and three-dimensional arrays. Multi-dimensional arrays include arrays of four or more dimensions, among other forms of arrays.

One-dimensional Array

The elements of a one-dimensional array are stored in contiguous memory regions and are accessed using a single index value. It's a sequential data structure that stores all of the components in order.

The elements are kept in memory in order, and the array variable is essentially a pointer to the address of the array's first element. This is referred to as the base address. The following formula can be used to calculate the address of any other element.

initialization One-dimensional Array syntax

dataType arrname[arrsize]=(value1, value2, value3,….);

Example :

#include <iostream> using namespace std; int main() { int x[5] = {5, 41, 15, 0, -5}; cout << "\nThe numbers are: "; // Printing array elements using intex for (int i = 0; i < 5; ++i) { cout << x[i] << " "; } return 0; }

OUTPUT:

array

Where

  • ADDRESS -memory location of the array's Kth element (To be calculated)
  • ARRAY – the ARRAY's name
  • WORDLENGTH - the number of bytes required to hold one element, which varies based on the data type, such as 1 byte for a character value and 2 bytes for an integer value.
  • LOWERBOUND – index of the array's initial element.

BASEADDRESS – The address of the array's first element. A snapshot of the memory in this example shows how an array of size 8 is stored in memory. Put the numbers in the preceding formula to determine the memory address of a certain element, say the 6th element of the array.

K=6

WORDLENGTH=2 (integer information)

LOWERBOUND =1 LOWERBOUND =1 LOWERBOUND =1 (index of first element of the array)

1001 BASEADDRESS

Two-dimensional Array

A two-dimensional array is a tabular representation of data with elements stored in rows and columns, and it is one of the most common array forms. A two-dimensional array is made up of M X N elements arranged in M rows and N columns. Two subscripts are necessary to access any element in a two-dimensional array. These subscripts define the position of an element in a certain row and column. The first index is for the row number, while the second index is for the column index. The first index values row=2 column=3 are utilised to retrieve element 56 in the example.

Syntax

dataType arrayName[rows][columns]; //Example int twodimen[4][3];

Example:

#include <iostream> using namespace std; int main() { int i=0,j=0; int arr[4][3]={{1,2,3},{2,3,4},{3,4,5},{4,5,6}}; //Two-dimensional Array for(i=0;i<4;i++) { for(j=0;j<3;j++) { cout <<"arr["<<i<<"]"<<"arr["<<j<<"] ="<<arr[i][j]<<"\n"; }//end of j }//end of i return 0; }

OUTPUT:

string type

Three-dimensional Array

A three-dimensional array is a depth-added expansion of a two-dimensional array. It resembles a cube with rows, columns, and a third dimension of depth. To get to any element in a three-dimensional array, you'll need three subscripts for the element's row, column, and depth. The depth (dimension or layer) index is the first, the row index is the second, and the column index is the third. The index values (2,0,3) are utilised to reach element 24 in the example.