Categories

# Calculate Memory Addresses in Arrays

The 2-dimensional arrays are stored as 1-dimensional arrays in the computer’s memory.

There are two ways to achieve this:

## Row-major Implementation

In this method, the first row elements are placed first, then the second row elements and so on.

The formula to calculate the address of [I, J]th block is:

Address of [I, J]th element in row-major = B + W[C(I – Lr) + (J – Lc)]

## Column-major Implementation

In this method, the first column elements are placed first, then the second column elements and so on.

The formula to calculate the address of [I, J]th block is:

Address of [I, J]th element in column-major = B + W[R(J – Lc) + (I – Lr)]

Note that:
B is the base address (address of the first block in the array).
W is the width in bytes (size in bytes for each block in the array).
Lr is the index of the first row.
Lc is the index of the first column.
R is the total number of rows.
C is the total number of columns.

## Solved Questions based on Array Implementation

ISC Year 2020: A matrix B[10][20] is stored in the memory with each element requiring 2 bytes of storage. If the base address at B[2][1] is 2140, find the address of B[5][4] when the matrix is stored in Column Major Wise.

Address of [I, J]th element in column-major = B + W[R(J – Lc) + (I – Lr)]
= 2140 + 2[10(4 – 1) + (5 – 2)]
= 2140 + 2[10 × 3 + 3]
= 2140 + 2[30 + 3]
= 2140 + 2[33]
= 2140 + 66
= 2206

ISC 2020 Specimen: Each element of an array arr[15][20] requires ‘W’ bytes of storage. If the address of arr[6][8] is 4440 and the base address at arr[1][1] is 4000, find the width ‘W’ of each cell in the array arr[][] when the array is stored as Column Major Wise.

Address of [I, J]th element in column-major = B + W[R(J – Lc) + (I – Lr)]
⇒ 4440 = 4000 + W[15(8 – 1) + (6 – 1)]
⇒ 4440 = 4000 + W[15(7) + 5]
⇒ 4440 = 4000 + W[105 + 5]
⇒ 4440 = 4000 + W[110]
⇒ W[110] = 440
⇒ W = 4.

ISC Year 2019: A matrix ARR[-4…6, 3…8] is stored in the memory with each element requiring 4 bytes of storage. If the base address is 1430, find the address of ARR[3][6] when the matrix is stored in Row Major Wise.

Number of columns, C = 8 – 3 + 1 = 6.
Address of [I, J]th element in row-major = B + W[C(I – Lr) + (J – Lc)]
⇒ Address of ARR[3][6] = 1430 + 4[6(3 – (-4)) + (6 – 3)]
⇒ Address of ARR[3][6] = 1430 + 4[6(3 + 4) + 3]
⇒ Address of ARR[3][6] = 1430 + 4[6(7) + 3]
⇒ Address of ARR[3][6] = 1430 + 4[42 + 3]
⇒ Address of ARR[3][6] = 1430 + 4[45]
⇒ Address of ARR[3][6] = 1430 + 180
⇒ Address of ARR[3][6] = 1610.

ISC Year 2018: A matrix A[m][m] is stored in the memory with each element requiring 4 bytes of storage. If the base address at A[1][1] is 1500 and the address of A[4][5] is 1608, determine the order of the matrix when it is stored in Column Major Wise.

Address of [I, J]th element in column-major = B + W[R(J – Lc) + (I – Lr)]
⇒ 1608 = 1500 + 4[m(5 – 1) + (4 – 1)]
⇒ 1608 = 1500 + 4[m(4) + 3]
⇒ 1608 = 1500 + 16m + 12
⇒ 1608 = 1512 + 16m
⇒ 16m = 96
⇒ m = 6.

ISC Year 2017: A matrix P[15][10] is stored with each element requiring 8 bytes of storage. If the base address at P[0][0] is 1400, determine the address at P[10][7] when the matrix is stored in Row Major Wise.

Address of [I, J]th element in row-major = B + W[C(I – Lr) + (J – Lc)]
⇒ Address at P[10][7] = 1400 + 8[10(10 – 0) + (7 – 0)]
⇒ Address at P[10][7] = 1400 + 8[10(10) + 7]
⇒ Address at P[10][7] = 1400 + 8[100 + 7]
⇒ Address at P[10][7] = 1400 + 8[107]
⇒ Address at P[10][7] = 1400 + 856
⇒ Address at P[10][7] = 2256.

ISC Year 2016: A matrix A[m][n] is stored with each element requiring 4 bytes of storage. If the base address at A[1][1] is 1500 and the address at A[4][5] is 1608, determine the number of rows of the matrix when the matrix is stored in Column Major Wise.

Address of [I, J]th element in column-major = B + W[R(J – Lc) + (I – Lr)]
⇒ 1608 = 1500 + 4[R(5 – 1) + (4 – 1)]
⇒ 1608 = 1500 + 4[4R + 3]
⇒ 1608 = 1500 + 16R + 12
⇒ 1608 = 1512 + 16R
⇒ 16R = 96
⇒ R = 6.

ISC Year 2015: The array D[-2…10][3…8] contains double type elements. If the base address is 4110, find the address of D[4][5], when the array is stored in Column Major Wise.

Number of rows, R = 10 – (-2) + 1 = 13.
Address of [I, J]th element in column-major = B + W[R(J – Lc) + (I – Lr)]
⇒ Address of D[4][5] = 4110 + 8[13(5 – 3) + (4 – (-2))]
⇒ Address of D[4][5] = 4110 + 8[13(2) + (4 + 2)]
⇒ Address of D[4][5] = 4110 + 8[26 + 6]
⇒ Address of D[4][5] = 4110 + 8[32]
⇒ Address of D[4][5] = 4110 + 256
⇒ Address of D[4][5] = 4366.

ISC Year 2014: An array AR[-4 … 6, -2 … 12], stores elements in Row Major Wise, with the address AR[2][3] as 4142. If each element requires 2 bytes of storage, find the Base address.

Number of columns, C = 12 – (-2) + 1 = 12 + 2 + 1 = 15.
Address of [I, J]th element in row-major = B + W[C(I – Lr) + (J – Lc)]
⇒ 4142 = B + 2[15(2 – (-4)) + (3 – (-2))]
⇒ 4142 = B + 2[15(2 + 4) + (3 + 2)]
⇒ 4142 = B + 2[15(6) + 5]
⇒ 4142 = B + 2[90 + 5]
⇒ 4142 = B + 2[95]
⇒ 4142 = B + 190
⇒ B = 3952.

ISC Year 2013: A square matrix M[][] of size 10 is stored in the memory with each element requiring 4 bytes of storage. If the base address at M[0][0] is 1840, determine the address at M[4][8] when the matrix is stored in Row Major Wise.

Address of [I, J]th element in row-major = B + W[C(I – Lr) + (J – Lc)]
⇒ Address at M[4][8] = 1840 + 4[10(4 – 0) + (8 – 0)]
⇒ Address at M[4][8] = 1840 + 4[10(4) + 8]
⇒ Address at M[4][8] = 1840 + 4[40 + 8]
⇒ Address at M[4][8] = 1840 + 4[48]
⇒ Address at M[4][8] = 1840 + 192
⇒ Address at M[4][8] = 2032.

ISC Year 2012: A matrix B[10][7] is stored in the memory with each element requiring 2 bytes of storage. If the base address at B[x][1] is 1012 and the address at B[7][3] is 1060, determine the value ‘x’ where the matrix is stored in Column Major Wise.

Address of [I, J]th element in column-major = B + W[R(J – Lc) + (I – Lr)]
⇒ 1060 = 1012 + 2[10(3 – 1) + (7 – x)]
⇒ 1060 = 1012 + 2[10(2) + 7 – x]
⇒ 1060 = 1012 + 2[20 + 7 – x]
⇒ 1060 = 1012 + 2[27 – x]
⇒ 1060 = 1012 + 54 – 2x
⇒ 1060 = 1066 – 2x
⇒ -2x = -6
⇒ x = 3.

ISC Year 2011: A square matrix A [m × m] is stored in the memory with each element requiring 2 bytes of storage. If the base address at A[1][1] is 1098 and the address at A[4][5] is 1144, determine the order of the matrix A[m × m] when the matrix is stored in Column Major Wise.

Address of [I, J]th element in column-major = B + W[R(J – Lc) + (I – Lr)]
⇒ 1144 = 1098 + 2[m(5 – 1) + (4 – 1)]
⇒ 1144 = 1098 + 2[m(4) + 3]
⇒ 1144 = 1098 + 8m + 6
⇒ 1144 = 1104 + 8m
⇒ 8m = 40
⇒ m = 5.

ISC Year 2010: A character array B[7][6] has a base address 1046 at 0, 0. Calculate the address at B[2][3] if the array is stored in Column Major Wise. Each character requires 2 bytes of storage.

Address of [I, J]th element in column-major = B + W[R(J – Lc) + (I – Lr)]
⇒ Address at B[2][3] = 1046 + 2[7(3 – 0) + (2 – 0)]
⇒ Address at B[2][3] = 1046 + 2[7(3) + 2]
⇒ Address at B[2][3] = 1046 + 2[21 + 2]
⇒ Address at B[2][3] = 1046 + 2[23]
⇒ Address at B[2][3] = 1046 + 46
⇒ Address at B[2][3] = 1092.

ISC Year 2009: Each element of an array A[20][10] requires 2 bytes of storage. If the address of A[6][8] is 4000, find the base address at A[0][0] when the array is stored in Row Major Wise.

Address of [I, J]th element in row-major = B + W[C(I – Lr) + (J – Lc)]
⇒ 4000 = B + 2[10(6 – 0) + (8 – 0)]
⇒ 4000 = B + 2[10(6) + 8]
⇒ 4000 = B + 2[60 + 8]
⇒ 4000 = B + 2[68]
⇒ 4000 = B + 136
⇒ B = 3864.

ISC Year 2008: A two-dimensional array defined as X[3…6, -2…2] requires 2 bytes of storage space for each element. If the array is stored in Row Major Wise order, determine the address of X[5][1], given the base address as 1200.

Number of columns, C = 2 – (-2) + 1 = 5.
Address of [I, J]th element in row-major = B + W[C(I – Lr) + (J – Lc)]
⇒ Address of X[5][1] = 1200 + 2[5(5 – 3) + (1 – (-2))]
⇒ Address of X[5][1] = 1200 + 2[5(2) + (3)]
⇒ Address of X[5][1] = 1200 + 2[13]
⇒ Address of X[5][1] = 1200 + 26
⇒ Address of X[5][1] = 1226.

## 2 replies on “Calculate Memory Addresses in Arrays”

sayan ranasays:

Sir,I have tried the program to delete automorphic numbers from the array
in this way
import java.util.Scanner;
class DeleteAutomorphic {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
int a[]=new int[10];
int b[]=new int[10];
int j=0,i;
System.out.println(“Enter array elements:”);
for(i=0;i<10;i++)
a[i]=sc.nextInt();
System.out.println("Original Array:");
for(i=0;i<10;i++)
System.out.print(a[i]+"\t");
for(i=0;i<10;i++){
if(isAutomorphic(a[i],10)==false){
b[j]=a[i];
j++;
}
}

System.out.println("\nResultant Array:");
for(i=0;inum*num)
return false;
else if((num*num)%p==num)
return true;
else
return isAutomorphic(num,p*10);
}
}

Enter array elements:
1
2
3
4
5
6
7
8
9
10
Original Array:
1 2 3 4 5 6 7 8 9 10
Resultant Array:
1 2 3 4 7 8 9 10
Sir,in my output it is showing 1 as non-automorphic number in the Resultant array,Why is it so?

sayan ranasays:

Sir,a program is given which states that :
WAP in JAVA to accept N numbers in a Single Dimensional Array.Now delete all the automorphic numbers from the array and display the new array after deletion.
(An automorphic number is one whose square ends with the original number itself.

Example:
INPUT: 25
OUTPUT: 25 is automorphic.)
The user should call a user-defined recursive function to check whether the number is automorphic or not.
Eg:
N=10
Sample Input:
1 2 3 4 5 6 7 8 9 10
Sample Output:
2 3 4 7 8 9 10

This site uses Akismet to reduce spam. Learn how your comment data is processed.