Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 12

Address Calculation of Array Element - Row Major Ordering

 Address can be calculated as Row major order:-


 Loc (A [I][J]) = Base (BA) + W [N x I + J ]

 W denotes the size, i.e; number of bytes per data element of the
array A,
 M is total numbers of rows, and
N is total number of columns in the array.
Address Calculation of Array Element - Column Major
Ordering
 Address can be calculated as Column major order:-
Loc (A[I][J] ) = Base (BA) + W [M x J + I ]
Where
 W denotes the size, i.e; number of bytes per data element of the
array A,
 M is total numbers of rows, and
N is total number of columns in the array
Row major order
When lower bound is given.
 Address can be calculated as Row major order:-

 Address of A[ I ][ J ]th element = BA + [ N * ( I - LBR) +


( J - LBC) ] * W

 Where
 BA = Base address
 W = Number of bytes occupied by each element
 N = Number of columns
Column major order

 Address can be calculated as Column major order:-


 Address of A[ I ][ J ]th element = BA + [ ( I - LBR) + M *
( J - LBC) ] * N
 Where BA = Base address
N = Number of bytes occupied by each element
M = Number of rows
Example

 Suppose element of array A[4][5] occupies 4 bytes, and the address


of the 1st element is 49. Find the address of the element A(4,3)
when the storage is row major and the lower bound for the compiler
is zero(0).

 = BA + [n * (i - LBR) + (j - LBC)] * w
 = 49 + [5 * (4 – 0) + (3 - 0)] * 4
 = 49 + [23] * 4
 = 49 + 92
 = 141
Exercises
 1. Assume that each element of an array a stored in row major order occupies
4 units of storage.
 If a is declared as by each of the following and the address of the first
element of a is 100, find the address of the element array element:-

 (i) int a[10][5] address of a[2][1]


 (ii) int a[5][20] address of a[5][10]
OPERATIONS ON ARRAYS
The operations that can be performed on arrays are:
Traversal
Search
Insertion
Deletion
Sorting
Reversing
Merging
What is a Function?

A Function is a routine, or set of


instructions that performs a specific
task.
 A function can be processed independently and called by another function.
 The function may perform a stand-alone task, such as initializing a set of variables, or it may accept values
(arguments) from the calling instruction, process them and pass the results back to calling program.
Functions
 A functionis a group of statements that is executed when it is called
from some point of the program.
 The following is its format:
 type name ( parameter1, parameter2, ...) { statements }

where:
 type is the data type specifier of the data returned by the function.
 name is the identifier by which it will be possible to call the
function.
 parameters (as many as needed):
Advantages of using Functions

 1. The entire program can be divided into simple subtasks and then function
subprograms can be written for each subtask.

 2. The subprograms are easier to write, understand and debug.

 3. A function can be shared by other programs.


Pointers
A pointer is a variable that holds
the memory address of another
variable.
 The storage and retrieval of data using the memory address of a variable is faster than that of using a variable or subscripts.

 A pointer provides a way of accessing a variable without refering to the variable name of the variable directly.
Question And Answer Session
1. When lower bound is given calculate the Row major order.
2. When lower bound is given calculate the column major order.
3. Suppose element of array A[4][5] occupies 4 bytes, and the address
of the 1st element is 49. Find the address of the element A(4,3)
when the storage is row major and the lower bound for the
compiler is zero(0).
4. List and explain the operation that can be perform on arrays.
5. What is a function?
6. Write the advantages of functions
7. What is a pointer?

You might also like