Professional Documents
Culture Documents
e.g.
int ar[][] ={ {2,6,8},{9,10,14},{10,5,2}};
(ii) Dynamic Initialization:
}
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:
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
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();
}
}
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