Write a program to declare a matrix A[ ][ ] of order (m*n) where 'm' is the number of rows and
n is the number of columns such that both m and n must be greater than 2 and less than 20.
Allow the user to input positive integers into this matrix. Perform the following tasks on the
matrix:
(a) Sort the elements of the outer row and column elements in ascending order using any
standard sorting technique.
(b) Calculate the sum of the outer row and column elements.
(c) Output the original matrix, rearranged matrix, and only the boundary elements of the
rearranged array with their sum.
Test your program for the following data and some random data.
1. Example :
INPUT : M=3, N=3
1 7 4
8 2 5
6 3 9
OUTPUT :
ORIGINAL MATRIX :
1 7 4
8 2 5
6 3 9
REARRANGED MATRIX :
1 3 4
9 2 5
8 7 6
BOUNDARY ELEMENTS :
1 3 9
8 4
5 7 6
SUM OF OUTER ROW AND OUTER COLUMN = 43
ISC COMPUTER SCIENCE PRACTICALS 2009
import java.util.*;
public class OuterRowColumn {
public static void main(String[] args) {
int M,N;
Scanner in = new Scanner(System.in);
System.out.println("INPUT THE VALUE OF ROW:");
M = in.nextInt();
System.out.println("INPUT THE VALUE OF COLUMN:");
N = in.nextInt();
while( M > 20 || M < 2 || N > 20 || N < 2) {
System.out.println("OUT OF RANGE, INPUT AGAIN:");
M = in.nextInt();
N = in.nextInt();
}
Matrix m = new Matrix(M,N);
m.inputMatrix();
System.out.println("OUTPUT:");
m.outputMatrix();
System.out.println();
m.rearrangedMatrix();
m.outputMatrix();
System.out.println();
m.boundaryElements();
in.close();
}
}
class Matrix {
Matrix(int M,int N) {
row = M;
col = N;
Mat = new int[M][N];
outerRowColSum = 0;
boundary = new int[2*col+2*(row-2)];
}
public void outputMatrix() {
for(int i = 0; i < row; i++) {
for(int j = 0; j < col; j++) {
System.out.print(Mat[i][j]+" ");
}
System.out.println();
}
}
public void rearrangedMatrix() {
System.out.println("REARRANGED MATRIX :");
int k = 0;
for(int i = 0; i < row; i++)
for(int j = 0; j < col; j++)
if(i == 0 || j == 0 || i == row-1 || j == col-1)
boundary[k++] = Mat[i][j];
Arrays.sort(boundary); // sorting the boundary elements
k = 0;
int i=0,j=0;
// inserting the sorted boundary elements clockwise
for ( i = 0; i < col; i++)
Mat[j][i] = boundary[k++];
for ( i = 1; i < row; i++)
Mat[i][col-1] = boundary[k++];
for ( i = col-2; i >= 0; i--)
Mat[row-1][i] = boundary[k++];
for ( i = row-2; i >= 1; i--)
Mat[i][0] = boundary[k++];
}
public void boundaryElements() {
System.out.println("BOUNDARY ELEMENTS :");
int k = 0;
for(int i = 0; i < row; i++) {
for(int j = 0; j < col; j++) {
if(i == 0 || j == 0 || i == row-1 || j == col-1) { // extracting the boundary elements
System.out.print(Mat[i][j]+" ");
boundary[k++] = Mat[i][j];
outerRowColSum += Mat[i][j];
}
else
System.out.print(" "+" ");
}
System.out.println();
}
System.out.println("SUM OF OUTER ROW AND OUTER COLUMN = "+outerRowColSum);
}
public void inputMatrix() {
Scanner in = new Scanner(System.in);
for(int i = 0; i < row; i++) {
for(int j = 0; j < col; j++) {
System.out.println("Input the element:");
Mat[i][j] = in.nextInt();
}
}
}
private int[] boundary;
private int row;
private int col;
private int[][] Mat;
private int outerRowColSum;
}
n is the number of columns such that both m and n must be greater than 2 and less than 20.
Allow the user to input positive integers into this matrix. Perform the following tasks on the
matrix:
(a) Sort the elements of the outer row and column elements in ascending order using any
standard sorting technique.
(b) Calculate the sum of the outer row and column elements.
(c) Output the original matrix, rearranged matrix, and only the boundary elements of the
rearranged array with their sum.
Test your program for the following data and some random data.
1. Example :
INPUT : M=3, N=3
1 7 4
8 2 5
6 3 9
OUTPUT :
ORIGINAL MATRIX :
1 7 4
8 2 5
6 3 9
REARRANGED MATRIX :
1 3 4
9 2 5
8 7 6
BOUNDARY ELEMENTS :
1 3 9
8 4
5 7 6
SUM OF OUTER ROW AND OUTER COLUMN = 43
ISC COMPUTER SCIENCE PRACTICALS 2009
import java.util.*;
public class OuterRowColumn {
public static void main(String[] args) {
int M,N;
Scanner in = new Scanner(System.in);
System.out.println("INPUT THE VALUE OF ROW:");
M = in.nextInt();
System.out.println("INPUT THE VALUE OF COLUMN:");
N = in.nextInt();
while( M > 20 || M < 2 || N > 20 || N < 2) {
System.out.println("OUT OF RANGE, INPUT AGAIN:");
M = in.nextInt();
N = in.nextInt();
}
Matrix m = new Matrix(M,N);
m.inputMatrix();
System.out.println("OUTPUT:");
m.outputMatrix();
System.out.println();
m.rearrangedMatrix();
m.outputMatrix();
System.out.println();
m.boundaryElements();
in.close();
}
}
class Matrix {
Matrix(int M,int N) {
row = M;
col = N;
Mat = new int[M][N];
outerRowColSum = 0;
boundary = new int[2*col+2*(row-2)];
}
public void outputMatrix() {
for(int i = 0; i < row; i++) {
for(int j = 0; j < col; j++) {
System.out.print(Mat[i][j]+" ");
}
System.out.println();
}
}
public void rearrangedMatrix() {
System.out.println("REARRANGED MATRIX :");
int k = 0;
for(int i = 0; i < row; i++)
for(int j = 0; j < col; j++)
if(i == 0 || j == 0 || i == row-1 || j == col-1)
boundary[k++] = Mat[i][j];
Arrays.sort(boundary); // sorting the boundary elements
k = 0;
int i=0,j=0;
// inserting the sorted boundary elements clockwise
for ( i = 0; i < col; i++)
Mat[j][i] = boundary[k++];
for ( i = 1; i < row; i++)
Mat[i][col-1] = boundary[k++];
for ( i = col-2; i >= 0; i--)
Mat[row-1][i] = boundary[k++];
for ( i = row-2; i >= 1; i--)
Mat[i][0] = boundary[k++];
}
public void boundaryElements() {
System.out.println("BOUNDARY ELEMENTS :");
int k = 0;
for(int i = 0; i < row; i++) {
for(int j = 0; j < col; j++) {
if(i == 0 || j == 0 || i == row-1 || j == col-1) { // extracting the boundary elements
System.out.print(Mat[i][j]+" ");
boundary[k++] = Mat[i][j];
outerRowColSum += Mat[i][j];
}
else
System.out.print(" "+" ");
}
System.out.println();
}
System.out.println("SUM OF OUTER ROW AND OUTER COLUMN = "+outerRowColSum);
}
public void inputMatrix() {
Scanner in = new Scanner(System.in);
for(int i = 0; i < row; i++) {
for(int j = 0; j < col; j++) {
System.out.println("Input the element:");
Mat[i][j] = in.nextInt();
}
}
}
private int[] boundary;
private int row;
private int col;
private int[][] Mat;
private int outerRowColSum;
}
No comments:
Post a Comment