Arrays are fundamental data structures in programming, defined as collections of similar data items stored at contiguous memory locations. They offer efficient means of organizing and accessing data, where each element can be accessed using its index number.
Imagine you have grades for 5 classes. Instead of managing five separate containers, think of an array as a single box with five compartments, each designated for a grade. To locate a particular grade, you simply refer to its assigned code (index always starts from (0) zero) – no exhaustive searching required! Arrays streamline organization and access in computer programs, making tasks significantly more manageable and efficient.
Properties of Array
- Consistent Data Type: All elements in an array are of the same data type and size, typically 4 bytes.
- Contiguous Memory Storage: Array elements are stored in consecutive memory locations, with the first element at the lowest memory address.
- Random Access: You can access any element in the array directly by calculating its address using the base address and the size of each element. This enables efficient and direct retrieval of array elements.
Representation of an array
Arrays can be represented differently in various programming languages. Let's look at how we declare an array in C language.
As per the above illustration, there are some of the following important points
- Index number starts with always 0 (Zero).
- The array's length is 10, which means we can store 10 elements. If we specify the array length as 5, then we can store 5 elements.
- Each element in the array can be accessed via its index number.
Why arrays are used?
- Sorting and searching for a value in an array is simpler.
- Arrays excel at handling multiple values swiftly and conveniently.
- Arrays streamline data storage in programming by enabling multiple values to be stored in a single variable. This simplifies code, reducing the need for numerous variable declarations and making it easier to manage and organize data.
Array - Insertion Operation
Insertion operation refers to the action of adding one or more elements into an array. This operation offers flexibility as elements can be inserted at various positions within the array, including the beginning, end, or any specific index according to specific requirements. For example, if we want to add a new element into our array, we can choose where it goes: maybe at the start, the end, or somewhere in between. It all depends on what we need or where we want to add.
Code Example
#include<iostream>
using namespace std;
void insertAnElement(int arr[], int size, int data, int index) {
int i;
for(i = size - 2; i >= index; i--) {
arr[i + 1] = arr[i];
}
arr[index] = data;
}
int main() {
int size;
cout << "Enter size of array :" << endl;
cin >> size;
int arr[size];
cout << "Enter array elements :" << endl;
for(int i = 0; i < size - 1; i++) {
cin >> arr[i];
}
int data;
cout << "Enter element to insert :" << endl;
cin >> data;
int index;
cout << "Enter index to insert :" << endl;
cin >> index;
insertAnElement(arr, size, data, index);
cout << "Printing array elements :" << endl;
for(int i = 0; i < size; i++) {
cout << arr[i] << " ";
}
return 0;
}
Output
Array - Deletion Operation
Array deletion is the process of removing an item from a particular spot in a list. It involves shifting all items after that spot to the left to fill the gap. This operation ensures that the list remains orderly and efficient. It's like taking a book out of a shelf and rearranging the books so there are no empty spaces. Array deletion is essential for managing data effectively in programming and helps maintain the integrity of the list. By understanding array deletion, programmers can build more efficient and organized software solutions.
Code Example
#include<iostream>
using namespace std;
void deleteAnElement(int arr[], int size, int index) {
for(int i = index; i < size - 1; i++) {
arr[i] = arr[i + 1];
}
}
int main() {
int size;
cout << "Enter size of array :" << endl;
cin >> size;
int arr[size];
cout << "Enter array elements :" << endl;
for(int i = 0; i < size; i++) {
cin >> arr[i];
}
int index;
cout << "Enter index number to delete :" << endl;
cin >> index;
deleteAnElement(arr, size, index);
cout << "Printing array after deletion :" << endl;
for(int i = 0; i < size - 1; i++) {
cout << arr[i] << " ";
}
return 0;
}
Output
Conclusion
Arrays are like organized boxes where we can store similar things in a neat row. Each item in the box has its number, starting from zero. This makes it super easy to find what we need without searching all over the place. Arrays are great for handling lots of stuff quickly and keeping things tidy in computer programs. We can add new things wherever we want in the array, just like putting books on a shelf or removing them neatly without leaving empty spaces. Arrays make it easier for computers to work with lots of information, helping us build better programs faster.