English Deutsch Français Italiano Español Português 繁體中文 Bahasa Indonesia Tiếng Việt ภาษาไทย
All categories

Define the term array. How are two-dimensional arrays represented in memory? Emplain how address of an element is calculated in a two dimensional array.

2007-08-10 01:34:26 · 5 answers · asked by Anonymous in Computers & Internet Programming & Design

5 answers

The standard way: if you have an array that is x by y (a[x][y]), it is stored as a straight list of x*y elements. To find a[i][j], you take the "i*y + j"th element. Each time you increment the row (i), you skip y elements in the list to the start of the next row.

Let's say that you do this:
int a[2][4] = { { 1, 2, 3, 4 }, {5, 6, 7, 8 } };

That would be stored like:
12345678

a[1][2] would be 1*4+2 = the 6th element after the start of the list (7).

2007-08-10 02:30:52 · answer #1 · answered by McFate 7 · 1 0

Arrays are objects in java that store multiple variables of the same type.Arrays can hold either primitives or object references but the array itself will be an object on the heap,even if the array is declared to hold primitive elements.In other words,there is no such thing as primitive array but you can make an array of primitives.
Two dimensional arrays are simply arrays of arrays.
E.g:int[ ] [ ] myArray =new int[3] [ ];//line 1
myArray[ 0 ]=new int[ 2 ];// line 2
myArray[ 0 ] [ 0 ]=6;// line 3
myArray[ 0 ] [ 1 ]=7;// line 4
myArray[ 1 ] = new int[ 3 ];//line 5
myArray [1 ] [ 0 ]=9;//line 6
myArray [1 ] [ 1 ]=8;//line 7
myArray [1 ] [ 2 ]=5;//line 8
In the eg above i have created an array of type int with each element in the array holding a reference to another int array.The 2nd dimension holds actual int primitives.Above,in line 1,i have constructed or created an array of type int which holds 3 integers.In the heap it will represent like this
[ - ] - ] null ] which are positioned as 012(positioning is 0 based ie instead of 123 it will be 012).In line 2,i am putting 2 int(6,7) in index or position 0.In line 5,i am putting 3 int(9,8,5) in index or postion 1.I have not put any int or used 2nd index so it is null.Sorry for not showing this diagrammetically,it wont come out properly. In case if you did not understand,i have given you a link which explains arrays & its dimensions,you may refer it.
http://en.wikipedia.org/wiki/Array

2007-08-10 02:47:37 · answer #2 · answered by coolblue 4 · 1 0

The elements are usually arranged in a linear fashion with a pointer (made up of the coordinates of the array) finding the required value.
Array (3,3)
123123123
... ^Pointer (1,3)

2007-08-10 01:41:39 · answer #3 · answered by Del Piero 10 7 · 1 0

An array stores a list of finite number of homogeneous data elements.
2-D array is represented as int a[3][4];
Address is calculated in two majors.
1. row major
2. column major
U have to know the base address(B) , width(w) ie. the size of int,float, char,etc., no. of rows n columns by the formula ->
upper bond - lower bond +1(if array is given as A[1...m][1...n])
then at last for address use the formula:-
B+W[n(I-Lr)+(J-Lc)] for row major
where
n= no. of columns
Lr=lower limit of rows
Lc=lower limit of columns
I=upper limit of rows
J=upper limit of columns
column wise
address(I,J)=B+w[r(J-Lc)+(I-Lr)]
r=no.of rows

2007-08-10 17:39:30 · answer #4 · answered by Pallavi 2 · 0 0

check ur ignou manual.....or search on google......u will get the answer :)
siv

2007-08-10 01:42:47 · answer #5 · answered by Anonymous · 1 1

fedest.com, questions and answers