0% found this document useful (0 votes)
3 views

array

Data structure

Uploaded by

sahaniarun542
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

array

Data structure

Uploaded by

sahaniarun542
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

Introduction to Arrays | Types of Arrays and their

Representation
An Array is a Linear data structure which is a collection of data items having
similar data types stored in contiguous memory locations. By knowing the address
of the first item we can easily access all items/elements of an array.

 Array Index: The location of an element in an array has an index,


which identifies the element. Array index starts from 0.
 Array element: Items stored in an array is called an element. The elements
can be accessed via its index.
 Array Length: The length of an array is defined based on the number of
elements an array can store. In the above example, array length is 6 which
means that it can store 6 elements.
 When an array of size and type is declared, the compiler allocates enough
memory to hold all elements of data.
 For example, an array face [10] will have 10 elements with index starting
from 0 to 9 and the memory allocated contiguously will be 20 bytes. The
compiler knows the address of the first byte of the array only. Also, the
address of the first byte is considered as the memory address for the
whole array.

Array Operation

Now that we know the basic idea behind an array, let us now look at the various
operations that can be performed on arrays.

 Traverse − Print all the elements in the array one by one.


 Insertion − Adds an element at the given index.
 Deletion − Deletes an element at the given index.
 Search − Searches an element in the array using the given index or the
value.
 Update − Updates an element at the given index.

The various types of arrays are as follows.

 One dimensional array


 Multi-dimensional array

One-Dimensional Array

A one-dimensional array is also called a single dimensional array where the


elements will be accessed in sequential order. This type of array will be accessed
by the subscript of either a column or row index.

Multi-Dimensional Array
In the two-dimensional array face [3] [4], the first index specifies the number
of rows and the second index specifies the number of columns and
the array can hold 12 elements (3 * 4).

Declaration/Initialization of Arrays

// A sample program for Array Declaration


#include <stdio.h>
int main()
{
int one_dim [10]; # declaration of 1D array
int two_dim [2][2]; #declaration of 2D array
int three_dim [2][3][4] = {
{ {3, 4, 2, 3}, {0, -3, 9, 11}, {23, 12, 23, 2} },
{ {13, 4, 56, 3}, {5, 9, 3, 5}, {3, 1, 4, 9} }
}; #declaration of 3D array. Here the elements are also defined.return 0;
}

Applications of an array:

Apart from being widely used in programming, arrays have additional applications
as well:

 Used in mathematical problems like matrices etc.


 They are used in the implementation of other data structures like linked lists etc.
 Database records are usually implemented as arrays.
 Used in lookup tables by computer.

 It effectively executes memory addressing logic wherein indices act as addresses to


the one-dimensional array of memory.

Sparse Matrix and its representations


A matrix is a two-dimensional data object made of m rows and n columns,
therefore having total m x n values. If most of the elements of the matrix have 0
value, then it is called a sparse matrix.
Why to use Sparse Matrix instead of simple matrix ?
 Storage: There are lesser non-zero elements than zeros and thus lesser
memory can be used to store only those elements.
 Computing time: Computing time can be saved by logically designing a data
structure traversing only non-zero elements..
Example:
00304
00570
00000
02600
Representing a sparse matrix by a 2D array leads to wastage of lots of memory as
zeroes in the matrix are of no use in most of the cases. So, instead of storing
zeroes with non-zero elements, we only store non-zero elements. This means
storing non-zero elements with triples- (Row, Column, value).
Sparse Matrix Representations can be done in many ways following are two
common representations:

1. Array representation
2. Linked list representation
Method 1: Using Arrays:
2D array is used to represent a sparse matrix in which there are three rows named
as
 Row: Index of row, where non-zero element is located
 Column: Index of column, where non-zero element is located
 Value: Value of the non zero element located at index – (row,column)

Method 2: Using Linked Lists


In linked list, each node has four fields. These four fields are defined as:
 Row: Index of row, where non-zero element is located
 Column: Index of column, where non-zero element is located
 Value: Value of the non zero element located at index – (row,column)
 Next node: Address of the next node

You might also like