3 Array
3 Array
Syntax of malloc()
Here, ptr is pointer of cast-type. The malloc() function returns a pointer to an area of
memory with size of byte size. If the space is insufficient, allocation fails and returns
NULL pointer.
C free()
Dynamically allocated memory created with either calloc() or malloc() doesn't get freed
on its own. You must explicitly use free() to release the space.
syntax of free()
free(ptr);
C realloc()
If the previously allocated memory is insufficient or more than required, you can change
the previously allocated memory size using realloc().
Syntax of realloc()
ptr = realloc(ptr, newsize);
C : int A[100]
Range of Index : Indices change from lower bound LB to and upper bound UB
1 D - Arrays
Indexing formula
B = Base address
w = Storage Size of one element stored in the array (in byte)
i = Subscript of element whose address is to be found
LB = Lower limit / Lower Bound of subscript, if not specified assume 0 (zero)
Given the base address of an array A[1300…..1900] as 1020 and size of each
element is 2 bytes in the memory. Find the address of A[1700].
Solution:
Traversing
Sorting
Searching
Insertion
Deletion
Two dimensional Arrays
Memory Representation
1. Row-major form
2. Column major form
Row Major System:
The address of a location in Row Major System is calculated using the following formula:
Address of A [ I ][ J ] = B + w * [ N * ( I – Lr ) + ( J – Lc ) ]
B = Base address
I = Row subscript of element whose address is to be found
J = Column subscript of element whose address is to be found
w = Storage Size of one element stored in the array (in byte)
Lr = Lower limit of row/start row index of matrix, if not given assume 0 (zero)
Lc = Lower limit of column/start column index of matrix, if not given assume 0 (zero)
M = Number of row of the given matrix
N = Number of column of the given matrix
Usually number of rows and columns of a matrix are given ( like A[20][30] or A[40][60] )
but if it is given as A[Lr- – – – – Ur, Lc- – – – – Uc]. In this case number of rows and
columns are calculated using the following methods:
Solution:
As you see here the number of rows and columns are not given in the
question. So they are calculated as:
Number or rows say M = (Ur – Lr) + 1 = [10 – (- 15)] +1 = 26
Number or columns say N = (Uc – Lc) + 1 = [40 – 15)] +1 = 26
[ans:126]