What is Array in data structure with example?

Array

Arrays are defined as the collection of comparable data objects that are stored in adjacent memory regions.
Arrays are the derived data type that can hold primitive data like int, char, dual, float, etc in the C programming language.
The array is the simplest data structure in which the index number of every data element may be reached arbitrarily.
For example, in six subjects we should not define the distinct variable for the marks in various topics if we want to save the student’s characteristics. Instead, we may create a matrix that can hold marks at contiguous memory sites in each topic.

In 10 distinct topics, each subject mark in a particular subscript of an array[10] determines the marks of the student, i.e. marks[0] indicate the first subject marks, markings[1] indicate marks in the second subject, and so on.

Properties

Each element has the same data type, i.e. int = 4 bytes.
Array items are stored where the initial element is kept at the smallest memory position at the contiguous memory.
Array elements may be accessed by random use since the address of each element of the matrix can be calculated using the provided base address and the data element size.
The syntax of defining an array is like the following in C language, for instance: int arr[10]; char arr[10]; float arr[5]

Need of using Array

Most situations involve the storage of huge numbers of like-minded data, in computer programming. We need to define a big number of variables to store such a quantity of data. The names of all variables during program writing would be exceedingly difficult to remember. It is best to construct a matrix and save all elements in it, rather than name all the variables with a distinct name.
This example shows how arrays may be beneficial for a specific difficulty in creating code. We have a mark of a student in six distinct disciplines in the following example. The task is to determine the student’s average grades. We built two applications to show the necessity of a matrix, one without a matrix and the other with the usage of arrays to store marks.

Program without array:

  1. #include <stdio.h>  
  2. void main ()  
  3. {  
  4.     int marks_1 = 56, marks_2 = 78, marks_3 = 88, marks_4 = 76, marks_5 = 56, marks_6 = 89;   
  5.     float avg = (marks_1 + marks_2 + marks_3 + marks_4 + marks_5 +marks_6) / 6 ;   
  6.     printf(avg);   
  7. }  

Program by using array:

  1. #include <stdio.h>  
  2. void main ()  
  3. {  
  4.     int marks[6] = {56,78,88,76,56,89);  
  5.     int i;    
  6.     float avg;  
  7.     for (i=0; i<6; i++ )   
  8.     {  
  9.         avg = avg + marks[i];   
  10.     }    
  11.     printf(avg);   
  12. }   

Complexity of Array operations

In the following table, time and space complexity are provided for the various array operations.

Time Complexity

AlgorithmAverage CaseWorst Case
AccessO(1)O(1)
SearchO(n)O(n)
InsertionO(n)O(n)
DeletionO(n)O(n)

Space Complexity

In array, space complexity for the Created by the case is O(n).

Memory Allocation of the array

As noted before, all data components of an array are saved in the main memory at adjacent places. The name of the matrix shows the first element’s database address or address. Adequate indexing is provided for each element of the matrix.
The table may be indexed three-way. The table can be defined.

  1. 0 (zero – based indexing) : The first element of the array will be arr[0].
  2. 1 (one – based indexing) : The first element of the array will be arr[1].
  3. n (n – based indexing) : Any random index number can be the first entry in the matrix.

The memory assignment of a matrix set of sizes 5 has been presented in the accompanying graphic. The table follows 0-based indexing. The array’s base address is the 100th byte. This is Arr[0address. ]’s Here, the int size is 4 bytes, so each item is stored in 4 bytes.

Array

Indexing n 0 based, The maximum index number if the size of a matrix is n, an element may have n-1. It would be n, however, if we use indexing based on 1.

Accessing Elements of an array

We need the following information in order to retrieve any random element in an array:
Base Array address. Array address.
Byte size of an element.
The array follows what type of indexing.
A 1D array can be computed by the following formulation for addressing any element: Byte address of element A[i]  = base address + size * ( i – first index)  

Example :

  1. In an array, A[-10 ….. +2 ], Base address (BA) = 999, size of an element = 2 bytes,   
  2. find the location of A[-1].  
  3. L(A[-1]) = 999 + [(-1) – (-10)] x 2  
  4.        = 999 + 18   
  5.        = 1017   

Passing array to the function :

As we said before, the array name indicates the first element in the array’s start address or address. The base address can be used to cross all items of the matrix.
The examples below show how a function may be provided to the matrix.

Example:

  1. #include <stdio.h>  
  2. int summation(int[]);  
  3. void main ()  
  4. {  
  5.     int arr[5] = {0,1,2,3,4};  
  6.     int sum = summation(arr);   
  7.     printf(“%d”,sum);   
  8. }   
  9.   
  10. int summation (int arr[])   
  11. {  
  12.     int sum=0,i;   
  13.     for (i = 0; i<5; i++)   
  14.     {  
  15.         sum = sum + arr[i];   
  16.     }   
  17.     return sum;   
  18. }  

As we said before, the matrix name indicates the first element in the array’s start address or address. The base address can be used to cross all items of the matrix.
The examples below show how a function may be provided to the matrix.