This article covers the topic of array in data structure, which are defined as a collection of data items of the same type stored in contiguous memory locations. Arrays are a straightforward data structure that allows for random access of data elements using their index number.
In C programming, array in data structure are considered derived data types that can store primitive types of data such as int, char, double, float, and more. For instance, if one wanted to store the marks of a student in six subjects, it would not be necessary to define a separate variable for each subject’s marks. Instead, an array can be defined to store the marks for each subject in contiguous memory locations.
Properties of array
The following are some properties of arrays:
- All elements in an array are of the same data type and size, typically 4 bytes.
- The array elements are stored in contiguous memory locations, with the first element occupying the smallest memory location.
- Array elements can be accessed randomly because we can calculate the memory address of each element using the base address and the size of the data element.
Representation of an array
In C programming language, an array can be declared using the following syntax:
data_type represents the type of data the array will store (e.g., int, char, float, etc.).
array_name is the name given to the array, which can be used to refer to it in the program.
array_size denotes the number of elements in the array, which must be a positive integer.
For example, to declare an array named
myArray of size 5 that stores integers, the following syntax can be used:
This declares an array named
myArray that can store 5 integers. Each element in the array is represented by an index starting from 0 to 4. The size of the array is fixed and cannot be changed during runtime.
Why are arrays required?
Arrays are useful for several reasons:
- Sorting and searching for values in an array is easier and more efficient compared to searching for values in other data structures.
- Arrays are an excellent choice when it comes to processing multiple values quickly and efficiently. They provide a simple and fast way of storing and retrieving data.
- Arrays are ideal for storing multiple values in a single variable. In computer programming, it is often necessary to store a large amount of data of the same type. Defining multiple variables to store this data would be tedious and could make the code difficult to read and maintain. Instead, an array can be defined to store all the elements in a single data structure with a single name. This makes it easier to manage and access the data, especially when dealing with large amounts of it.
Memory allocation of an array
As mentioned earlier, array elements are stored in contiguous memory locations, and the array’s name represents the base address or the address of the first element in the memory. Each element of the array is accessed using its index.
We can define the indexing of an array in several ways, such as:
- Zero-based indexing: In this method, the first element of the array is represented by the index 0. For example, in an array named
arr, the first element would be
- One-based indexing: In this method, the first element of the array is represented by the index 1. For example, in an array named
arr, the first element would be
- n-based indexing: In this method, the first element of the array can reside at any random index number, such as 2 or 5. The programmer can choose the starting index for the array. However, this method is not commonly used in most programming languages.
How to access an element from the array?
To access a specific element in an array, the following information is required:
- Base Address: The memory location of the first element in the array. This is usually represented by the name of the array.
- Size of an Element: The size of each element in the array. This is determined by the data type of the array, such as
float, etc. The size is usually represented in bytes.
- Indexing Method: The method used to index the array, such as zero-based indexing or one-based indexing.
To calculate the memory location of a specific element in the array, the following formula can be used:
Address of ith element = Base Address + (i * Size of an Element)
i represents the index of the element that we want to access.
For example, if we have an integer array named
myArray with 5 elements and we want to access the third element (i.e.,
myArray), and the base address of the array is
1000, and the size of an integer is
4 bytes, we can use the following formula:
Address of 3rd element = 1000 + (2 * 4) = 1008
Therefore, the memory location of the third element in the
myArray array is
Yes, you are correct. The basic operations supported in the array are as follows:
- Traversal: Traversing an array means visiting every element of the array once. This operation is used to print or access all the elements of the array.
- Insertion: Insertion is the process of adding a new element to the array at a specific index. This operation requires shifting the elements after the insertion point to make space for the new element.
- Deletion: Deletion is the process of removing an element from the array at a specific index. This operation requires shifting the elements after the deletion point to fill the gap created by the removed element.
- Search: Searching an array means looking for a particular element in the array. This operation can be performed using the index of the element or the value of the element.
- Update: Updating an element means changing the value of an element at a particular index to a new value.
These operations are the basic building blocks for many advanced operations that can be performed on arrays, such as sorting and merging.
How to define constant in java?
What are the basic SQL Command ?