C++

C++ Arrays

C++ Arrays

An array in any programming language is a collection of data items of same type that are stored at contiguous memory locations and elements can be accessed at random using integer indices within the array.  The stored data can be of any type such as primitive data types (int, float, double, char, etc o or derive datatypes such as the structures, pointers etc.). 

We need array to avoid declaring individual variables, such as number0, number1, ..., and number99 and instead club them in a single array variable. All arrays have contiguous memory locations with the lowest address corresponds to the first element and the highest address to the last element.

We can declare C ++ array in C++ by specifying the type of the elements and the number of elements required by it.

type arrayName [ arraySize ];

This is called a 1-D array. The arraySize must be an integer constant greater than zero and type can be any valid C++ data type (generic or derived). For example, following line declares a 20-element array called numRecords of type int

int numRecords[20];

C++ array elements are initialized one at a time or in a single statement as shown below. Please note that number of initializers must not exceed the array capacity.

//Array initialization one at a time
for (i=0;i<6;i++)
{
	numRecords[i] = (i+1);	
}

//Array initialization in one go
int numRecords[6] = {1,2,3,4,5,6};

You can access any array by indexing in the array name with proper index value. This is done by placing the index of the element within square brackets after the name of the array. For example :

int currentRecord = numRecords[2]; 

The above statement will take 2nd element from the array and assign the value to currentRecord variable. 

Few Advantages of an Array in C++ are as follows:

  1. It provides Random access of elements using array index.
  2. The program code print decreases as we use less line of code to creates a single array of multiple elements.
  3. It gives easy access to all the elements.
  4. Let us write a simple code to show all operations on arrays.
  5. Array traversal becomes easy using for loop.
  6. Sorting and searching becomes easy

However like any other data structure, it also has its share of disadvantages. Array is a non-flexible data structure meaning only fixed number of elements can be entered that is decided at the time of declaration. Unlike a LinkedList, an array size cannot be dynamically adjusted. This makes insertion and deletion of  elements costly. If we declare an array of size 20, then the array will contain elements from index 0 to 19. But if we try to access the element at index 10 or more than 10, it will result in Undefined Behaviour.

Multidimensional arrays can be described as "arrays of arrays". For example, a bidimensional array can be imagined as a two-dimensional table made of elements, all of them of a same uniform data type. For e.g.

// 2D array to store elements of 3X3 matrix
int mat[3][3];
To access 2nd element from 2nd row, we can write
x =mat[1][1];   

Arrays can be passed as parameters to function calls by reference. This means that the function can directly access and change the contents of the passed array. While passing a two-dimensional array as a formal parameter, we can omit the size of the first dimension, but must specify the size of second. For example:

void displayArray(int Mat[][3],int numRows, int numCols);

We can call this function by providing a two-dimensional array as follows:

int main()
{
	...
    int arr[4][3];
    ....
    display (arr, 4, 3);
}