SUBJECT: CP (THEORY QUESTIONS: ARRAY)
1. What is an Array?
An array is a group (or collection) of similar data types.
In array, all the elements are stored in a contiguous memory location and all the elements can
be referenced using the same name called array name.
2. How to define an array?
An array in C is defined as following:
data_type array_name [size];
data_type: It is the type of elements that an array stores. If array stores character elements then
type of array is ‘char’. If array stores integer elements then type of array is ‘int’. If type of elements
in array is structure objects then type of array becomes
the structure.
array_name: This is the name that is given to array.
size: This value in subscripts [ ] indicates the number of elements the array can store.
For example, an array of integers can be defined as:
int arr[5];
Memory bytes allocated for the array depends on the datatype of the elements stored in array.
For example in the above declaration, each of the integer element require 2 bytes, so total bytes
allocated for the array ‘arr’ is (5*2 = 10 ) bytes.
Memory representation of array:
Assuming the array starts at address 1880, following figure shows memory representation of arr.
Note that in C array index starts at 0.
Here first element would be stored ar memory address 1880
Second would be stored at memory address 1882….and so on
Last element that is 5th element would be stored at memory location 1888.
arr[0] arr[1] arr[2] arr[3] arr[4]
1880 1882 1884 1886 1888
3. How to initialize an array?
An array can be initialized in many ways as shown in the code-snippets below.
1) Method 1: Initializing each element of an array separately.
For example:
int arr[10];
int i = 0;
for(i=0; i<10; i++)
{
arr[i] = i; // Initializing each element separately
}
1
2) Method 2: Initializing an array at the time of declaration.
For example:
int arr[ ] = {1,2,3,4,5};
In the above example an array of five integers is declared. Note that, since we are initializing array
at the time of declaration, we need not to mention its size. Size will automatically be calculated from
the number of values. In this case, the size will be 5.
3) Method 3: Initializing all elements of array to 0.
int arr[10] = {0};
All elements in the array will be initialized to zero.
4) Method 4: Initializing few elements in array
int arr[10] = {1,2,3,4,5};
Here first five elements in array would be 1,2,3,4,5 and rest all will set to 0. That is elements with
missing values will be initialized to zero.
1 2 3 4 5 0 0 0 0 0
arr[0] arr[1] arr[2] arr[3] arr[4] arr[5] arr[6] arr[7] arr[8] arr[9]
4. How to access values in an Array?
An array element is accessed by using its subscript or index.
For example, we have declared array of integer of size 5.
int arr[5] = ( 10,20,30,40,50};
Below is shown the memory representation of the array arr.
10
20 30 40 50
arr[0] arr[1] arr[2] arr[3] arr[4]
Then each individual element of the array is accesses by using array name and its subscript (or
index). That is by using
array_name[subscript]
In the above example,
First element is accessed using arr[0]
Second element is accesed using arr [1]
…
…
In C, array index/ subscript starts with 0, so first element is referred by arr[0], second by arr[1]
and so on.
5. How to declare two dimensional array?
The two-dimensional array can be defined as an array of arrays.
The 2D array is organized as matrices which can be represented as the collection of rows and
columns.
2
For example:
datatype 2D_array_name[rows][cols];
Example: float a[2][4];
Here, a, is, an array of two dimension, which is an example of multidimensional array. This array has
2 rows and 4 columns. It can hold 2*4= 8 elements.
Array elements of above example can be visualized as below:
Col1 Col2 Col3 Col4
row1 a[0][0] a[0][1] a[0][2] a[0][3]
Row2 a[1][0] a[1][1] a[1][2] a[1][3]
Note that, here subscript starts with zero, so a[0][0] represents the first element in 2D array, a[0][1]
represents the second element and so on…
Actual representation of above 2D array in memory is shown below.
a[0][0] a[0][1] a[0][2] a[0][3] a[1][0] a[1][1] a[1][2] a[1][3]
Row 1 Row 2
6. What are 3D arrays or Muti- dimensional arrays?
C allows array of 2 or more dimensions.
Arrays with two or more dimensions are called multi-dimensional array.
2D Array
2D array is an array of array.
2D arrays can be visualized as shown in following figure.
Col1 Col2 Col3 Col4
row1 a[0][0] a[0][1] a[0][2] a[0][3]
Row2 a[1][0] a[1][1] a[1][2] a[1][3]
The above array can be declared in C as,
int a[2][4];
means it has 2 rows and 4 columns.
Total number of elements that can be stored in a multidimensional array can be calculated by
multiplying the size of all the dimensions.
For example, above two dimensional array can store (2*4) = 8 elements.
3D Arrays:
3D arrays can be considered as an array of matrices or array of 2D arrays.
3D array can be visualized as shown in the below figure.
3
The above 3D array can be declared in C as,
int a[3][3][3];
Here the total number of elements that array can store = 3*3*3 =27 elements.
In general, Suppose there is a multidimensional array arr[ i ] [ j ] [ k ] [m]. Then this array can hold (i*j*k*m)
numbers of data. Similarly, the array of any dimension can be initialized in C programming.
7. How to initialize two dimensional / Multi dimensional arrays?
Initialization of 2D Array:
Two dimensional arrays can be initialized in many ways.
One way is shown below:
int a[2][3] = { {1, -2, 3 }, { 4, 8 ,6 }};
in the above example elements in each row are kept in separate curly braces.
So there are two rows {1, -2, 3 } and {4,8,6}. Each row having three elements (columns).
1 -2 3 <- first row
4 8 6 <- Second row
Another way is,
int a[ ][3] ={ {1, -2, 3 }, { 4, 8 ,6 }};
Note that , in the above example we did not mention the number of rows. Thus number of rows is optional
whiling initializing the array. But, it is mandatory to mention number of columns.
The third way is,
int a[2][3] = { 1,-2,3,4,8,6 };
In the above example all the elements are kept in one curly bracket. Compiler automatically arranges
element by looking at the number of rows and column mention.
4
Initialization of three-dimensional Array:
Method 1:
int x[2][3][4] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10,
11, 12, 13, 14, 15, 16, 17, 18, 19,
20, 21, 22, 23};
Method 2 (Better Method):
int x[2][3][4] =
{
{ {0,1,2,3}, {4,5,6,7}, {8,9,10,11} },
{ {12,13,14,15}, {16,17,18,19}, {20,21,22,23} }
};
Things that you must consider while initializing a 2D array
/* Valid declaration*/
int abc[2][2] = {1, 2, 3 ,4 };
/* Valid declaration*/
int abc[ ][2] = {1, 2, 3 ,4 }; // number of rows(first dimension) is optional while initializing array
/* Invalid declaration – you must specify second dimension*/
int abc[ ][ ] = {1, 2, 3 ,4 }; // you must specify no. of columns
/* Invalid because of the same reason mentioned above*/
int abc[2][ ] = {1, 2, 3 ,4 }; // you must specify no. of columns
8. Consider integer array of size 4 rows and 5 cols defined. if Starting address is 4000, what is
address of element a[2][4] ?
The address of the element in array a[M][N] is calculated as
address of a[ i ][ j ] = Base_address + S * (i*N + j)
Where S is the size of one element in array (e.g. 2 bytes for int, 1 byte for char etc..),
i is row number of the element whose address you want to find
j is column number of the element whose address you want to find.
M is total number of rows in array and
N is total number of columns in array.
Here M = 4, N = 5
Base_address = 4000
Here S = 2 bytes for integer.
We need to find address of a[2][4]. Here i = 2 and j =4.
Address of a [2] [4] = 4000 + (2) * (2 * 5 + 4)
= 4000 + (2) * (14)
= 4000 + 28
= 4028
Address of element a[2][4] is 4028.
5
Additional questions asked in University:
1. What is an array? What does an array name signify? Can array index be negative?
o An array is a group (or collection) of similar data types.
o In array, all the elements are stored in a contiguous memory location and all the elements can be
referenced using the same name called array name.
o An array is a data structure commonly used in computer programs to organize data so that a related
set of values can be easily sorted or searched.
o Yes, array index can be negative. It can be used to print the array in a reverse order.