Arrays

Arrays

8 | Arrays


     Basically, array is a group of values/items/elements of same data-type. A set of values/items/elements of same data-type can be placed/stored in an array. There are three types of arrays i.e. one dimensional arrays, two dimensional arrays and multi-dimensional arrays. These types are explained below.


8.0 | Contents :

     1.   One Dimensional Arrays
     2.   Two Dimensional Arrays
     3.   Multi-Dimensional Arrays


8.1 | One Dimensional Arrays :

     An array, which requires only one index to define any of the value/item/element in that array is called as one dimensional array. In other words, an array which have only one/single row, is called as one dimensional array.

Declaration :
          data_type array_name[size_of_the_array];

     where, data_type is the valid data-type
     and array_name represents the name of array
     and size_of_the_array represents the size of array

Initialization :
          data_type array_name[size_of_the_array] = {list of values (separated by comma)};

Use :
          printf("format_specifier",array_name[index_of_the_element]);


Example 1 :

    int arr[5]={1,2,3,4,5};

     Here, we declared an array of data-type int (i.e. integer) named as arr of size 5 (i.e. it can store maximum 5 values). If we want to call an element of the array, then we have to use index of that element in the array. The index of an element in an array is lesser by one than the location of that element in that array.
     In other words, like in given array arr[5], if we want to call the first element i.e. 1, then we have to write like this arr[0]. Here, location of 1 in array arr[5] is 1 i.e. 1 is at first location (position) in the array. So, the index of 1 is 0 (index of one dimensional array = location - 1 = 1 - 1 = 0).
     To know more, see the table given below :

Elements
of
arr[5]
Index
of the
Element
(index = location - 1)
Syntax/Format
to Call
the Element
Syntax/Format
to Print
the Element
on the
Output Screen
1 1 - 1 = 0 arr[0] printf("%d",arr[0]);
2 2 - 1 = 1 arr[1] printf("%d",arr[1]);
3 3 - 1 = 2 arr[2] printf("%d",arr[2]);
4 4 - 1 = 3 arr[3] printf("%d",arr[3]);
5 5 - 1 = 4 arr[4] printf("%d",arr[4]);



Example 2 :

    char arr[5]={'a','b','c'};

     Here, we declared an array of data-type char (i.e. character) named as arr of size 5 (i.e. it can store maximum 5 characters). But we stored only three characters.
     So, what about other to remaining locations. The other two remaining locations have values as 0.
     To know more, see the table given below :

Elements
of
arr[5]
Index
of the
Element
(index = location - 1)
Syntax/Format
to Call
the Element
Syntax/Format
to Print
the Element
on the
Output Screen
a 1 - 1 = 0 arr[0] printf("%c",arr[0]);
b 2 - 1 = 1 arr[1] printf("%c",arr[1]);
c 3 - 1 = 2 arr[2] printf("%c",arr[2]);
0 4 - 1 = 3 arr[3] printf("%c",arr[3]);
0 5 - 1 = 4 arr[4] printf("%c",arr[4]);



8.2 | Two Dimensional Arrays :

     An array, which requires two indices to define any of the value/item/element in that array is called as two dimensional array.

Declaration :
          data_type array_name[row][cols];

     where, data_type is the valid data-type
     and array_name represents the name of array
     and rows represents the number of rows of the array
     and cols represents the number of columns of the array

Initialization :
          data_type array_name[row][cols] = {list of values (separated by comma)};
OR
          data_type array_name[row][cols] = { {list of values of 1st row} , {list of values of 2nd row} , ..... , {list of values of nth row} };

Use :
          printf("format_specifier",array_name[row][cols]);


Example 1 :

    int arr[3][2]={1,2,3,4,5,6};
OR
    int arr[3][2]={{1,2},{3,4},{5,6}};

     Here, we declared an array of data-type int (i.e. integer) named as arr of size 6 (size of two dimensional array = row x cols = 3 x 2 = 6) (i.e. it can store maximum 6 values).
     Now, in given array arr[3][2], if we want to call the first element i.e. 1, then we have to write like this arr[0][0]. Here, location of 1 in array arr[3][2] is at first row and first column i.e. 1 is at [1][1] location (position) in the array. So, the index of 1 is [0][0] (index of two dimensional array = [row - 1][column - 1] = [1 - 1][1 - 1] = [0][0]).
     To know more, see the table given below :

Elements
of
arr[3][2]
Index
of the
Element
(index = [row - 1][column - 1])
Syntax/Format
to Call
the Element
Syntax/Format
to Print
the Element
on the
Output Screen
1 [1 - 1][1 - 1] = [0][0] arr[0][0] printf("%d",arr[0][0]);
2 [1 - 1][2 - 1] = [0][1] arr[0][1] printf("%d",arr[0][1]);
3 [2 - 1][1 - 1] = [1][0] arr[1][0] printf("%d",arr[1][0]);
4 [2 - 1][2 - 1] = [1][1] arr[1][1] printf("%d",arr[1][1]);
5 [3 - 1][1 - 1] = [2][0] arr[2][0] printf("%d",arr[2][0]);
6 [3 - 1][2 - 1] = [2][1] arr[2][1] printf("%d",arr[2][1]);

          From the table given below, you can easily understand the indices of elements :

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


Example 2 :

    char arr[4][5] = {'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't'};
OR
    char arr[4][5] = {{'a', 'b', 'c', 'd', 'e'} , {'f', 'g', 'h', 'i', 'j'} , {'k', 'l', 'm', 'n', 'o'} , {'p', 'q', 'r', 's', 't'}};

arr[0][0] arr[0][1] arr[0][2] arr[0][3] arr[0][4]
a b c d e
arr[1][0] arr[1][1] arr[1][2] arr[1][3] arr[1][4]
f g h i j
arr[2][0] arr[2][1] arr[2][2] arr[2][3] arr[2][4]
k l m n o
arr[3][0] arr[3][1] arr[3][2] arr[3][3] arr[3][4]
p q r s t



8.3 | Multi Dimensional Arrays :

     An array, which requires more than two indices to define any of the value/item/element in that array is called as multi dimensional array.

Declaration :
          data_type array_name[dim][row][cols];

     where, data_type is the valid data-type
     and array_name represents the name of array
     and dim represents the number of dimensions of the array
     and rows represents the number of rows of the array
     and cols represents the number of columns of the array

Initialization :
          data_type array_name[dim][row][cols] = {list of values (separated by comma)};

Use :
          printf("format_specifier",array_name[dim][row][cols]);


Note :
          Here, I used three dimensional array for example. The example for nth dimensional array is coming soon.


Example 1 :

    int arr[2][3][2] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12};
OR
    int arr[2][3][2] = {{1,2}, {3,4}, {5,6}, {7,8}, {9,10}, {11,12}};

     Here, we declared an array of data-type int (i.e. integer) named as arr of size 12 (size of three/multi dimensional array = dim x row x cols = 2 x 3 x 2 = 12) (i.e. it can store maximum 12 values).
     Now, in given array arr[2][3][2], if we want to call the first element i.e. 1, then we have to write like this arr[0][0][0]. Here, location of 1 in array arr[2][3][2] is at first dimension, first row and first column i.e. 1 is at [1][1][1] location (position) in the array. So, the index of 1 is [0][0][0] (index of three dimensional array = [dim - 1][row - 1][column - 1] = [1 - 1][1 - 1][1 - 1] = [0][0][0]).
     To know more, see the table given below :

Elements
of
arr[2][3][2]
Index
of the
Element
(index = [dim - 1][row - 1][column - 1])
Syntax/Format
to Call
the Element
Syntax/Format
to Print
the Element
on the
Output Screen
1 [1 - 1][1 - 1][1 - 1] = [0][0][0] arr[0][0][0] printf("%d",arr[0][0][0]);
2 [1 - 1][1 - 1][2 - 1] = [0][0][1] arr[0][0][1] printf("%d",arr[0][0][1]);
3 [1 - 1][2 - 1][1 - 1] = [0][1][0] arr[0][1][0] printf("%d",arr[0][1][0]);
4 [1 - 1][2 - 1][2 - 1] = [0][1][1] arr[0][1][1] printf("%d",arr[0][1][1]);
5 [1 - 1][3 - 1][1 - 1] = [0][2][0] arr[0][2][0] printf("%d",arr[0][2][0]);
6 [1 - 1][3 - 1][2 - 1] = [0][2][1] arr[0][2][1] printf("%d",arr[0][2][1]);
7 [2 - 1][1 - 1][1 - 1] = [1][0][0] arr[1][0][0] printf("%d",arr[1][0][0]);
8 [2 - 1][1 - 1][2 - 1] = [1][0][1] arr[1][0][1] printf("%d",arr[1][0][1]);
9 [2 - 1][2 - 1][1 - 1] = [1][1][0] arr[1][1][0] printf("%d",arr[1][1][0]);
10 [2 - 1][2 - 1][2 - 1] = [1][1][1] arr[1][1][1] printf("%d",arr[1][1][1]);
11 [2 - 1][3 - 1][1 - 1] = [1][2][0] arr[1][2][0] printf("%d",arr[1][2][0]);
12 [2 - 1][3 - 1][2 - 1] = [1][2][1] arr[1][2][1] printf("%d",arr[1][2][1]);

          From the table given below, you can easily understand the indices of elements :

arr[0][0][0] arr[0][0][1]
1 2
arr[0][1][0] arr[0][1][1]
3 4
arr[0][2][0] arr[0][2][1]
5 6
arr[1][0][0] arr[1][0][1]
7 8
arr[1][1][0] arr[1][1][1]
9 10
arr[1][2][0] arr[1][2][1]
11 12


Example 2 :

    char arr[3][2][5] = {'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', '+', '-', '*', '/'};
OR
    char arr[3][2][5] = {{'a', 'b', 'c', 'd', 'e'} , {'f', 'g', 'h', 'i', 'j'} , {'k', 'l', 'm', 'n', 'o'} , {'p', 'q', 'r', 's', 't'} , {'u', 'v', 'w', 'x', 'y'} , {'z', '+', '-', '*', '/'}};

arr[0][0][0] arr[0][0][1] arr[0][0][2] arr[0][0][3] arr[0][0][4]
a b c d e
arr[0][1][0] arr[0][1][1] arr[0][1][2] arr[0][1][3] arr[0][1][4]
f g h i j
arr[1][0][0] arr[1][0][1] arr[1][0][2] arr[1][0][3] arr[1][0][4]
k l m n o
arr[1][1][0] arr[1][1][1] arr[1][1][2] arr[1][1][3] arr[1][1][4]
p q r s t
arr[2][0][0] arr[2][0][1] arr[2][0][2] arr[2][0][3] arr[2][0][4]
u v w x y
arr[2][1][0] arr[2][1][1] arr[2][1][2] arr[2][1][3] arr[2][1][4]
z + - * /




Comments