This program is implemented by SHRISHTI SHRIVASTAV / Bteh CSE/ Delhi University
import java.util.Scanner;
public class Matrix {
// lets declare a two dimension array
// look here, the array memory is not allocated yet
// we will declare memory for this array later on
static int[][]a;
/*
this method will be used to take array size and its
values from the keyboard
*/
static void createAndInitializeArray() {
// create Scanner to take input from keyboard
Scanner sc=new Scanner(System.in);
System.out.print("Enter no of rows for matrix: ");
int rows=sc.nextInt();
System.out.print("Enter no of cols for matrix: ");
int cols=sc.nextInt();
// allocate memory for matrix
a=new int[rows][cols];
// now take values for matrix from keyboard
// this loop works for the i'th row of matrix
for(int i=0;i<a.length;i++) {
// this loop works for all the columns of i'th row of matrix
for(int j=0;j<a[i].length;j++) {
System.out.print("enter value for row:"+i+" col:"+j+"->");
// get int value from keyboard and store it at given row and col
a[i][j]=sc.nextInt();
}
System.out.println("row:"+i+" initialized");
}
}
/*
this method will be used to show the content of array as matrix
*/
static void showMatrix() {
System.out.println("###matrix is given below###");
for(int i=0;i<a.length;i++) {
for(int j=0;j<a[i].length;j++) {
System.out.print(a[i][j]+"\t");
}
// print a new line after each row
System.out.println();
}
}
/*
this method will be used to show the right diagonal of matrix
*/
static void printRightDiagonal() {
for(int i=0;i<a[0].length;i++) {
for(int j=0;j<a[0].length;j++) {
for(int k=0;k<a[0].length;k++) {
System.out.print(" ");
}
if(i==j)
System.out.print(a[i][j]);
}
System.out.print('\n');
}
}
/*
this method will be used to show the left diagonal of matrix
*/
static void printLeftDiagonal() {
for(int i=0;i<a[0].length;i++) {
for(int j=0;j<a[0].length;j++) {
for(int k=a[0].length-1;k>=0;k--) {
System.out.print(" ");
}
if(i+j==a[0].length-1) {
System.out.print(a[i][j]);
}
}
System.out.print('\n');
}
}
/*
this method will be used to show the content of matrix clockwise
*/
static void printClockwise() {
for(int i=0, j=0;j<a[0].length;j++) {
System.out.print(a[i][j]);
}
for(int k=1,m=a[0].length-1;k<a[0].length;k++) {
System.out.print(a[k][m]);
}
for(int i=a[0].length-1,j=1;j>=0;j--) {
System.out.print(a[i][j]);
}
System.out.println(a[1][0]);
}
/*
this method will be used to show the content of matrix anti clockwise
*/
static void printAntiClockwise() {
for(int i=0, j=0;j<a[0].length;j++) {
System.out.print(a[j][i]);
}
for(int k=1,m=a[0].length-1;k<a[0].length;k++) {
System.out.print(a[m][k]);
}
for(int i=a[0].length-1,j=1;j>=0;j--) {
System.out.print(a[j][i]);
}
System.out.println(a[0][1]);
}
/*
this method will be used to rotate matrix 90 degree and show it
*/
static void rotate() {
for(int i=0;i<a[0].length;i++) {
for(int j=a[0].length-1;j>=0;j--) {
System.out.print(a[j][i]);
}
System.out.print('\n');
}
}
public static void main(String[] args) {
// create and initialized matrix
createAndInitializeArray();
// show matrix
showMatrix();
Scanner s=new Scanner(System.in);
for(int i=1;i<=5;i++) {
System.out.println("Enter a choice");
System.out.println("1. Right diagonal");
System.out.println("2. Left diagonal");
System.out.println("3. clockwise print ");
System.out.println("4. Anti-clockwise print");
System.out.println("5. rotate by 90 degree");
int choice=s.nextInt();
switch(choice) {
case 1:
System.out.println("###printing array right diagonal###");
printRightDiagonal();
break;
case 2:
System.out.println("###printing array left diagonal###");
printLeftDiagonal();
break;
case 3:
System.out.println("###printing array clockwise###");
printClockwise();
break;
case 4:
System.out.println("###printing array anti-clockwise###");
printAntiClockwise();
break;
case 5:
System.out.println("###printing array rotating at 90 degree angle###");
rotate();
break;
default : System.out.println("Invalid choice");
}
}
}
}
public class Matrix {
// lets declare a two dimension array
// look here, the array memory is not allocated yet
// we will declare memory for this array later on
static int[][]a;
/*
this method will be used to take array size and its
values from the keyboard
*/
static void createAndInitializeArray() {
// create Scanner to take input from keyboard
Scanner sc=new Scanner(System.in);
System.out.print("Enter no of rows for matrix: ");
int rows=sc.nextInt();
System.out.print("Enter no of cols for matrix: ");
int cols=sc.nextInt();
// allocate memory for matrix
a=new int[rows][cols];
// now take values for matrix from keyboard
// this loop works for the i'th row of matrix
for(int i=0;i<a.length;i++) {
// this loop works for all the columns of i'th row of matrix
for(int j=0;j<a[i].length;j++) {
System.out.print("enter value for row:"+i+" col:"+j+"->");
// get int value from keyboard and store it at given row and col
a[i][j]=sc.nextInt();
}
System.out.println("row:"+i+" initialized");
}
}
/*
this method will be used to show the content of array as matrix
*/
static void showMatrix() {
System.out.println("###matrix is given below###");
for(int i=0;i<a.length;i++) {
for(int j=0;j<a[i].length;j++) {
System.out.print(a[i][j]+"\t");
}
// print a new line after each row
System.out.println();
}
}
/*
this method will be used to show the right diagonal of matrix
*/
static void printRightDiagonal() {
for(int i=0;i<a[0].length;i++) {
for(int j=0;j<a[0].length;j++) {
for(int k=0;k<a[0].length;k++) {
System.out.print(" ");
}
if(i==j)
System.out.print(a[i][j]);
}
System.out.print('\n');
}
}
/*
this method will be used to show the left diagonal of matrix
*/
static void printLeftDiagonal() {
for(int i=0;i<a[0].length;i++) {
for(int j=0;j<a[0].length;j++) {
for(int k=a[0].length-1;k>=0;k--) {
System.out.print(" ");
}
if(i+j==a[0].length-1) {
System.out.print(a[i][j]);
}
}
System.out.print('\n');
}
}
/*
this method will be used to show the content of matrix clockwise
*/
static void printClockwise() {
for(int i=0, j=0;j<a[0].length;j++) {
System.out.print(a[i][j]);
}
for(int k=1,m=a[0].length-1;k<a[0].length;k++) {
System.out.print(a[k][m]);
}
for(int i=a[0].length-1,j=1;j>=0;j--) {
System.out.print(a[i][j]);
}
System.out.println(a[1][0]);
}
/*
this method will be used to show the content of matrix anti clockwise
*/
static void printAntiClockwise() {
for(int i=0, j=0;j<a[0].length;j++) {
System.out.print(a[j][i]);
}
for(int k=1,m=a[0].length-1;k<a[0].length;k++) {
System.out.print(a[m][k]);
}
for(int i=a[0].length-1,j=1;j>=0;j--) {
System.out.print(a[j][i]);
}
System.out.println(a[0][1]);
}
/*
this method will be used to rotate matrix 90 degree and show it
*/
static void rotate() {
for(int i=0;i<a[0].length;i++) {
for(int j=a[0].length-1;j>=0;j--) {
System.out.print(a[j][i]);
}
System.out.print('\n');
}
}
public static void main(String[] args) {
// create and initialized matrix
createAndInitializeArray();
// show matrix
showMatrix();
Scanner s=new Scanner(System.in);
for(int i=1;i<=5;i++) {
System.out.println("Enter a choice");
System.out.println("1. Right diagonal");
System.out.println("2. Left diagonal");
System.out.println("3. clockwise print ");
System.out.println("4. Anti-clockwise print");
System.out.println("5. rotate by 90 degree");
int choice=s.nextInt();
switch(choice) {
case 1:
System.out.println("###printing array right diagonal###");
printRightDiagonal();
break;
case 2:
System.out.println("###printing array left diagonal###");
printLeftDiagonal();
break;
case 3:
System.out.println("###printing array clockwise###");
printClockwise();
break;
case 4:
System.out.println("###printing array anti-clockwise###");
printAntiClockwise();
break;
case 5:
System.out.println("###printing array rotating at 90 degree angle###");
rotate();
break;
default : System.out.println("Invalid choice");
}
}
}
}
No comments:
Post a Comment