What are Arrays?

Hi everyone, inside this article we will see the concept about Array Data Structure.

An array is a data structure that stores a collection of elements, each identified by an index or a key. Elements in an array are typically of the same data type and are stored in contiguous memory locations.

Arrays can be used to store and manipulate large amounts of data efficiently, as elements can be accessed and modified quickly using their index. They are commonly used in computer programming to store lists of items or a sequence of values. Arrays can be of one-dimensional or multi-dimensional.

What are Characteristics of Array?

Some of the main features of arrays as a data structure include:

  1. Contiguity: Elements in an array are stored in contiguous memory locations, which allows for fast access and modification of elements using their index.
  2. Indexing: Elements in an array can be accessed and modified using their index, which is a positive integer value that corresponds to the position of the element in the array.
  3. Size: The size of an array is fixed when it is created and cannot be changed afterwards.
  4. Data type: Elements in an array are typically of the same data type, such as integers, floating-point numbers, or characters.
  5. One-dimensional and Multi-dimensional: Array can be of one-dimensional or multi-dimensional.
  6. Random Access: Elements in an array can be accessed randomly, which means that any element can be accessed directly and efficiently.
  7. Low Memory overhead: Arrays have low memory overhead as compared to Linked List.
  8. Easy to implement: Arrays are simple to implement and can be used in many programming languages.
  9. Advantages over Linked List: Arrays have better cache locality that can make them significantly faster than linked lists for some operations, such as traversing all the elements sequentially.

Why are Arrays required?

Arrays are required in computer programming for several reasons, including:

  1. Efficient storage and manipulation of large amounts of data: Arrays allow for the storage of a large number of elements in contiguous memory locations, which makes it easy to access and manipulate the data.
  2. Random access: Elements in an array can be accessed randomly, which means that any element can be accessed directly and efficiently, this is useful when you need to look for a specific element in a large collection of data.
  3. Simplicity: Arrays are simple to implement and can be used in many programming languages, this make it easy to understand and easy to maintain.
  4. Speed: Arrays have better cache locality that can make them significantly faster than other data structures for some operations, such as traversing all the elements sequentially.
  5. Low memory overhead: Arrays have low memory overhead as compared to Linked List.
  6. Simple Algorithms: Many simple algorithms can be implemented with arrays, such as sorting, searching, and statistical analysis.
  7. Convenient Representation: Arrays are useful in representing many mathematical objects and structures such as matrices, polynomials, graphs, etc.
  8. Used in many applications: Arrays are used in many applications such as video games, simulations, and scientific computations. They can also be used to implement other data structures such as stacks, queues, and hash tables.

Types of Array

There are several types of arrays in data structure, which include:

  1. One-dimensional array: A one-dimensional array is a linear collection of elements, each identified by an index. It is the simplest type of array and stores a linear sequence of elements.
  2. Multi-dimensional array: A multi-dimensional array is an array with more than one dimension. It can be represented as a 2D array or a 3D array, etc. The element of multi-dimensional array can be accessed using multiple indices.
  3. Dynamic array: A dynamic array is a type of array that can change its size during the execution of a program. It is useful when the number of elements in the array is not known in advance or when the array needs to grow or shrink in size.
  4. Sparse array: A sparse array is a data structure used to store large arrays where most of the elements have the same value.
  5. Jagged array: A jagged array, also known as an “array of arrays” is an array where each element of the array is itself an array.
  6. Bit array: A bit array is used to store a sequence of bits or Boolean values.
  7. Circular array: A circular array is similar to a one-dimensional array, but with the added property that the last element is followed by the first element, creating a circle of elements.

About One-dimensional Array

In C++, arrays can be declared by specifying the data type, followed by the array name, and then the size of the array in square brackets. Here is an example of how to declare an array of integers with the name “myArray” and size of 10:

int myArray[10];

You can also initialize an array at the time of declaration by specifying the values within curly braces {}

int myArray[]={1,2,3,4,5};

Here is an example of a program that demonstrates the use of a one-dimensional array in C++:

#include <iostream>
using namespace std;
int main()
{
  int num [5] ;
  cout<<"Enter array elements : \n";
  for(int i = 0; i < 5 ; i++)
  {
    cin >> num[i] ;
  }
}

In this method, The user is asked to enter the array elements of his/her choice during the run-time of the program.

Output


Enter array elements :
1
3
5
7
9

About Multi-dimensional Array

A multi-dimensional array is an array with more than one dimension. It is an array of arrays, where each element of the array is itself an array. Each element of a multi-dimensional array is accessed using multiple indices or subscripts, one for each dimension.

It can be like of Two Dimensional Array (2D), Three Dimensional Array (3D), etc.

For example, a two-dimensional array can be represented as a table with rows and columns, where each cell in the table is an element of the array. In this case, the first index refers to the row number, and the second index refers to the column number.

string letters[2][4];

Array declaration with values initialization,

string letters[2][4] = {
  { "A", "B", "C", "D" },
  { "E", "F", "G", "H" }
};

Here is an example of a program that demonstrates the use of a two-dimensional array in PHP:

#include <iostream>
using namespace std;

int main() {
  string letters[2][4] = {
    { "A", "B", "C", "D" },
    { "E", "F", "G", "H" }
  };
  
  cout << letters[0][2];
  return 0;
}

In this example, we first declare a two-dimensional array called letters.

This program will output:

C

We hope this article helped you to understand Array, Characteristics, Why you use, etc in a very detailed way.

Online Web Tutor invites you to try Skillshike! Learn CakePHP, Laravel, CodeIgniter, Node Js, MySQL, Authentication, RESTful Web Services, etc into a depth level. Master the Coding Skills to Become an Expert in PHP Web Development. So, Search your favourite course and enroll now.

If you liked this article, then please subscribe to our YouTube Channel for PHP & it’s framework, WordPress, Node Js video tutorials. You can also find us on Twitter and Facebook.