GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

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
Filter options
  • Programs
    • SortNonBoundary.java
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎Programs/SortNonBoundary.java‎

Copy file name to clipboard
+113Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
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+

0 commit comments

Comments
0 (0)
You are visitor 01402520 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.