You are on page 1of 10

Array

An Array is collection of similar data element. These Data Element have


same data type. The Element of array stored in consecutive memory
Location. It is reference by an Index(also known as subscript).

Declaration of Array:- Type name[size];

For Example:- int marks[10];

Note: Array Index start from zero.


Initialize the Element

Storing value in an
Array Input value for the Elements

Assign value to the element

Storing value in an Array


Array and Pointer
int arr[5]={1,2,3,4,5};
int *ptr;
ptr=&arr[0];

1 2 3 4 5 1 2 3 4 5

arr[0] arr[1] arr[2] arr[3] arr[4] arr[0] arr[1] arr[2] arr[3] arr[4]

Memory Representation of arr[ ]


ptr
Ptr point to first element of the array

Note:- We know very well that the Pointer stored the address of another variable.
Two Dimensional Array

A two dimensional array is specified using two subscript where one subscript
denote the row and other denote the column.
Two dimensional array which can be view as array of array.
column
dimension

rows
First

Second dimension

Two dimensional Array


For Example:-

int arr[3][5];

Rows
Col 0 Col 1 Col 2 Col 3 Col 4
column
Row 1 Arr[0][0] Arr[0][1] Arr[0][2] Arr[0][3] Arr[0][4]

Row 2 Arr[1][0] Arr[1][1] Arr[1][2] Arr[1][3] Arr[1][4]

Row 3 Arr[2][0] Arr[2][1] Arr[2][2] Arr[2][3] Arr[2][4]


Multi Dimensional Array

A Multi-Dimensional Array is declared and initialized the same way


we declare and initialize one and two dimensional array.

The format of declaration of a multidimensional array in C is given


below:

data_type array_name [expr 1] [expr 2] …. [expr n];

where data_type is the type of array such as int, char etc.,


array_name is the name of array and expr 1, expr 2, ….expr n are
positive valued integer expressions.
Sparse Matrix

Matrices with good number of zero entries are called sparse


matrices.
A triangular matrix is a square matrix in which all the elements either
above or below the main diagonal are zero. Triangular matrices are
sparse matrices

A tridiagonal matrix is a square matrix in which all the elements


except for the main diagonal, diagonals on the immediate upper and
lower side are zeroes. Tridiagonal matrices are also sparse matrices
Representati on of Array
Row Major Representation

The first method of representing a two-dimensional array in memory is the


row major representation. Under this representation, the first row of the
array occupies the first set of the memory location reserved for the array,
the second row occupies the next set, and so forth.

Row 0 Row 1 Row 2 Row 3 Row 4 ………… Row N

Schematic of a Row major representation of an Array


Column Major Representation

The second method of representing a two-dimensional array in memory is


the column major representation. Under this representation, the first
column of the array occupies the first set of the memory locations reserved
for the array. The second column occupies the next set and so forth.

Col 0 Col 1 Col 2 Col3 ………. Col N

Schematic of a Column major representation of an Array

You might also like