Please open the menu to show more

Friday, August 4, 2017

Code to perform some very useful operations on matrix (double dimensional array)

This code is implemented and provided by => Mayank Banga (WW)=> CSE=> G.B.U (Noida)


# save this class inside MatrixAssignment.java file #

// inside this code we going to perform some operations on matrix, like transpose, sum of elements, printing in clockwise, and anti-clock wise directions

import java.util.Scanner;

class MatrixAssignment {

void showMatrix(int[][] a) {
for (int i = 0; i < a.length; i++) {
for (int j = 0; j < a[i].length; j++) {
System.out.print(a[i][j] + "\t");
}
System.out.println();
}
}

void transposeMatrix(int[][] a) {
for (int i = 0; i < a.length; i++) {
for (int j = 0; j < a[i].length; j++) {
System.out.print(a[j][i] + "\t");
}
System.out.println();
}
}

void diagonlOfMatrix1(int[][] a) {
int sum = 0;
for (int i = 0; i < a.length; i++) {
for (int j = 0; j < a[i].length; j++) {
if (i == j) {
System.out.print(a[i][j]);
sum = sum + a[i][j];
} else {
System.out.print("\t");
}
}
System.out.println();
}
System.out.println("\n\nSum of diagonal matrix is :" + sum);
}

void diagonlOfMatrix2(int[][] a) {
int sum2 = 0;
for (int i = 0; i < a.length; i++) {
for (int j = 0; j < a[i].length; j++) {
if (i == a.length - 1 - j) {
System.out.print(a[i][j]);
sum2 = sum2 + a[i][j];
} else {
System.out.print("\t");
}
}
System.out.println();
}
System.out.println("\n\nSum of diagonal matrix is :" + sum2);
}

void maxValueOfMatrix(int[][] a) {
int max = a[0][0];
for (int i = 0; i < a.length; i++) {
for (int j = 0; j < a[i].length; j++) {
if (max < a[i][j]) {
max = a[i][j];
}

}
}
System.out.println("\n\n Max value in full matrix is : " + max);
}

void minValueOfMatrix(int[][] a) {
int min = a[0][0];
int sum4 = 0;
for (int i = 0; i < a.length; i++) {
for (int j = 0; j < a[i].length; j++) {
sum4 = sum4 + a[i][j];
if (min > a[i][j]) {
min = a[i][j];
}

}
}
System.out.println("\n\n Min value in full matrix is : " + min);
}
void sumOfMatrix(int[][]a){
int sum4 = 0;
for (int i = 0; i < a.length; i++) {
for (int j = 0; j < a[i].length; j++) {
sum4 = sum4 + a[i][j];
}
}
System.out.println("\n\n sum of full matrix is : " + sum4);
}

void minOfMAtrixPerRow(int[][] a) {
for (int i = 0; i < a.length; i++) {
int min2 = a[i][0];
for (int j = 0; j < a[i].length; j++) {
if (min2 > a[i][j]) {
min2 = a[i][j];
}
}
System.out.println("\nMin value in row :" + i + " is :" + min2);
}
}

void maxOfMAtrixPerRow(int[][] a) {
for (int i = 0; i < a.length; i++) {
int max2 = a[i][0];
for (int j = 0; j < a[i].length; j++) {
if (max2 < a[i][j]) {
max2 = a[i][j];
}
}
System.out.println("\nMax value in row :" + i + " is :" + max2);
}
}

void sumOfMAtrixPerRow(int[][] a) {
for (int i = 0; i < a.length; i++) {
int sum = 0;
for (int j = 0; j < a[i].length; j++) {
sum = sum + a[i][j];
}
System.out.println("\nSum of row :" + i + " is :" + sum);
}
}

void sumOfMAtrixPerColumn(int[][] a) {
for (int i = 0; i < a.length; i++) {
int sum = 0;
for (int j = 0; j < a[i].length; j++) {
sum = sum + a[j][i];
}
System.out.println("\nSum of column :" + i + " is :" + sum);
}
}

void maxOfMAtrixPerColumn(int[][] a) {
for (int i = 0; i < a.length; i++) {
int max3 = a[0][i];
for (int j = 0; j < a[i].length; j++) {
if (max3 < a[j][i]) {
max3 = a[j][i];
}
}
System.out.println("\nMax value in column :" + i + " is :" + max3);
}
}

void minOfMAtrixPerColumn(int[][] a) {
for (int i = 0; i < a.length; i++) {
int min3 = a[0][i];
for (int j = 0; j < a[i].length; j++) {
if (min3 > a[j][i]) {
min3 = a[j][i];
}
}
System.out.println("\nMin value in column :" + i + " is :" + min3);
}
}

void showClockwiseMatrix(int[][] a) {
int x = 0;
int[] b = new int[a.length - 1];
for (int i = 0; i < a.length; i++) {
for (int j = 0; j < a[i].length; j++) {
if (i == 0) {
System.out.print(a[i][j] + "  ");
} else if (i == a.length - 1) {
System.out.print(a[a.length - 1][a[i].length - 1 - j] + "  ");
} else {

System.out.print(a[i][a[i].length - 1] + "  ");
b[x] = a[i][0];
x = x + 1;
break;
}
}
}
for (int i = x - 1; i >= 0; i--) {
System.out.print(b[i] + "  ");
}
}

void showAntiClockwiseMatrix(int[][] a) {
int x = 0;
int[] b = new int[a.length - 1];
for (int i = 0; i < a.length; i++) {
for (int j = 0; j < a[i].length; j++) {
if (i == 0) {
System.out.print(a[j][i] + "  ");
} else if (i == a.length - 1) {
System.out.print(a[a[i].length - 1 - j][a.length - 1] + "  ");
} else {

System.out.print(a[a[i].length - 1][i] + "  ");
b[x] = a[0][i];
x = x + 1;
break;
}
}
}
for (int i = x - 1; i >= 0; i--) {
System.out.print(b[i] + "  ");
}
}

}

public class MatrixAssignment {

public static void main(String[] args) {
Scanner sc= new Scanner(System.in);

System.out.print("please enter the no of rows : --> ");
int rows=sc.nextInt();

System.out.print("please enter the no of columns : --> ");
int columns=sc.nextInt();

// define a matrix of given rows and columns
                int[][] a=new int [rows][columns];
System.out.println("please enter the elements for matrix :");
for (int i = 0; i < rows; i++) {
for (int j = 0; j < columns; j++){
                                System.out.print("enter the value for row-"+i+" and column-"+j+":");
a[i][j]=sc.nextInt();
}
}

System.out.println("Please choose any of the following option : ");
System.out.print("1 to see the matrix"
+ "\n 2 to see the transpose of matrix"
+ "\n 3 to see the left diagonal of matrix and its sum"
+ "\n 4 to see the right diagonal of matrix and its sum"
+ "\n 5 to see the max value in matrix"
+ "\n 6 to see the min value in matrix"
+ "\n 7 to see the sum of full matrix"
+ "\n 8 to see the min value per row in matrix"
+ "\n 9 to see the max value per row in matrix"
+ "\n 10 to see the sum of elements per row in matrix"
+ "\n 11 to see the min value per column in matrix"
+ "\n 12 to see the max value per column in matrix"
+ "\n 13 to see the sum of elements per column in matrix"
+ "\n 14 to see the outermost elements in clockwise form"
+ "\n 15 to see the outermost elements in anti-clockwise form : --> ");
int ch=sc.nextInt();
MatrixAssignment obj=new MatrixAssignment();
switch(ch){
case 1:
System.out.println("your matrix is : \n");
obj.showMatrix(a);
break;
case 2:
System.out.println("your matrix is : \n");
obj.showMatrix(a);
System.out.println("transpose matrix is : \n");
obj.transposeMatrix(a);
break;
case 3:
System.out.println("your matrix is : \n");
obj.showMatrix(a);
System.out.println("Here is your answer");
obj.diagonlOfMatrix1(a);
break;
case 4:
System.out.println("your matrix is : \n");
obj.showMatrix(a);
System.out.println("Here is your answer");
obj.diagonlOfMatrix2(a);
break;
case 5:
System.out.println("your matrix is : \n");
obj.showMatrix(a);
System.out.println("Here is your answer");
obj.maxValueOfMatrix(a);
break;
case 6:
System.out.println("your matrix is : \n");
obj.showMatrix(a);
System.out.println("Here is your answer");
obj.minValueOfMatrix(a);
break;
case 7:
System.out.println("your matrix is : \n");
obj.showMatrix(a);
System.out.println("Here is your answer");
obj.sumOfMatrix(a);
break;
case 8:
System.out.println("your matrix is : \n");
obj.showMatrix(a);
System.out.println("Here is your answer");
obj.minOfMAtrixPerRow(a);
break;
case 9:
System.out.println("your matrix is : \n");
obj.showMatrix(a);
System.out.println("Here is your answer");
obj.maxOfMAtrixPerRow(a);
break;
case 10:
System.out.println("your matrix is : \n");
obj.showMatrix(a);
System.out.println("Here is your answer");
obj.sumOfMAtrixPerRow(a);
break;
case 11:
System.out.println("your matrix is : \n");
obj.showMatrix(a);
System.out.println("Here is your answer");
obj.minOfMAtrixPerColumn(a);
break;
case 12:
System.out.println("your matrix is : \n");
obj.showMatrix(a);
System.out.println("Here is your answer");
obj.maxOfMAtrixPerColumn(a);
break;
case 13:
System.out.println("your matrix is : \n");
obj.showMatrix(a);
System.out.println("Here is your answer");
obj.sumOfMAtrixPerColumn(a);
break;
case 14:
System.out.println("your matrix is : \n");
obj.showMatrix(a);
System.out.println("Here is your answer");
obj.showClockwiseMatrix(a);
break;
case 15:
System.out.println("your matrix is : \n");
obj.showMatrix(a);
System.out.println("Here is your answer");
obj.showAntiClockwiseMatrix(a);
break;
default :
System.out.println("you selected wrong choice Please try again by restarting program");
}
}
}

No comments:

Post a Comment