EducationProgramming

What is array in C/C++ ?

An array in C or C++ is defined as the collection of homogeneous or similar items that are stored in consecutively memory locations. In simple words, arrays are used to store similar types of data. These similar types of data are related to the primitive data-types such as long, int, float, char, etc. And not only related to this, you can even define your own data-type object or derived datatype using structure in C/C++ and store them in a continuous fashion way. If you come across programming, then array is a very useful data structure. Even in large competitive programming, arrays are used.

But why Arrays ?

The main reason lies is the execution speed. Arrays are stored in such consecutive memory locations that can easily be iterated and perform the operations. Due to it’s fixed size and position, arrays beat almost other data structure like linked-list or trees. Another reason is due to its simple syntax, which is similar like declaring a variable. When compared to linked-list or trees, you need to write long code, declaring pointers and assigning location references. Without declaring various variables, you can declare an array, store the values in it and access it or modify the elements.

How Arrays work ?

Loading...

As discussed earlier, it is a simple declaration like variable. Checkout this syntax on your IDE, if you don’t know what is an IDE or doesn’t have one, you can go through this guide link: https://www.digitalgyan.org/setting-up-c-environment/

Syntax for array in C/C++:  int arr[5] = {0, 1, 2, 3, 4};

It can also be initialized as :   int arr[5];

And later we can define elements

And also it can be defined as:    int arr[] = {1,2,3,4,5};

                           Here size is taken automatically

 You can check memory location using  & operator. Example:  &arr [ 1 ] = gives address  

 The index is used to find the location of element inside an array. It generally starts with 0, 1, 2, 3, … , size-1.

If you declare array of double or float type, then it consist of element with double or floating values. For example:

       double arr[5] = {0.01, 0.02, 0.03, 0.04, 0.05};

Types of Arrays :  Array can be classified into the Dimensions. Array can be single dimension or linear array. Double Dimensional Array or 2D array, Three-Dimensional Array or 3D Array and Multi-Dimensional Arrays. For example,

1D Array :   [ 1, 2, 3, 4, 5 ]

2D Array :    Example:  int arr[3][3] =

[

[ 1, 2, 3, 4 ],   

            [ 5, 6, 7, 8 ],        

            [ 9, 10, 11, 12 ],

            [ 13, 14, 15, 16 ]

]

1234
5678
9101112
13141516

3D Array :  The elements are stored in a 3D- Cubical like Structure, the syntax is

         int arr[3][3][3];  // for 3-D array

Disadvantages of using arrays

Arrays are declared in fixed size, so either elements cannot be added more than it’s defined size and also large array size with few elements is the waste of memory. It is not dynamic in nature.

The time take for iteration becomes large as the size of array is increased.

Loading...

Important operations like Insertion and Deletion takes more time as it involves the searching and moving all the elements, hence Linked-List is preferred for such case.

Show More

Leave a Reply

Back to top button