UNIT2 ARRAY
UNIT2 ARRAY
What is Array?
An array is a group of similar elements or data items of the same type collected
at contiguous memory locations. In simple words, we can say that in computer
programming, arrays are generally used to organize the same type of data.
Array for Integral value:
Representation of an Array:
Arrays always store the same type of values. In the above example:
• int is a type of data value.
• Data items stored in an array are known as elements.
• The location or placing of each element has an index value.
Important: Array can store only the same type of data items. From the below
example we can see how it works:
2 5 8 44 21 11 7 9 3 1
Initialization of an Array:
If an array is described inside a function, the elements will have garbage value.
And in case an array is static or global, its elements will be initialized
automatically to 0.
We can say that we can simply initialize elements of an array at the time of
declaration and for that, we have to use the proper syntax:
Syntax: datatype Array_Name[size] = { value1, value2, value3, ….. valueN };
Types of Arrays:
There are two types of arrays:
• One-Dimensional Arrays
• Multi-Dimensional Arrays
One -Dimensional Arrays
A one-dimensional array is a kind of linear array. It involves single sub-scripting.
The [] (brackets) is used for the subscript of the array and to declare and access
the elements from the array.
Syntax: DataType ArrayName [size];
For example: int a[10];
Multi-Dimensional Arrays
In multi-dimensional arrays, we have two categories:
• Two-Dimensional Arrays
• Three-Dimensional Arrays
1. Two-Dimensional Arrays
An array involving two subscripts [] [] is known as a two-dimensional array.
They are also known as the array of the array. Two-dimensional arrays are
divided into rows and columns and are able to handle the data of the table.
Syntax: DataType ArrayName[row_size][column_size];
For Example: int arr[5][5];
2. Three-Dimensional Arrays
When we require to create two or more tables of the elements to declare the
array elements, then in such a situation we use three-dimensional arrays.
Syntax: DataType ArrayName[size1][size2][size3];
For Example: int a[5][5][5];
Visit and explore, Difference Between One-Dimensional and Two-Dimensional
Array
Advantages of Array
• It is a better version of storing the data of the same size and same type.
• It enables us to collect the number of elements in it.
• Arrays have a safer cache positioning that improves performance.
• Arrays can represent multiple data items of the same type using a single
name.
Disadvantages Of Array:
• In an array, it is essential to identify the number of elements to be stored.
• It is a static structure. It means that in an array, the memory size is fixed.
• When it comes to insertion and deletion, it is a bit difficult because the
elements are stored sequentially and the shifting operation is expensive.