Data Structures-Arrays
Data Structures-Arrays
Array:
Definition:
An array is defined as a collection of elements of similar data type. All the
elements of an array are stored in consecutive memory locations i.e. one
after the other or in a sequence in main memory. All the elements shares
common array name and they are accessed using index numbers.
Eg: Consider an array of Marks of 5 student as shown below
80 65 38 76 55
Marks [0] Marks [1] Marks [2] Marks [3] Marks [4]
Here, Marks is name of the array and [0], [1], [2], [3], [4] are the index
numbers.
Properties of Array:
Types of Array:
1. Single Dimensional Array( 1-D)
2. Multi-Dimensional Array (2-D OR 3-D)
Syntax:
Data_type array_Name [array size] ;
Example:
int a[6];
Here, the compiler will reserve 6 locations (4*6=24 bytes) for the array a.
In each of the location we can store an integer value. The memory allocated
by the compiler is pictorially represented as:
80 65 87 76 55 65
12 32 43 54 65
14 23 0 0 0
Here,We have not mentioned the array size, so array size will be
the toal number of elements specified.so the array size will be set
to 4 automatically.
32 54 53 78
OR
0 1 2
3. Transpose of matrix:
Transpose of a matrix is obtained by changing rows to columns and
columns to rows.
Program to get transpose matrix:
4. Trace of matrix:
The trace of a matrix is the sum of the elements of diagonal in a given
matrix. For example, consider the matrix shown b6666666elow:
Size of matrix is 3 × 3
10 15 20
25 30 35
40 45 50
Trace=10+30+50=90
If we add the diagonal elements 10, 30 and 50, we get 90.So, the trace of
given matrix is 90.
Note: Since diagonal elements exist only for a square matrix, the size of the
matrix should be n x n i.e. size of row and column should be equal. For a
rectangular matrix trace cannot be computed.
SPARSE MATRICES:
Sparse Array:
● A sparse array is an array in which majority of the array elements
are zero.
● If half of the array element or more than half element is zero then it
would be a sparse array.
Dense Array:
● A Dense array is an array in which majority of the array elements
are non-zero.
1 0 3
0 0 4
0 6 0
1 2 0
4 11 5
0 15 0
0 2 0
3 0 5
0 0 1
It’s a sparse array It’s a Dense sparse array It’s a sparse array