Array and two-dimensional array, Data Structure & Algorithms

Q. Describe the term array.  How do we represent two-dimensional arrays in memory?  Explain how we calculate the address of an element in a two dimensional array.                                                

Ans:

An array is a systematic arrangement of objects generally in rows and columns

Each memory location is defined by an array element. An array element is similar to one variable except it is identified by an index value as a substitute of a name. An index value is a number used to recognize an array element.

Declaration of a two dimensional array- A two dimensional array is declared similarly as we declare a one-dimensional array except that we specify the number of elements in both dimensions. Such as,

int grades[3][4];

The first bracket ([3]) tells the compiler that we are declaring 3 number of pointers, each of them pointing to an array. Here we are not talking about a pointer variable or pointer array. Instead of that, we are saying that each element of the first dimension of a two dimensional array reference a corresponding second dimension. In the above example, all the arrays pointed to by the first index are of the same size. The second index may be of variable size. For example, the earlier statement declares a two- dimensional  array  where  there  are  3  elements  in  the  first  dimension  and  4 elements in the second dimension array.

Two-dimensional array can be represented in memory by following two ways:

1.  Row major representation: To attain b this linear representation, the first row of the array is stored in the first memory locations reserved for the array, then the second row and this process continues.

2.  Column major representation: In this elements of the column are stored next to one another.

In row major representation, the address is always calculated in a two dimensional array as per the formula written below. The address of a[i][j]=base(a)+(i*m+ j)*size in which base(a) is the address of a[0][0], m is second dimension of array a and size represent size of the data type.

Posted Date: 7/10/2012 3:37:49 AM | Location : United States







Related Discussions:- Array and two-dimensional array, Assignment Help, Ask Question on Array and two-dimensional array, Get Answer, Expert's Help, Array and two-dimensional array Discussions

Write discussion on Array and two-dimensional array
Your posts are moderated
Related Questions

Example: Insertion of a key 33 into a B-Tree (w/split) Step 1: Search first node for key closet to 33. Key 30 was determined. Step 2: Node pointed through key 30, is se

Q. Explain the complexity of an algorithm?  What are the worst case analysis and best case analysis explain with an example.

RGB Model The RGB model is based on the assumption that any desired shade of colour can be obtained by mixing the correct amounts of red, green, and blue light. The exact hues

Define a sparse metrics. A matrix in which number of zero entries are much higher than the number of non zero entries is known as sparse matrix. The natural method of showing m

Data array A has data series from 1,000,000 to 1 with step size 1, which is in perfect decreasing order. Data array B has data series from 1 to 1,000,000, which is in random order.

1. develop an algorithm which reads two decimal numbers x and y and determines and prints out wether x>y or y>x. the input values, x and y, are whole number > or equal to 0, which

multilist representation of graph

Worst Case: For running time, Worst case running time is an upper bound with any input. This guarantees that, irrespective of the type of input, the algorithm will not take any lo

Unlike a binary-tree, each node of a B-tree may have a number of keys and children. The keys are stored or saved in non-decreasing order. Each key has an related child that is the