For Details of the program: CLICK HERE
Programming Code:
import java.io.*;
class CSort
{
int
m,arr[][],dummy[];
BufferedReader br=new BufferedReader(new
InputStreamReader(System.in));
void takeNumber()throws Exception
{
while(true)
{
System.out.println("Enter
the Number of rows(3 to 10):");
m=Integer.parseInt(br.readLine());
if(m>=3 && m<=10)
break;
}
arr=new int[m][m];
dummy=new int[(m-2)*(m-2)];
for(int
i=0;i< m;i++)
{
for(int j=0;j< m;j++)
{
System.out.println("Enter
Value:");
arr[i][j]=Integer.parseInt(br.readLine());
}
}
}
void show()
{
for(int
i=0;i< m;i++)
{
for(int
j=0;j< m;j++)
{
System.out.print("
"+arr[i][j]);
System.out.println();
}
}
void takeToDummy()
{
int x=0;
for(int i=0;i< m;i++)
{
for(int j=0;j< m;j++)
{
if(i!=0 && i!=m-1 && j!=0
&& j!=m-1)
dummy[x++]=arr[i][j];
}
}
}
void takeToArray()
{
int x=0;
for(int i=0;i< m;i++)
{
for(int j=0;j< m;j++)
{
if(i!=0 && i!=m-1 && j!=0
&& j!=m-1)
arr[i][j]=dummy[x++];
}
}
}
void showDiagonals()
{
int left,right;
left=right=0;
System.out.println("Diagonal values are as
follows.\n");
for(int i=0;i< m;i++)
{
for(int
j=0;j< m;j++)
{
if (i==j ||i+j==m-1)
System.out.print("
"+arr[i][j]);
else
System.out.print("
");
if(i==j)
left+=arr[i][j];
else if(i+j==m-1)
right+=arr[i][j];
}
System.out.println();
}
System.out.println("Sum of Left Diagonal
values="+left);
}
void sort()
{
int i,j,t;
for(i=0;i< (m-2)*(m-2);i++)
{
for(j=i+1;j< (m-2)*(m-2);j++)
{
if(dummy[i]>dummy[j])
{
t=dummy[i];
dummy[i]=dummy[j];
dummy[j]=t;
}
}
}
}
public static
void main(String args[]) throws Exception
{
CSort ob=new
CSort();
ob.takeNumber();
System.out.println("Original
Array\n");
ob.show();
ob.takeToDummy();
ob.sort();
ob.takeToArray();
System.out.println("Arranged
Array\n");
ob.show();
ob.showDiagonals();
}
}
Variable
Description
Type
|
Name
|
Purpose
|
Scope
|
||
int
|
m
|
Holds the size of
|
Within all functions
|
||
array
|
|||||
BufferedReader
|
br
|
Used for user input
|
Within void
|
||
takeNumber ()
|
|||||
function
|
|||||
int
|
arr [][]
|
2-d array to hold the
|
Within all functions
|
||
numbers
|
|||||
int
|
dummy []
|
Used to sort the non
|
Within voi sort (),
|
||
boundary
elements
|
void
takeToArray ()
|
||||
functions
|
|||||
Algorithm
|
|||||
Step 1: int type variable ‘m’ and BufferedReader
object ‘br’ are created. Array arr[][] and dummy [] are declared
Step 2: Take the size of the square array from
user and store it in ‘m’
Step 3: Check the validity of the value of ‘m’
and if it passes, create arrays arr[][] and dummy [].
Step 4: Take values from user and store them in
array ‘arr [][]’.
Step 5: Display the original array.
Step 6: Take the non boundary values from the
array ‘arr [][]’ and store them in array dummy [].
Step 7: Sort the elements in the 1-d array dummy
and finally set those values in the original array arr [][] in non boundary
locations so that the elements are sorted now.
Step 8: Now display the array arr [][]
Step 9: Display the diagonal values and sum of
the diagonals
Back To 2016 Computer Practical paper: CLICK HERE
Back To 2016 Computer Practical paper: CLICK HERE
No comments:
Post a Comment