# Practice questions on Arrays

In this article, we will discuss some important concepts related to arrays and problems based on that. Before understanding this, you should have basic idea about Arrays.

Type 1. Based on array declaration –
These are few key points on array declaration:

• A single dimensional array can be declared as int a or int a[] = {1, 2, 3, 4}. It means specifying the number of elements is optional in 1-D array.
• A two dimensional array can be declared as int a or int a[] = {1, 2, 3, 4, 5, 6, 7, 8}. It means specifying the number of rows is optional but columns are mandatory.
• The declaration of int a will give the values as garbage if printed. However, int a = {1,1} will initialize remaining two elements as 0.

Que – 1. Predict output of following program

```int main()
{
int i;
int arr = {1};
for (i = 0; i
(A) 1 followed by four garbage values:
(B) 1 0 0 0 0
(C) 1 1 1 1 1
(D) 0 0 0 0 0
Solution: As discussed, if array is initialized with few elements, remaining elements will be initialized to 0. Therefore, 1 followed by 0, 0, 0, 0 will be printed.
Que - 2. Predict output of the following program:
int main()
{
int a[][] = {{1,2},{3,4}};
int i, j;
for (i = 0; i
(A) 1 2 3 4
(B) Compiler Error in line ” int a[][] = {{1,2},{3,4}};”
(C) 4 garbage values
(D) 4 3 2 1
Solution: As discussed, specifying the number of columns in 2-D array is mandatory, so, it will give compile time error.
Type 2. Finding address of an element with given base address -
When an array is declared, a contiguous block of memory is assigned to it which helps in finding address of elements from base address.
For a single dimensional array a, address of ith element can be found as:

Where BA represents base address (address of 0th element) and SIZE represents size of each element in the array.
For a two dimensional array x, the elements can be represented as:

As 2-D array is stored in row major order in C language, row 0 will be stored first followed by row 1 and row 2. For finding the address of x, we need to go to 2nd row (each row having 3 elements). After reaching 2nd row, it can be accessed as single dimensional array. Therefore, we need to go to 2nd element of the array. Assuming BA as 0 and size as 1, the address of x will be 0 + (2 * 3 + 2) * 1 = 8.
For a given array with m rows and n columns, the address can be calculated as:
add(a[i][j]) = BA + (i*n + j) * SIZE

Where BA represents base address (address of 0th element), n represents number of columns in 2-D array and SIZE represents size of each element in the array.
Que - 3. Consider the following declaration of a ‘two-dimensional array in C:
char a;

Assuming that the main memory is byte-addressable and that the array is stored starting from memory address 0, the address of a is: (GATE CS 2002)
(A) 4040
(B) 4050
(C) 5040
(C) 5050
Solution: Using the formula discussed,
addr = 0 + (40*100 + 50) * 1 = 4050

Que - 4. For a C program accessing X[i][j][k], the following intermediate code is generated by a compiler. Assume that the size of an integer is 32 bits and the size of a character is 8 bits. (GATE-CS-2014)
t0 = i * 1024
t1= j * 32
t2 = k * 4
t3 =t1 + t0
t4 = t3 + t2
t5 = X[t4]
Which one of the following statement about the source code of C program is correct?
(A) X is declared as “int X”
(B) X is declared as “int X”
(C) X is declared as “char X”
(D) X is declared as “char X”
Solution: For a three dimensional array X, we have 10 two dimensional matrices of size *. Therefore, for a 3 D array X[M][N][O], the address of X[i][j][k] can be calculated as:
BA + (i*N*O+j*O+k)*SIZE
Given different expressions, the final value of t5 can be calculated as:
t5 = X[t4] = X[t3+t2] = X[t1+t0+t2] = X[i*1024+j*32+k*4]
(i*N*O+j*O+k)SIZE = i*1024+j*32+k*4 = (i*256+j*8+k)4
Comparing the values of i, j and SIZE, we get
SIZE = 4, N*O = 256 and O = 8, hence, N = 32
As size is 4, array will be integer. The option which matches value of N and O and array as integer is (A).
Type 3. Accessing array elements using pointers -
```
• In a single dimensional array a, the element a[i] can be accessed as a[i] or *(a+i) or *(i+a)
• Address of a[i] can be accessed as &a[i] or (a+i) or (i+a)
• In two dimensional array a, the element a[i][j] can be accessed as a[i][j] or *(*(a+i)+j) or *(a[i]+j)
• Address of a[i][j] can be accessed as &a[i][j] or a[i]+j or *(a+i)+j
• In two dimensional array, address of ith row can be accessed as a[i] or *(a+i)

Que - 5. Assume the following C variable declaration

`int *A , B;  `

Of the following expressions

```I. A
II. A
III. B
IV. B```

which will not give compile-time errors if used as left hand sides of assignment statements in a C program (GATE CS 2003)?
(A) I, II, and IV only
(B) II, III, and IV only
(C) II and IV only
(D) IV only

Solution: As given in the question, A is an array of 10 pointers and B is a two dimensional array. Considering this, we take an example as:

```int *A, B;
int C[] ={1, 2, 3, 4, 5};```

As A represents an integer pointer, it can store the address of integer array as: A = C; therefore, I is valid.

As A represents base address of C, A can be modified as: A = *(C +3) = 0; it will change the value of C to 0. Hence, II is also valid.

As B is 2D array, B can be modified as: B = 5; it will change the value of B to 5. Hence, IV is also valid.

As B is 2D array, B represent address of 2nd row which can’t be used at LHS of statement as it is invalid to modify the address. Hence III is invalid.

Attention reader! Don't stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.

This article is contributed by Sonal Tuteja. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

Article Tags :
Practice Tags :