Arrays
Arrays
1st 2nd 3rd 4th 5th 6th 7th 8th 9th 10th
element element element element element element element element element element
marks[0] marks[1] marks[2] marks[3] marks[4] marks[5] marks[6] marks[7] marks[8] marks[9]
To access all the elements of the array, you must use a loop. That is, we can access all
the elements of the array by varying the value of the subscript into the array. But note
that the subscript must be an integral value or an expression that evaluates to an
integral value.
CALCULATING THE ADDRESS OF ARRAY int i, marks[10];
for(i=0;i<10;i++)
ELEMENTS marks[i] = -1;
99 67 78 56 88 90 34 85
Marks[0] marks[1] marks[2] marks[3] marks[4] marks[5] marks[6] marks[7]
marks[7] 1000 1002 1004 1006 1008 1010 1012 1014
99 67 78 56 88 90 34 85
Marks[0] marks[1] marks[2] marks[3] marks[4] marks[5] marks[6 marks[7]]
for(i=0;i<n;i++)
{
printf(“\n Arr[%d] = “, i);
scanf(“%d”,&num[i]);
}
printf(“\n The array elements are “);
for(i=0;i<n;i++)
printf(“Arr[%d] = %d\t”, i, arr[i]);
return 0;
}
Algorithm to insert a new element to the end of the array.
Calling INSERT (Data, 6, 3, 100) will lead to the following processing in the array
45 23 34 12 56 20 20 45 23 34 12 12 56 20
Data[0] Data[1] Data[2] Data[3] Data[4] Data[5] Data[6] Data[0] Data[1] Data[2] Data[3] Data[4] Data[5] Data[6]
45 23 34 12 56 56 20 45 23 34 100 12 56 20
Data[0] Data[1] Data[2] Data[3] Data[4] Data[5] Data[6] Data[0] Data[1] Data[2] Data[3] Data[4] Data[5] Data[6]
Algorithm to delete an element from the end of the array
20
45 23 34 12 56 20 23 12 56 56
45
Data[0] Data[1] Data[2] Data[3] Data[4] Data[5]
Data[0] Data[1] Data[2] Data[3] Data[4] Data[5]
20
23 12 56 20
Calling DELETE (Data, 6, 2) will lead to the following processing in the array 45
Data[0] Data[1] Data[2] Data[3] Data[4] Data[5]
20
23 12 12 56 23 12 56 20
45 45
Data[0] Data[1] Data[2] Data[3] Data[4] Data[5]
Data[0] Data[1] Data[2] Data[3] Data[4]
LINEAR_SEARCH(A, N, VAL, POS)
BINARY SEARCH
BEG = lower_bound and END = upper_bound
MID = (BEG + END) / 2
If VAL < A[MID], then VAL will be present in the left segment of the array. So,
the value of END will be changed as, END = MID – 1
If VAL > A[MID], then VAL will be present in the right segment of the array. So,
the value of BEG will be changed as, BEG = MID + 1
BINARY_SEARCH(A, lower_bound, upper_bound, VAL, POS)
.
There are two ways of storing a 2-D array can be stored in memory. The first way is row major
order and the second is column major order.
In the row major order the elements of the first row are stored before the elements of the second
and third row. That is, the elements of the array are stored row by row where n elements of the
first row will occupy the first nth locations.
(0,0) (0, 1) (0,2) (0,3) (1,0) (1,1) (1,2) (1,3) (2,0) (2,1) (2,2) (2,3)
However, when we store the elements in a column major order, the elements of the first column
are stored before the elements of the second and third column. That is, the elements of the
array are stored column by column where n elements of the first column will occupy the first nth
locations.
(0,0) (1,0) (2,0) (3,0) (0,1) (1,1) (2,1 (3,1) (0,2) (1,2) (2,2) (3,2)
Address(A[I][J] = Base_Address + w{M ( J - 1) + (I - 1)}, if the array elements are stored in column
major order.
And, Address(A[I][J] = Base_Address + w{N ( I - 1) + (J - 1)}, if the array elements are stored in row
major order.
Where, w is the number of words stored per memory location
m, is the number of columns
n, is the number of rows
I and J are the subscripts of the array element
A two dimensional array is initialized in the same was as a single dimensional array is initialized. For example,
int marks[2][3]={90, 87, 78, 68, 62, 71};
int marks[2][3]={{90,87,78},{68, 62, 71}};
There are three ways of passing parts of the two dimensional array to a function. First, we can pass
individual elements of the array. This is exactly same as we passed element of a one dimensional
array.
main()
Passing a row {
int arr[2][3]= ( {1, 2, 3}, {4, 5, 6} };
func(arr[1]);
}
void func(int arr[])
{
int i;
for(i=0;i<5;i++)
printf("%d", arr[i] * 10);
}
Passing the entire 2D array
To pass a two dimensional array to a function, we use the array name as the actual parameter.
(The same we did in case of a 1D array). However, the parameter in the called function must
indicate that the array has two dimensions.
A multi dimensional array is an array of arrays.
Like we have one index in a single dimensional array, two indices in a two dimensional
array, in the same way we have n indices in a n-dimensional array or multi
dimensional array.
Conversely, an n dimensional array is specified using n indices.
An n dimensional m1 x m2 x m3 x ….. mn array is a collection m1*m2*m3* ….. *mn
elements.
In a multi dimensional array, a particular element is specified by using n subscripts as
A[I1][I2][I3]…[In], where,
I1<=M1 I2<=M2 I3 <= M3 ……… In <= Mn
#include<stdio.h>
int main()
{ int array1[3][3][3], i, j, k;
printf(“\n Enter the elements of the matrix”);
printf(“\n ******************************”);
for(i=0;i<2;i++)
{ for(j=0;j<2;j++)
{
for(k=0;k<2;k++)
{
printf(“\n array[%d][ %d][ %d] = ”, i, j, k);
scanf(“%d”, &array1[i][j][k]);
}
}
}
printf(“\n The matrix is : “);
printf(“\n *********************************”)l
for(i=0;i<2;i++)
{ printf(“\n\n”);
for(j=0;j<2;j++)
{
printf(“\n”);
for(k=0;k<2;k++)
printf(“\t array[%d][ %d][ %d] = %d”, i, j, k, array1[i][j][k]);
}
}
}
Sparse matrix is a matrix that has many elements with a value zero.
In order to efficiently utilize the memory, specialized algorithms and data
structures that take advantage of the sparse structure of the matrix should be
used. Otherwise, execution will slow down and the matrix will consume large
amounts of memory.
There are two types of sparse matrices. In the first type of sparse matrix, all
elements above the main diagonal have a value zero. This type of sparse matrix is
also called a (lower) triagonal matrix. In a lower triangular matrix, Ai,j = 0 where
i<j.
An nXn lower triangular matrix A has one non zero element in the first row, two
non zero element 1in the second row and likewise, n non zero elements in the nth
row. 5 3
2 7 -1
3 1 4 2
-9 2 -8 1 7
1 2 3 4 5
3 6 7 8
-1 9 1
9 3
7
In the second variant of a sparse matrix, elements with a non-zero value can appear
only on the diagonal or immediately above or below the diagonal. This type of matrix
is also called a tridiagonal matrix.
In a tridiagonal matrix, Ai,j = 0 where | i – j| > 1. Therefore, if elements are present
on
the main diagonal the, it contains non-zero elements for i=j. In all there will be n
elements
diagonal below the main diagonal, it contains non zero elements for i=j+1. In all there
will be n-1 elements
diagonal above the main
4 diagonal,
1 it contains non zero elements for i=j-1. In all there
will be n-1 elements 5 1
9
2
3 1
4 2 2
5 1 9
6 7