C++ Array

array in c++

Objective

What is Array?

Definition:

An array is defined as finite ordered collection of Homogeneous data, stored in contiguous memory locations.

In Short:

Finite means:  data range (size of array) must be defined

Homogeneous means: An array is a group or collection of same data types.

Ordered means: Stored at contiguous memory locations or address

It is a type of data structure

One-dimensional array is like a list.

Array name is base address or first element address. Both addresses are same.

For e.g. int array holds the elements of int types while a char array holds the elements of char types.

Where arrays are used

  • To store roll number of students
  • To store name of employee
  • To store marks of students
  • or to store list of numbers or characters etc..
base address


How to declare array?

Arrays must be declared before they are used. General form of array declaration is:

Note:

data_type: it can be any valid C++ data type

array_size: must be an integer & constant greater than 0

array_name: must be follow naming convention of array declaration

Name must not start with a digit

Name can consist of alphabets, digits and underscore _

Blank or spaces are not allowed

Keywords are not allowed

Upper and lower case names are treated as different, as C++ is case-sensitive

For example

How to access array elements?

  • An element is accessed by indexing the array name.
  • Arrays have 0 as the first index not 1. In this example, item[0].
  • If the size of an array is n, to access the last element, (n-1) index is used.

For example:

Here is code:

How to initialize an array?

one dimension array
  • An array is declared it must be initialized.
  • By Default, it will contain garbage value (any random value).
  • An array can be initialized in two ways Compile Time Runtime

Compile Time Initialization:

Syntax:

Note: At the time of initialization array_size is optional.

For Example:

Before initialization:

Output:

before initialization


After initialization:

Output:

Tips:

Valid or Invalid Array Initialization

Runtime Initialization:

Output:

1d array output

2D Array or Multi-Dimension

What is Multi-Dimension Array?

  • multidimensional array is an array with more than one dimension or levels.
  • Most of the operations that you can perform on Table (i.e., 2D arrays, contain rows and column).

For Example: 2D Array


What is 2D Array??

  • Data in multidimensional arrays are stored in tabular form.
  • Both the row’s and column’s index begins from 0

2D Array Declaration:

Syntax:

How to initialize 2D array?

2D array is also have compile time and run time initialization. By default we are following row major order for initialization.

Compile Time Initialization (2D Array)
Syntax:

Note: At the time of initialization

row_size: Row Size is optional

column_size: Column Size is compulsory

For Example:

Tips:

int items [ 2 ][ 3 ] = {{111, 223, 10}, {-11, 4, 9}};                            //valid

int items[2][3] = {{12,2},{1,2,3}};                                                   //valid

int items[2][3] = {{12,2,},{1,2,3}};                                                  //valid

int items [ ][ 3 ]  = {{12, 213, 10}, {-1, 25, -9}, {25,52,65}};           //valid

int items [ 2 ][ 3 ]  = {2, 3, 1, -1, 5, 9};                                          //valid

int items [ 2 ][ 3 ]  = {2, 3, 1, -1, 5};                                              //valid

int items[2][3] = {{12,2,3},{1,2,3},{12,4,5}};                                  //Invalid

int items[2][3] = {{,2,2},{1,2,3}};                                                   //Invalid

int items [ 3][  ]  = {{12, 213, 10}, {-1, 25, -9}, {25,52,65}};          //Invalid

Runtime Initialization (2D Array):

Output:

output 2d array

Memory Address Calculation

One Dimensional Array (1D Array)
1d array memory calculation

Array of an element inside an array say “item[I]” is calculated by using this formula:

Note:

item[I] :  Address of array element is item[I]

B         :  Base Address / First Address of array element. Both are  same.

W          :  Size / Width in byte. For eg. int (2 byte), float(4 byte) and so on.

I           :  Index / Subscript of array elements whose address is to be found.

LB       :  Lower Bound / First element Index value, if not specified assume 0 (Zero)

Example:

Given the base address of an array item[1100 … 2000] as 1024 and size of each element is 2 bytes in ram. Find the address of item[1500].

Sol.:

Given values are:

  • Base address (B)   : 1024
  • Size (W)                    :  2 bytes
  • Lower Bound (LB)  :  1100
  • Index (I)                   :   1500

AddressOf( item[I] ) = B + ( W * ( I + LB) )

=  1024 + ( 2 * ( 1500 – 1100) )

=  1024 + ( 2 * 400 )

=  1024 + 800

=  1824

So, item [ 1500 ] address is 1824

Two Dimensional Array (2D Array)

The elements of a 2-D array in memory, these are stored in contiguous memory locations like 1D Array. 2D Array elements are accessed in two ways:

2 d array memory representation

Row Major Order

2D Array can be accessed Row-Wise. The Elements of the 1st row is stored first, then the elements of 2nd row and so on.

Row major order

Row Major Order is calculated using this formula:

Note: 

R          :  The Row number of the element.

C          :  The Column number of the element.

B          :  Base Address / First Address of array element. Both are  same.

S          :  Size / Width in byte. For eg. int (2 byte), float(4 byte) and so on.

n          :  Total number of columns in the array.

LBR     :  Lower Bound of the Row.

LBC     :  Lower Bound of the Column.

Example:

An Array Item[15][10] is stored along the row in the memory with each element requiring 8 bytes of storage. If the base address of array Item is 14000, find out the address of Item[10][7].

Sol.:

Given values are:

  • Item[10][7]              :  ?
  • Base address (B)   :  14000
  • Size (W)                    :  8 bytes
  • n                               :  10
  •                               : 15
  • C                               : 10

AddressOf( item[R][C] ) = B + W * [ n * ( R – LBR ) + (C – LBC) ]

Item[10][7] =  14000 + 8 * [ 10 ( 10 – 0 ) + ( 7 – 0 ) ]

=  14000 + 8 * [ 10 ( 10 ) + 7]

=  14000 + 8 * [ 100 + 7 ]

=  14000 + 8 * 107

=  14000 + 856

=  14856

So, Item[10][7] address is 14856

Column Major Order

2D Array can be accessed Column-Wise. The Elements of the 1st column is stored first, then the elements of 2nd column and so on.

column major order

Column Major Order is calculated using this formula:

Note: 

R          :  The Row number of the element.

C          :  The Column number of the element.

B          :  Base Address / First Address of array element. Both are  same.

W          :  Size / Width in byte. For eg. int (2 byte), float(4 byte) and so on.

m          :  Total number of Rows in the array

LBC     :  Lower Bound of the Column.

LBR     :  Lower Bound of the Row.

Example:

An array Item[20][10] is stored in the memory along the column with each of the elements occupying 2 bytes. Find out the memory address of Item[10][5], if the element T[2][9] is stored at the address 7600.

Sol.:

Given values are:

  • Item[2][9]                 :  7600
  • Base address (B)   :  ?
  • Size (W)                    :  2 bytes
  • m                              :  20
  •                               :  2
  • C                               :  9

AddressOf( item[R][C] ) = B + W * [ ( R – LBR ) + m * (C – LBC) ]

7600 =  B + 2 * [ ( 2 – 0 ) + 20 * ( 9 – 0 ) ]

7600 =  B + 2 *  ( 2 + 180 )

7600 =  B + 364

B =  7600 – 364

B = 7236

So, Base address is 7236

  • Item[10][5]              :  ?
  • Base address (B)   :  7236
  • Size (S)                    :  2 bytes
  • m                              :  20
  •                               :  10
  • C                               :  5

Item[10][5] = 7236 + 2 * [ ( 10 – 0 ) + 20 * ( 5 – 0 ) ]

= 7236 + 220

= 7456

Please follow and like us:

Leave a Reply

Your email address will not be published. Required fields are marked *