What are array ? - data structure tutorial


What are array ? - data structure tutorial

The array is a fixed-size sequenced collection of variables having similar data types. The arrays have adjoining memory locations to store values. 

Since arrays give a convenient structure to store and manipulate data, thus it falls under the class of the data structures in C.

The syntax for declaring array are: 


        data_type array_name [array_size];

Element: Every value stored in an array.

Index: every memory location of an element in an array is denoted by a numerical index that is used for recognizing the element. 


For what reason Do You Need Arrays for Building a Specific Data Structure?

At the point when a program works with many variables that hold a comparable form of data, at that point organization and managing difficulty arises. At that time if you are not using arrays, then the number of variables will increase. Using the arrays, the number of variables decreases i.e., you can use a single name for various values. i.e., you can use a single name for multiple values, you need to deal with its index values (starting from 0 to n).

So if the total run of each player is getting stored in separate variables, using arrays you can bring them all into one array having a single name like plrscore[11]; 

What are array ? - data structure tutorial



Array Representation

Arrays can be declared in various forms depending on the languages used. For illustration, let's take a C array declaration.

What are array ? - data structure tutorial

According to the above representation, the following are important points to be remembered.
·       Index starts with 0.
· The length of the array is 10 which means it can store up to 10 values.
·       Each element can be accessed via its index. For instance, we can fetch an element at index 6 as 9.

In C, when an array is initialized with size, at that point it assigns default values to its elements in the following request.
What are array ? - data structure tutorial



Basic Operations

Below are the basic operations which an array supports:
·      Insertion – To add an element at a given index.
·      Deletion – To delete an element at a given index.
·      Search – To search an element at a given index.
·      Update – To update an element at a given index.
·      Traverse – To print all values.


Insertion Operation

Insert operation is used to add at least one data element into an array. In light of the prerequisite, a new element can be added at the start, end, or any given index of the array. 

Here, we see a practical implementation of insertion activity, where we add data elements at the end of the array - 

 

Algorithm

Let us consider an Array be a linear unordered array of MAX elements.

Example

Result
Let LA be the Linear Array (unordered) which has N elements and K is a positive integer such that K<=N. Following is the algorithm where ITEM is inserted into the Kth position of LA –

What are array ? - data structure tutorial

Example

Following is the implementation of the above algorithm –
What are array ? - data structure tutorial


When we compile and execute the above program, it produces the following result −

Output

What are array ? - data structure tutorial



Deletion Operation

Deletion refers to removing a current element from the array and re-arranging all elements in an array.


Algorithm

Consider LA is a linear array with N elements and K is a positive number to such that K<=N. Following is the algorithm to delete an element available at the Kth position of LA.

What are array ? - data structure tutorial


Example

Following is the implementation of the above algorithm –
What are array ? - data structure tutorial


When we compile and execute the above program, it produces the following result −

Output

What are array ? - data structure tutorial


Search Operation


You can perform a search for an array element depending on its index or its value.


Algorithm


Let LA is an array with N elements and K is a positive number such that K<=N. Following is the algorithm to find an element with a value of ITEM using sequential search.
What are array ? - data structure tutorial


Example


Following is the implementation of the above algorithm –


What are array ? - data structure tutorial


When we compile and execute the above program, it produces the following result −

Output

What are array ? - data structure tutorial



Update Operation


Update operation means updating an existing element from the array at a given index.

Algorithm


Consider LA is a linear array with N elements and K is a positive integer such that K<=N. Following is the algorithm to update an element available at the Kth position of LA.
What are array ? - data structure tutorial


Example

Following is the implementation of the above algorithm –
What are array ? - data structure tutorial


When we compile and execute the above program, it produces the following result −

Output

What are array ? - data structure tutorial


Accessing Elements of an array


To get to a random element of an array we need the following data:

1.     Base Address of the array.

2.     Size of an element in bytes.

3.     Which type of indexing, array follows.

Text Box: Byte address of element A[i]  = base address + size * ( i - first index)  Address of any element of a 1D array can be calculated by using the following formula:


Byte address of element A[i]  = base address + size * ( i - first index)  



Example:


What are array ? - data structure tutorial



This was all about What are array ? - data structure tutorial.

Post a Comment (0)
Previous Post Next Post