Defination:
Array is a collection of homogeneous data elements with following properties- All the data elements are stored contiguous memory location
- Data elements are accessed with index number.
- 1-D Array (Linear Array)
- 2-D Array (Matrix)
- N-D Array (Multidimensional Array)
Linear Array
  In this type of array there will be single index all the element of array will be stored in contiguous memory location. NAME of array is actually a pointer which points the 1st element of Array.
Declaration
In C:    data_type arrName[array_size]
       in c starting index of array is always 0
General Way:     declare arrName[LB, UB] as DataType
                           *LB - Lower Bound
                           *UB - Upper Bound
Access Element
            In C:     arrName[ELEMENT_INDEX]
            where ELEMENT_INDEX must be from 0 to array_size -1 
Total Size of Array
           Size of array can be found by following formulas
            Array_Size = UB - LB + 1
Memory allocation of Linear Array
Suppose we have n elements in an array which are allocated memory from starting location 100, and each element of the array is having size of 2 bytes. so that 1st element of the array will be stored at index memory location 100 and next element will be stored at memory location 102, this will be continued for nth element.
 Calculation of memory location for mth element of array
To fetch the element of index m from the array we use following formula
Add_Of_Element_m = Base_Address+size * (m - LB)
where Base_Address: Address of starting element
size: Size of element
m: Index of the element whose address is to be found.
LB: Lower Bound of array
Example
Suppose we have an array having 10 elements, Lower Bound of array is 0, Starting address of Array is 1200, element size is 4 bytes, than what will be the address of element of index 5.
Soln:
we are given:
Base_Address = 1200, size=4, LB=0, m = 5
Add_of_Element_m = 1200+4*(5-0)
Add_of_Element_m = 1220 --> Ans.
To fetch the element of index m from the array we use following formula
Add_Of_Element_m = Base_Address+size * (m - LB)
where Base_Address: Address of starting element
size: Size of element
m: Index of the element whose address is to be found.
LB: Lower Bound of array
Example
Suppose we have an array having 10 elements, Lower Bound of array is 0, Starting address of Array is 1200, element size is 4 bytes, than what will be the address of element of index 5.
Soln:
we are given:
Base_Address = 1200, size=4, LB=0, m = 5
Add_of_Element_m = 1200+4*(5-0)
Add_of_Element_m = 1220 --> Ans.
2-D Array
These are also known as Matrix. These are collection of homogeneous data elements in rows and columns format. The name of 2 D array is actually a pointer which points the element at 1st Row and 1st Index.
Declaration
General Way: declare arr[LBR,UBR][LBC,UBC] as DataType
In C: data_type arr_Name[NO_OF_ROWS][NO_COLS];
                                  *LBR: Lower Bound for Rows;   UBR: Upper Bound for Rows
                                  *LBC: Lower Bound for Columns;  UBC: Upper Bound for Columns
                                  *NO_OF_ROWS: No of Rows
                                  *NO_OF_COLS: No of Columns
Accessing Element
In C:      arr_Name[Row_Index][Col_Index]
             where  Row_Index is index of row 0<= Row_Index< NO_OF_ROWS
                        Col_Index is index of columns 0<=Col_Index< NO_OF_COLS
Total size of Matrix
             Total Size of Matrix is given by below formula:
NO_OF_ROWS = UBR - LBR +1
NO_OF_COLS = UBC-LBC+1
NO_OF_ROWS = UBR - LBR +1
NO_OF_COLS = UBC-LBC+1
             SIZE_OF_MATRIX = NO_OF_ROWS * NO_OF_COLS
Memory Allocation For Matrix
There are two ways in which matrix can be allocated memory these are:
1. Row Major
2. Column Major
There are two ways in which matrix can be allocated memory these are:
1. Row Major
2. Column Major
- Row Major: In this scenario matrix is stored in memory Row by Row. For Eg: Suppose there is a matrix of called Mat[3][4]. Starting location(Base address) of matrix is 2000, in this case LBR=0,UBR=2; LBC=0,LBR=3. For Row Major memory allocation (Row 1)Mat[0][0],Mat[0][1],Mat[0][2],Mat[0][3],(Row 2) Mat[1][0],Mat[1][1]... will be the sequence.
- Memory Location for i,j element of Matrix in Row Major Allocation
                    j: column index of element
                    size: size of datatype 
                    m: no. of rows
                    n: no. of cols
                    LBC: Lower bound of columns
                    LBR: Lower bound of Row
                    BA: Base Address
                    LOC_OF_IJ_ELEMENT = BA + size * [(i-LBR)*n + (j-LBC)]
- Column Major: In this scenario matrix is stored in memory column by column. For Eg: Suppose there is a matrix of called Mat[3][4]. Starting location(Base address) of matrix is 2000, in this case LBR=0,UBR=2; LBC=0,LBR=3. For Column Major memory allocation (Column1)Mat[0][0],Mat[1][0],Mat[2][0],(Column2) Mat[0][1],Mat[1][1]... will be the sequence.
- Memory Location for i,j indexed element of Matrix in Column allocation
                    i: row index of element
                    j: column index of element
                    size: size of datatype 
                    m: no. of rows
                    n: no. of cols
                    LBC: Lower bound of columns
                    LBR: Lower bound of Row
                    BA: Base Address
                    LOC_OF_IJ_ELEMENT = BA + size * [(i-LBR) + (j-LBC)*m]

 
 
No comments:
Post a Comment