Monday, September 3, 2012

Address calculation of 2D array

The home work given by Prof.R.K.Selvakumar on DCA in C++ Subject on 02.09.2012

The answer is :

CALCULATION OF ADDRESS IN 2D ARRAY

Date : 02.09.2012

Definition 2 D Array: A 2D array is an array in which each element is itself an Array.

For instance, an array A[M][N] is an M X N matrix.

Where : M = No. of rows

N = No. of Columns

M X N = No. of elements.

Implementation of 2-D Array : There are two way to store elements of 2-D array in Memory

1. Row Major - Where elements are stored row wise.

2. Column Major. Where elements are stored Column wise.

Finding The Location (address) of an element in 2-D array:

CASE : 1 . When elements are stored row wise:

Case 1.1 When lower bond is not given.

A[M][N] or A[M,N]

Address of A[I][J] or A[I,J] = B+ W[N( I)+J] .

M= Total No of Rows

N= Total No of Columns

I = Expected row

J = Expected Column

W = size of each element in byte.

Case 1.2 When lower bound is given.

A[LrUr][Lc…Uc] or A[Lr : Ur , Lc : Uc]

Address of A[I][J] or A[I,J] = B+ W[N( I - Lr )+(J-Lc )] .

N= Uc Lc + 1 (Total No of Columns )

I = Expected row

J = Expected Column

W = size of each element in byte.

Lr = Lower Bound of row

Lc= Lower Bound of column

Uc = Upper Bound of column

CASE : 2 . When elements are stored column wise:

Case 2.1 When lower bond is not given.

A[M][N] or A[M,N]

Address of A[I][J] or A[I,J] = B+ W[M(J)+I] .

M= Total No of Rows

N= Total No of Columns

I = Expected row

J = Expected Column

W = size of each element in byte.

Case 2.2 When lower bound is given.

A[LrUr][Lc…Uc] or A[Lr : Ur , Lc : Uc]

Address of A[I][J] or A[I,J] = B+ W[( I - Lr )+M(J-Lc )] .

M= Uc Lc + 1 (Total No of row)

I = Expected row

J = Expected Column

W = size of each element in byte.

Lr = Lower Bound of row

Lc = Lower Bound of column

Uc = Upper Bound of column

Basic Arithmetic Operation On 2 D Array.

-Addition

-Subtraction

-Multiplication


Download the pdf file << Here >>

No comments:

Post a Comment