Take values in 4 x 4 array and interchange the values in cyclic order means 1st row will come down to 2nd row, 2nd row to 3rd, 3rd row to 4th and 4th row to 1st
import java.io.*;
class Matrix
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
int arr[][]=new int[4][4];
int i,j,x;
void take() throws Exception
{
for(i=0;i<4;i++)
{
for(j=0;j<4;j++)
{
System.out.print("\nValue: ");
arr[i][j]=Integer.parseInt(br.readLine());
}
}
}
void arrange()
{
for(i=3;i>0;i--)
{
for(j=0;j<4;j++)
{
x=arr[i][j];
arr[i][j]=arr[i-1][j];
arr[i-1][j]=x;
}
}
}
void show()
{
for(i=0;i<4;i++)
{
for(j=0;j<4;j++)
{
System.out.print(" "+arr[i][j]);
}
System.out.println();
}
}
public static void main(String args[]) throws Exception
{
Matrix ob=new Matrix();
ob.take();
System.out.println("\nArray Before Interchange");
ob.show();
ob.arrange();
System.out.println("\nArray After Interchange");
ob.show();
}
}
Sample output after entering 16 values in a 4 X 4 matrix
Array Before Interchange
12 4 55 6
5 4 3 22
3 4 5 7
8 9 90 9
Array After Interchange
8 9 90 9
12 4 55 6
5 4 3 22
3 4 5 7
No comments:
Post a Comment