Arrays

EdPsycho Hub
0

An array is a linear data structure that is a finite set of homogeneous elements stored in continuous memory locations. Arrays have fixed size. 

Example: Say Array A => [H, E, L, L, O]


This type of array can be called a "one-dimensional" array. In these arrays, each element can be accessed by specifying an index. 

Example: 

Index     Element
    0            H
    1            E
    2            L
    3            L
    4            0

Here in this array, A[0] = H, A[1] = E, and so on. We can do several operations on arrays.
  • Accessing an element
  • Modifying an element
  • Iterating through array
  • Inserting an element at the end
  • Inserting an element at the beginning/middle
  • Deleting from the end
  • Deleting from the beginning/middle
  • Linear search
Let's talk about the time complexities for these operations.
             Operation Time Complexity
---------------------------------- ---------------------
Accessing an element - O(1)
Modifying an element - O(1)
Iterating through array - O(n)
Inserting at the end - O(1)
Inserting at beginning/middle - O(n)
Deleting from the end - O(1)
Deleting from beginning/middle - O(n)
Linear search - O(n)
Alright, let's explore these array operations using a programming language. Arrays can be implemented using any programming language. But in this post, I will use C++ language as I posted the introduction to C++ posts. If you want to know about C++ language, Click Me.

*Reading comment lines is essential to understand the code and try these codes yourself.
#include <iostream>
using namespace std;

int main() {
// Declare and initialize a one-dimensional array
int arr[5] = {1, 2, 3, 4, 5};
// Array can only keep 5 items

// Accessing an element
cout << "Element at index 2: " << arr[2] << endl; // Output: 3

// Modifying an element
arr[2] = 10; // Modify the third element to be 10
cout << "Modified element at index 2: " << arr[2] << endl; // Output: 10

// Iterating through the array
cout << "Elements of the array: ";
for (int i = 0; i < 5; i++) {
cout << arr[i] << " ";
}
cout << endl;

// Getting the length of the array
int length = sizeof(arr) / sizeof(arr[0]);
cout << "Length of the array: " << length << endl; // Output: 5

return 0;
}

Two Dimensional Arrays

This is a 2D array. These types of arrays work with rows and columns. Therefore we can use these arrays to implement matrices.

Example: Say Array B => [[1,2,3],[4,5,6],[7,8,9]]

This is what a 2D array looks like.

Here in this array, B[0][0] = 1, B[0][1] = 2 and so on. 

Alright, let's move on to the implementations of the 2D array.
*Reading comment lines is essential to understand the code and try these codes yourself.
#include <iostream>
using namespace std;

int main() {
// Declare and initialize a two-dimensional array (3x3 matrix)
int matrix[3][3] = {
{1, 2, 3},
{4, 5, 6},
{7, 8, 9}
};

// Accessing an element
cout << "Element at row 1, column 2: " << matrix[1][2] << endl; // Output: 6

// Modifying an element
matrix[1][2] = 10; // Modify the element at row 1, column 2 to be 10
cout << "Modified element at row 1, column 2: " << matrix[1][2] << endl; // Output: 10

// Iterating through the two-dimensional array
cout << "Elements of the matrix:" << endl;
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
cout << matrix[i][j] << " ";
}
cout << endl;
}

return 0;
}

Multi-Dimensional Array

Like how we expanded to two dimensions from one, we can continue the same thing here for expanding to N-dimensional arrays.

Example:
int arr[2][3][4] = {
{
{1, 2, 3, 4}, // First row of first matrix
{5, 6, 7, 8}, // Second row of first matrix
{9, 10, 11, 12} // Third row of first matrix
},
{
{13, 14, 15, 16}, // First row of second matrix
{17, 18, 19, 20}, // Second row of second matrix
{21, 22, 23, 24} // Third row of second matrix
}
};

These all types are fixed-size arrays. But most of the time we need dynamic arrays which can increase their sizes at runtime. These dynamic arrays are implemented using these normal arrays. Programming languages have their own implementations for these dynamic arrays. In C++, it is called as "Vectors". In Java, it is "ArrayList". If you want to learn about them, just leave a comment below.

Advantages of Using Arrays

  1. Fast Access: It takes constant time [O(1)] to access elements.
  2. Memory Efficiency: Arrays are efficient in terms of memory usage as it has fixed size.

Disadvantages of Using Arrays

  1. Fixed Size: In many languages, the size of an array cannot be changed after its creation, which can be limiting.
  2. Insertion and Deletion: Inserting or deleting elements (except at the end) can be inefficient, as it may require shifting elements.

Conclusion

In this section we discussed arrays. First, we discussed one-dimensional(1D) arrays and their operations with time complexities. After that, we talked about two-dimensional(2D) arrays and their operations. Finally, we discussed multi-dimensional(ND) arrays. If you have any questions in this section, please contact me, and if you enjoy this article share this with your friends. Thank you for being with me. Have a nice day!

Post a Comment

0Comments

Post a Comment (0)