Arrays: Oxford University Press 2010. All Rights Reserved
Arrays: Oxford University Press 2010. All Rights Reserved
ARRAYS
INTRODUCTION
2nd element
marks[1]
3rd element
marks[2]
4th element
marks[3]
5th element
marks[4]
6th element
marks[5]
7th element
marks[6]
8th element
marks[7]
9th element
marks[8]
10th element
marks[9]
67
marks[1] 1002
78
marks[2] 1004
56
marks[3] 1006
88
marks[4] 1008
90
marks[5] 1010
34
marks[6] 1012
85
marks[7] 1014
Initialization of Arrays Arrays are initialized by writing, type array_name[size]={list of values}; int marks[5]={90, 82, 78, 95, 88}; Inputting Values
int i, marks[10]; for(i=0;i<10;i++) scanf(%d, &marks[i]);
Assigning Values
67
marks[1]
78
marks[2]
56
marks[3]
88
marks[4]
90
marks[5]
34
marks[6
85
marks[7]]
Here, lower_bound = 0, upper_bound = 7 Therefore, length = Oxford 7 0 University + 1 = 8 Press 2010. All rights reserved.
}
printf(\n The array elements are ); for(i=0;i<n;i++) printf(Arr[%d] = %d\t, i, arr[i]); return 0; Oxford University Press 2010. All rights reserved.
Calling INSERT (Data, 6, 3, 100) will lead to the following processing in the array 45
Data[0]
23
34
12
56
Data[4]
20
20
45
Data[0]
23
34
12
12
Data[4]
56
20
Data[1]
Data[2] Data[3]
Data[5] Data[6]
Data[1]
Data[2] Data[3]
Data[5] Data[6]
45
Data[0]
23
Data[1]
34
12
56
56
20
45
23
34
100
Data[3]
12
Data[4]
56
20
Data[2] Data[3]
Data[4] Data[5] Data[6] Data[0] Data[1] Data[2] Oxford University Press 2010. All rights reserved.
Data[5] Data[6]
1: [INITIALIZATION] SET I = POS 2: Repeat Steps 3 and 4 while I <= N - 1 3: SET A[I] = A[I + 1] 4: SET I = I + 1 [End of Loop] Step 5: SET N = N - 1 Step 6: EXIT
45
Data[0]
23
Data[1]
34
Data[2]
12
Data[3]
56
Data[4]
20
Data[5]
45
Data[0]
23
Data[1]
12
56
56
Data[4]
20
Data[5]
Data[2] Data[3]
Calling DELETE (Data, 6, 2) will lead to the following processing in the array
45
Data[0]
23
Data[1]
12
56
20
Data[4]
20
Data[5]
Data[2] Data[3]
45
Data[0]
23
Data[1]
12
Data[2]
12
Data[3]
56
20 45 23 12 56 20
Data[4]
Data[4] Data[5] Oxford University Press 2010. All rights reserved. Data[0] Data[1]
Data[2] Data[3]
LINEAR SEARCH
LINEAR_SEARCH(A, N, VAL, POS) Step Step Step Step 1: [INITIALIZE] SET POS = -1 2: [INITIALIZE] SET I = 0 3: Repeat Step 4 while I<N 4: IF A[I] = VAL, then SET POS = I PRINT POS Go to Step 6 [END OF IF] [END OF LOOP] Step 5: PRINT Value Not Present In The Array Step 6: EXIT
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 University changedPress as, BEG =All MID +1 Oxford 2010. rights reserved.
BINARY_SEARCH(A, lower_bound, upper_bound, VAL, POS) Step Step Step Step 1: [INITIALIZE] SET BEG = lower_bound, END = upper_bound, POS = -1 2: Repeat Step 3 and Step 4 while BEG <= END 3: SET MID = (BEG + END)/2 4: IF A[MID] = VAL, then POS = MID PRINT POS Go to Step 6 IF A[MID] > VAL then; SET END = MID - 1 ELSE SET BEG = MID + 1 [END OF IF] [END OF LOOP] Step 5: IF POS = -1, then PRINTF VAL IS NOT PRESENT IN THE ARRAY [END OF IF] Step 6: EXIT
int A[] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10}; and VAL = 9, the algorithm will proceed in the following manner. BEG = 0, END = 10, MID = (0 + 10)/2 = 5 Now, VAL = 9 and A[MID] = A[5] = 5 A[5] is less than VAL, therefore, we will now search for the value in the later half of the array. So, we change the values of BEG and MID. Now, BEG = MID + 1 = 6, END = 10, MID = (6 + 10)/2 =16/2 = 8 Now, VAL = 9 and A[MID] = A[8] = 8 A[8] is less than VAL, therefore, we will now search for the value in the later half of the array. So, again we change the values of BEG and MID. Now, BEG = MID + 1 = 9, END = 10, MID = (9 + 10)/2 = 9 Now VAL = 9 and A[MID] = 9. Oxford University Press 2010. All rights reserved. Now VAL = 9 and A[MID] = 9.
ONE DIMENSIONAL ARRAYS FOR INTER FUNCTION 1D Arrays For Inter Function Communication COMMUNICATION
Passing individual elements Passing entire array
Passing addresses
main() { int arr[5] ={1, 2, 3, 4, 5}; func(&arr[3]); } void func(int *num) { printf("%d", num); }
int marks[3][5]
Col 0 Marks[0][0] Marks[1][0] Marks[2][0] Col 1 Marks[0][1] Marks[1][1] Marks[2][1] Col2 Marks[0][2] Marks[1][2] Marks[2][2] Col 3 Marks[0][3] Marks[1][3] Marks[2][3] Col 4 Marks[0][4] Marks[1][4] Marks[2][4]
(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 Oxford University Press 2010. All rights reserved. I and J are the subscripts of the array element
int marks[2][3]={90, 87, 78, 68, 62, 71}; int marks[2][3]={{90,87,78},{68, 62, 71}};
TWO DIMENSIONAL ARRAYS FOR INTER FUNCTION 2D Array for Inter Function Communication COMMUNICATION
Passing individual elements Passing a row Passing the entire 2D array
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. Passing a row
main() { 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. Oxford University Press 2010. All rights reserved.
SPARSE MATRIX
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 in the second row and likewise, n non zero elements in the nth row.
1 5 2 3 -9 3 7 1 2
-1 4 -8
2 1
In an upper triangular matrix Ai,j = 0 where i>j. An nXn upper triangular matrix A has n non zero element in the first row, n-1 non zero element in
the second row and likewise, 1 non zero elements in the nth row.
1 2 3 3 6 -1 4 7 9 9 5 8 1 3 7
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 diagonal, it contains non zero elements for i=j-1. In all there will be n-1 elements
4 5 1 1 9 2 3 4
1 2 5
2 1 6
9 7