# Practice questions on Arrays in C++

Array is a data structure that store data in the contagious memory location.

Declaring arrays

Declaring arrays is done by the following syntax :
int 1D[] - for 1-D array
int 2D[][] - for 2-D array

If you initialize an array with lesser number of elements, rest are initialized with 0.

Memory address of elements of the array

1-D array : address[i] = baseAddress + i*size
2-D array (row major) : address[i][j] = baseAddress + (i*n + j) * size

Now, let’s see some practice problem

Predict the output of the following code snippet

int arr = {6, 9};
for(int i = 0; i<5; i++)
cout<<arr[i]<<" ";

## Output

6 9 0 0 0

The array is initialized with two values and the rest of the values are initialized as 0 which is reflected in the output.

int arr[] = {1, 2, 3, 4, 5, 6, 7, 8, 9};
cout<<arr;

## Output

6

Find the address of the given element of the integer array. If base address is 1420.

1D array : arr
address = 1420 + 43*2 = 1506
2D array of size arr : arr, stored as row major
address = 1420 + (5*10 + 4)*2 = 1420 + (54)*2 = 1528.