GeezerVison is showing github.com — Merge pull request #101 from RajdeepDs/main · arshcoder13/Java@dd29f75
File tree
Expand file treeCollapse file tree
Open diff view settings
Expand file treeCollapse file tree
Open diff view settings
Collapse file
Copy file name to clipboard +113Lines changed: 113 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
|
| 1 | +//Program to sort non-boundary elements in square matrix with print diagonal elements of matrix and sum of diagonal elements. |
| 2 | + |
| 3 | +import java.util.Scanner; |
| 4 | +class SortNonBoundary |
| 5 | +{ |
| 6 | + Scanner sc=new Scanner(System.in); |
| 7 | + int A[][],B[],m,n; |
| 8 | + void input() |
| 9 | + { |
| 10 | + Scanner sc = new Scanner(System.in); |
| 11 | + System.out.print("Enter the size of the square matrix : "); |
| 12 | + m=sc.nextInt(); |
| 13 | + if(m<4 || m>10) |
| 14 | + { |
| 15 | + System.out.println("Invalid Range"); |
| 16 | + System.exit(0); |
| 17 | + } |
| 18 | + else |
| 19 | + { |
| 20 | + A = new int[m][m]; |
| 21 | + n = (m-2)*(m-2); |
| 22 | + B = new int[n]; |
| 23 | + System.out.println("Enter the elements of the Matrix : "); |
| 24 | + for(int i=0;i<m;i++) |
| 25 | + { |
| 26 | + System.out.print("Enter a value : "); |
| 27 | + for(int j=0;j<m;j++) |
| 28 | + { |
| 29 | + A[i][j]=sc.nextInt(); |
| 30 | + } |
| 31 | + } |
| 32 | + } |
| 33 | + } |
| 34 | + void convert(int s) |
| 35 | + { |
| 36 | + int x=0; |
| 37 | + for(int i=0;i<m;i++) |
| 38 | + { |
| 39 | + for(int j=0;j<m;j++) |
| 40 | + { |
| 41 | + if(i != 0 && j != 0 && i != m-1 && j != m-1) |
| 42 | + { |
| 43 | + if(s==1) |
| 44 | + B[x] = A[i][j]; |
| 45 | + else |
| 46 | + A[i][j] = B[x]; |
| 47 | + x++; |
| 48 | + } |
| 49 | + } |
| 50 | + } |
| 51 | + } |
| 52 | + void sortArray() |
| 53 | + { |
| 54 | + int c = 0; |
| 55 | + for(int i=0; i<n-1; i++) |
| 56 | + { |
| 57 | + for(int j=i+1; j<n; j++) |
| 58 | + { |
| 59 | + if(B[i]>B[j]) |
| 60 | + { |
| 61 | + c = B[i]; |
| 62 | + B[i] = B[j]; |
| 63 | + B[j] = c; |
| 64 | + } |
| 65 | + } |
| 66 | + } |
| 67 | + } |
| 68 | + void printArray() |
| 69 | + { |
| 70 | + for(int i=0;i<m;i++) |
| 71 | + { |
| 72 | + for(int j=0;j<m;j++) |
| 73 | + { |
| 74 | + System.out.print(A[i][j]+"\t"); |
| 75 | + } |
| 76 | + System.out.println(); |
| 77 | + } |
| 78 | + } |
| 79 | + void printDiagonal() |
| 80 | + { |
| 81 | + int sum = 0; |
| 82 | + for(int i=0;i<m;i++) |
| 83 | + { |
| 84 | + for(int j=0;j<m;j++) |
| 85 | + { |
| 86 | + if(i==j || (i+j)==m-1) |
| 87 | + { |
| 88 | + System.out.print(A[i][j]+"\t"); |
| 89 | + sum = sum + A[i][j]; |
| 90 | + } |
| 91 | + else |
| 92 | + System.out.print("\t"); |
| 93 | + } |
| 94 | + System.out.println(); |
| 95 | + } |
| 96 | + System.out.println("Sum of the Diagonal Elements : "+sum); |
| 97 | + } |
| 98 | + public static void main(String args[]) |
| 99 | + { |
| 100 | + SortNonBoundary ob = new SortNonBoundary(); |
| 101 | + ob.input(); |
| 102 | + System.out.println("The original matrix:"); |
| 103 | + ob.printArray(); |
| 104 | + ob.convert(1); |
| 105 | + ob.sortArray(); |
| 106 | + ob.convert(2); |
| 107 | + System.out.println("The Rearranged matrix:"); |
| 108 | + ob.printArray(); |
| 109 | + System.out.println("The Diagonal Elements:"); |
| 110 | + ob.printDiagonal(); |
| 111 | + } |
| 112 | +} |
| 113 | + |