You are on page 1of 15

Ch.

11: Double Dimensional Array

11.1 2-D Array: An array of arrays is known as double dimensional array.

11.2 Declaration and Instantiation of 2-D arrays

Syntax to declare a 2-D array is:

<data-type> <Array-name> [][];


or
<data-type> [ ][] <Array-name>; // to declare multiple arrays with same size and type.
e.g. int ar[][];
int [ ][]a,b;

Syntax to Instantiate a 2-D arrays

<Array-name>= new <data-type> [Rows] [Cols]


e.g. ar = new int [5][5];

We can combine both statements to declare and instantiate an array

<data-type> <Array-name> [][] = new <data-type> [Rows][Cols]


e.g. int ar[] = new int [5][5];
// create an array ar of order 5 x 5 (i.e. 25 elements.)

int [][]a,b = new int [5][5];


// create two integer arrays ‘a’ and ‘b’ with order 5 x 5.

11.3 Initialization of 2-D Arrays:

(i) Static Initialization:


<data-type> <Array-name> [] []= { {Row1 elements},{Row2 elements},…..};

e.g.
int ar[][] ={ {2,6,8},{9,10,14},{10,5,2}};
(ii) Dynamic Initialization:

Step 1. <data-type> <Array-name> [][] = new <data-type> [Rows][Cols];


Step 2.
for(int i=0 ; i<Rows ; i++)
{
for(int j=0 ; j<Cols ; j++)
{
<Array-name>[i][j] = sc.nextInt();
}
}
e.g
int ar[][]=new int[3][4];
System.out.println(“Enter the elements in array”);
for(int i=0 ; i<3 ; i++)
{
for(int j=0 ; j<4 ; j++)
{
ar[i][j]=sc.nextInt();
}
}
11.4 To display a 2-D array of order M x N
System.out.println(“Matrix given as”);
for(int i=0 ; i<M ; i++)
{
for(int j=0 ; j<N ; j++)
{
System.out.print(ar[i][j]+”\t”); // To display row elements in same line with tab space

}
System.out.println(); //To display a new row in next line
}
11.5 Memory representation of 2-D Array
A 2-D array stores elements at adjacent locations in memory.
e.g
int ar[][] ={{5,6,7},{5,2,7},{1,6,8}.{9,4,5}}; will store as:

[0] [1] [2]

ar[0] 5 6 7
ar[0][0] ar[0][1] ar[0][2]

ar[1] 5 2 7
ar[1][0] ar[1][1] ar[1][2]

ar[2] 1 6 8
ar[2][0] ar[2][1] ar[2][2]

ar[3] 9 4 5
ar[3][0] ar[3][1] ar[3][2]

Prog 1: Write a program to create and display a N X N matrix. Also display both diagonals of
the matrix.
Sol.
import java.util.*;
class Diagonals
{
public static void main()
{
Scanner sc =new Scanner (System.in);
System.out.println("Enter size(N)");
int N=sc.nextInt();
int i,j,a[][]=new int[N][N];
//To input elements
System.out.println("Enter elements");
for(i=0;i<N;i++)
{
for(j=0;j<N;j++)
{
a[i][j]=sc.nextInt();
}
}
//To display Matrix
System.out.println("Matrix given as");
for(i=0;i<N;i++)
{
for(j=0;j<N;j++)
{
System.out.print(a[i][j]+"\t");
}
System.out.println();
}
// To print only diagonals
System.out.println("Diagonal elements of the matrix");
for(i=0;i<N;i++)
{
for(j=0;j<N;j++)
{
if((i==j)||(i+j==N-1)) // for left diagonal i==j and for rigth i+j==N-1
System.out.print(a[i][j]+"\t");
else
System.out.print(" \t");
}
System.out.println();
}
}
}

OUTPUT
Enter size(N)
3
Enter elements
123456789
Matrix given as

1 2 3

4 5 6

7 8 9

Diagonal elements of the matrix

1 3

7 9
Prog 2: Write a program to create and display a m x n matrix. Reverse each element of the
matrix and display the resultant matrix also.
e.g.
ORIGINAL MATRIX RESULTANT MATRIX
12 6 24 21 6 42
25 15 36 52 51 63
1 10 48 1 1 84

Sol.
import java.util.*;
class MatReverse
{
int arr[][],m,n;
public MatReverse(int mm,int nn)
{
m=mm;
n=nn;
arr = new int[m][n];
}
//To input
void fillarray()
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter elements");
for(int i=0;i<m;i++)
{
for(int j=0;j<n;j++)
{
arr[i][j]=sc.nextInt();
}
}
}
// To get reverse of a number x
int reverse(int x)
{
int rev=0,d;
while(x!=0)
{
d=x%10;
rev=rev*10 + d;
x=x/10;
}
return rev;
}
//To display the Matrix
void show()
{
for(int i=0;i<m;i++)
{
for(int j=0;j<n;j++)
{
System.out.print(arr[i][j]+"\t");
}
System.out.println();
}
}
//To assign reverse of each number in the matrix
void revMat()
{
for(int i=0;i<m;i++)
{
for(int j=0;j<n;j++)
{
arr[i][j]=reverse(arr[i][j]);
}
}
}
public static void main(int r,int c)
{
MatReverse ob1=new MatReverse(r,c);
ob1.fillarray();
System.out.println("Original Matrix");
ob1.show();
ob1.revMat();
System.out.println("Resultant Matrix");
ob1.show();
}
}
Prog 3: Write a program to create and display a m x n matrix. Also rotate and display the
matrix clock-wise and Anti-clock wise.
e.g.
ORIGINAL MATRIX MATRIX(ClockWise) Matrix(AntiClcokWise)
12 6 4 1 5 12 4 6 8
5 1 6 10 1 6 6 1 10
1 10 8 8 6 4 12 5 1

import java.util.*;
class rotate90
{
//instance variables
int arr[][];
int m,n;
//Parameterized constructor
public rotate90(int mx,int nx)
{
m=mx;
n=nx;
arr=new int[m][n];
}
//To input
void acceptMat()
{
Scanner in =new Scanner (System.in);
System.out.println("Enter elements");
for(int i=0;i<m;i++)
{
for(int j=0;j<n;j++)
{
arr[i][j]=in.nextInt();
}
}
}
//To display the matrix
void displayMat()
{
System.out.println("Matrix given as");
for(int i=0;i<m;i++)
{
for(int j=0;j<n;j++)
{
System.out.print(arr[i][j]+"\t");
}
System.out.println();
}
}
//To rotate matrix 90 degree clock wise
void Rotate()
{
System.out.println("Matrix after 90 degree rotation given as");

for(int i=0;i<n;i++)
{
for(int j=m-1;j>=0;j--)
{
System.out.print(arr[j][i]+"\t");
}
System.out.println();
}
}
//To rotate matrix 90 degree anticlock wise
void RotateAnti()
{
System.out.println("Matrix after 90 degree rotation given as");

for(int i=n-1;i>=0;i--)
{
for(int j=0;j<m;j++)
{
System.out.print(arr[j][i]+"\t");
}
System.out.println();
}
}
// To execute
public static void main(int r,int c)
{
rotate90 ob = new rotate90(r,c);
ob.acceptMat();
ob.displayMat();
ob.Rotate();
ob.RotateAnti();
}
}

11.6 Memory Address Calculation in 2-D Array


In 2-D array, elements are stored in two ways
(1) Row Major wise
(2) Column Major wise
e.g. An array of order 3 X 3 will store 9 elements (1,2,3,4,5,6,7,8,9) as :
Row major wise Column major wise
1 2 3 1 4 7
4 5 6 2 5 8
7 8 9 3 6 9

Formulae to find location of an element of 2-D array


1. Row major wise
Address of Ar[I][J] = B + W.[ C.(I - Lr) +( J - Lc) ]
2. Column major wise
Address of Ar[I][J] = B + W.[ (I - Lr) +R.( J - Lc) ]
Where
B  Base Address (Address of first element)
W  Size of each element (in Bytes)
R  Total no. of rows
C  Total no. of columns
Lr  Lower index of rows
Lc  Lower index of columns
Note: To calculate R and C
case 1 : If an array given as AR[X][Y] then
Lr = Lc = 0 ,
R=X and C=Y
e.g. for an array Mat[10][20]
Lr = Lc = 0 ,
R=10 and C=20
Case 2: if an array given as AR[Lr…..Ur][Lc…..Uc] then
R=Ur – Lr + 1 and C = Uc – Lc +1
e.g. for an array Mat[-6…5][2…11]
Lr = -6 , Ur = 5 , Lc = 2 , Uc = 11
R= 5 – ( -6) +1 = 12
C = 11 – 2 +1 = 10

Q.1:
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.
Sol.
Given
B = 1500
Lr = Lc = 1
W=4
Address of A [4][5]=1608
R=C=m
Now in Column major wise
Address of A [I][J] = B + W.[ (I - Lr) +R.( J - Lc) ]
Address of A [4][5] = 1500 + 4 [ (4-1) + m.(5-1) ]
1608 = 1500 + 4[3+4m]
108 = 4[3+4m]
27 =3+4m
4m=24
m=6
Hence order of array is 6 X 6.

Q.2:
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.
Sol.
Given
B = 1430
Lr = -4 , Ur = 6 , Lc = 3 , Uc = 8
R=6-(-4)+1=11
C=8-3+1=6
W=4
Now in Row major wise
Address of ARR[I][J] = B + W.[C. (I - Lr) +( J - Lc) ]
Address of ARR[3][6] = 1430 + 4. [ 6.(3-(-4)) +(6-3) ]
= 1430 + 4 [ 42+3]
=1430+180
=1610

You might also like