### what is array

An array is a container object that holds a fixed number of values of a single type. The length of an array is established when the array is created. After creation, its length is fixed.

### Declaration and initialization of array

An array is a variable that can store multiple values. For example, if you want to store 100 integers, you can create an array for it.

dataTypearrayName[arraySize];

int data[100];

It is possible to initialize an array during declaration. For example,

int mark[5] = {19, 10, 8, 17, 9};

You can also initialize an array like this.

int mark[] = {19, 10, 8, 17, 9};

### Accessing array

Array can be accessed using array-name and subscript variable written inside pair of square brackets [].

arr[3] = Third Element of Array

arr[5] = Fifth Element of Array

arr[8] = Eighth Element of Array

#include<stdio.h>

#include<conio.h>

void main()

{

intarr[] = {51,32,43,24,5,26};

int i;

for(i=0; i<=5; i++) {

printf(“\nElement at arr[%d] is %d”,i,arr[i]);

}

getch();

}

### Displaying array

The implementation of the above derived pseudocode is as follows −

#include <stdio.h>

int main() {

int array[10] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 0};

int loop;

for(loop = 0; loop < 10; loop++)

printf(“%d “, array[loop]);

return 0;

}

The output should look like this −

1 2 3 4 5 6 7 8 9 0

### Sorting arrays and functions

Sorting is the method of arranging a given array in accending or decending order.

**#include <stdio.h>**

**#define ARRAY_SIZE 5**

int**main**()

{

int numbers[ARRAY_SIZE], i ,j ,temp;

*// Read Input*

for (i = 0; i < ARRAY_SIZE; i++)

{

printf(“Enter the Number : %d : “, (i+1));

scanf(“%d”, &numbers[i]);

}

*// Array Sorting – Ascending
Order*

for (i = 0; i < ARRAY_SIZE; ++i)

{

for (j = i + 1; j < ARRAY_SIZE; ++j)

{

if (numbers[i] > numbers[j])

{

temp = numbers[i];

numbers[i] = numbers[j];

numbers[j] = temp;

}

}

}

printf(“Sorting Order Array: \n”);

for (i = 0; i < ARRAY_SIZE; ++i)

printf(“%d\n”, numbers[i]);

return0;

}

Enter the elements you want to sort:

45, 50, 36, 3, 87,

Sorted array:3, 36, 45, 50, 87.

## Two dimensional array:

An array of arrays is known as 2D array. The two dimensional (2D) array in C programming is also known as matrix. A matrix can be represented as a table of rows and columns.

### declaration

The syntax declaration of 2-D array is not much different from 1-D array. In 2-D array, to declare and access elements of a 2-D array we use 2 subscripts instead of 1.

**Syntax**: datatypearray_name[ROW][COL];

The total number of elements in a 2-D array is ROW*COL. Let’s take an example.

intarr[2][3];

This array can store 2*3=6 elements. You can visualize this 2-D array as a matrix of 2 rows and 3 columns.

### intialization

There are two ways to initialize a two Dimensional arrays during declaration.

int disp[2][4] = { {10, 11, 12, 13}, {14, 15, 16, 17} };

OR

int disp[2][4] = { 10, 11, 12, 13, 14, 15, 16, 17};

Although both the above declarations are valid, I recommend you to use the first method as it is more readable, because you can visualize the rows and columns of 2d array in this method.

### accessing of array element

The individual elements of the above array can be accessed by using two subscript instead of one. The first subscript denotes row number and second denotes column number. As we can see in the above image both rows and columns are indexed from 0. So the first element of this array is at arr[0][0] and the last element is at arr[1][2]. Here are how you can access all the other elements:

arr[0][0] – refers to the first element

arr[0][1] – refers to the second element

arr[0][2] – refers to the third element

arr[1][0] – refers to the fourth element

arr[1][1] – refers to the fifth element

arr[1][2] – refers to the sixth element

**Simple Two dimensional(2D) Array Example**

#include<stdio.h> int main(){ /* 2D array declaration*/ int disp[2][3]; /*Counter variables for the loop*/ int i, j; for(i=0; i<2; i++) { for(j=0;j<3;j++) { printf("Enter value for disp[%d][%d]:", i, j); scanf("%d", &disp[i][j]); } } //Displaying array elements printf("Two Dimensional array elements:\n"); for(i=0; i<2; i++) { for(j=0;j<3;j++) { printf("%d ", disp[i][j]); if(j==2){ printf("\n"); } } } return 0; }

Output:

Enter value for disp[0][0]:1 Enter value for disp[0][1]:2 Enter value for disp[0][2]:3 Enter value for disp[1][0]:4 Enter value for disp[1][1]:5 Enter value for disp[1][2]:6 Two Dimensional array elements: 1 2 3 4 5 6

## Memory representations of array

A 2D array is stored in the computer’s memory one row following another. The address of the first byte of memory is considered as the memory location of the entire 2D array. … Again one should not think of a 2D array as just an array with two indexes. You should think of it as an array of arrays.

**Row Major System**:

The address of a location in Row Major System is calculated using the following formula:

Address of A [ I ][ J ] = B + W * [ N * ( I – Lr ) + ( J – Lc ) ]

**Column Major System**:

The address of a location in Column Major System is calculated using the following formula:

Address of A [ I ][ J ] Column Major Wise = B + W * [( I – Lr ) + M * ( J – Lc )]

Where,

B = Base address

I = Row subscript of element whose address is to be found

J = Column subscript of element whose address is to be found

W = Storage Size of one element stored in the array (in byte)

Lr = Lower limit of row/start row index of matrix, if not given assume 0 (zero)

Lc = Lower limit of column/start column index of matrix, if not given assume 0 (zero)

M = Number of row of the given matrix

N = Number of column of the given matrix

## Multidimensional array

A multidimensional array is an array containing one or more arrays. PHP understands multidimensional arrays that are two, three, four, five, or more levels deep. However, arrays more than three levels deep are hard to manage for most people.

It is an array of arrays; an array that has multiple levels. The simplest multi-dimensional array is the 2D array, or two-dimensional array. It’s technically an array of arrays, as you will see in the code. A 2D array is also called a matrix, or a table of rows and columns.

Declaring a multi-dimensional array is similar to the one-dimensional arrays. For a 2D array, we need to tell C that we have 2 dimensions.