What is an Array?
What is an Array?
a data structure used to store a collection of items in computer memory. Array implementation differs based on the programming language.
Array Details
Arrays are helpful as they provide programmers with a data structure that can store multiple elements of the same type together. An index position can uniquely identify each element or individual item in an array. The starting value of the index is usually 0, and the difference between indices is the offset.
A number array is a collection of numbers in a contiguous memory location. The size of an array can be static or dynamic, depending on the programming language used. In the C language, array sizes remain static at the time of declaration. The size of the array is static as there is no guarantee that the next memory location is free for utilization. Shrinking of the array is not possible either as the computer statically allocates memory to the array.
In zero-based indexing, the first element of the array has a subscript of 0, in one-based indexing, the first element of the array has a subscript of 1, and in n-based indexing, the base index of the array can be any data type. Programmers can select negative values or characters as the base index value.
Real-World Example of Arrays
Arrays as data structures find uses in many real-world software applications. A software application that tracks and stores contact numbers can use arrays to arrange all contact numbers in a contiguous memory location. This makes accessing phone numbers easier under a single name based on the index positions.
Arrays also find their application in speech processing, where the program stores each speech signal as an array of signal amplitudes. We use two-dimensional arrays extensively in image processing and recognition software.
Significance of Array
Arrays allow programmers to refer to multiple data items of the same type using a single name, enabling programmers to locate any random element in the array using the index number. In the C programming language, we represent arrays using square brackets ([]).
To access any index element, we can specify the index position inside the square braces. For example, accessing the 3rd index element of an array named 'sample' is represented as sample[2] for a zero-indexed array. Arrays provide performance and memory-related benefits as memory allocation is in fixed and contiguous memory locations. This eliminates memory overflow or the shortage of memory in arrays.
Advanced data structures like queues, stacks, linked lists, trees, and graphs follow array principles. Operating system job scheduling uses arrays to schedule computing tasks on a First In First Out (FIFO) basis. Arrays with multiple rows and columns represent two-dimensional matrices.
Types of Array
Arrays are classified based on the number of dimensions and the base indexing of the array. A one-dimensional array is a collection of contiguous memory locations in a linear data structure.
In a two-dimensional array, elements represent a tabular format with rows and columns. An X * Y two-dimensional array has X rows and Y columns. Elements in a two-dimensional array use two subscripts where the first index is the row and the second is the column.
Two-dimensional arrays can be further classified based on their memory representation. In a row-major representation, the storage of array elements takes place row-wise. In a column-major representation, the storage of array elements takes place column-wise.