- #1
ishika17
- 10
- 3
- Homework Statement
- We have to design a program in Java to accept the dimensions of a matrix such that the dimensions are greater than 2 and less than 8.Then we have to allow the console to enter integers in the array.
we have to display the original matrix with the sum of it's boundary matrix and Also display the matrix and the sum of boundary elements after sorting the elements in descending order using a Bubblesort technique.
- Relevant Equations
- none
I have taken the variables as follows:
A[][]=the matrix
max=to store the maximum integer value present in the matrix
min=to store the minimum integer value present in the matrix
sum=to store the sum of boundary elements
display()=methos to print matrix
sort()=method to sort matrix in descending order using bubblesort
store()=to find and store maximum and minimum values in max and min
bsum()=to calculate the sum of boundary elements
I've fixed the problem.If you want to have a look
A[][]=the matrix
max=to store the maximum integer value present in the matrix
min=to store the minimum integer value present in the matrix
sum=to store the sum of boundary elements
display()=methos to print matrix
sort()=method to sort matrix in descending order using bubblesort
store()=to find and store maximum and minimum values in max and min
bsum()=to calculate the sum of boundary elements
I've fixed the problem.If you want to have a look
Java:
import java.util.*;
class d {
int A[][];
int M, N, sum, max, min;
d(int mm, int nn) {
M = mm;
N = nn;
A = new int[M][N];
max = 0;
min = 0;
sum = 0;
}
void Input() {
Scanner sc = new Scanner(System.in);
System.out.println("ENTER THE ELEMENTS OF THE MATRIX");
for (int i = 0; i < M; i++) {
for (int j = 0; j < N; j++) {
A[i][j] = sc.nextInt();
}
}
}
void display() {
for (int i = 0; i < M; i++) {
for (int j = 0; j < N; j++) {
System.out.print(A[i][j] + "\t");
}
System.out.println();
}
}
void bsum() {
sum = 0;
for (int i = 0; i < M; i++) {
for (int j = 0; j < N; j++) {
if (i == 0 || j == 0 || i == M - 1 || j == N - 1)
sum = sum + A[i][j];
}
}
System.out.println("THE SUM OF BORDER ELEMENTS IS:" + sum);
}
void store() {
max = A[0][0];
min = A[0][0];
for (int i = 0; i < M; i++) {
for (int j = 0; j < N; j++) {
if (A[i][j] > max)
max = A[i][j];
if (A[i][j] < min)
min = A[i][j];
}
}
System.out.println("MAX=" + max + "MIN=" + min);
}
void sort() {
int in1 = 0, in2 = 0, temp = 0;
for (int i = max; i >= min; i--) {
for (int j = 0; j < M; j++) {
for (int k = 0; k < N; k++) {
if (A[j][k] == i) {
temp = A[in1][in2];
A[in1][in2] = A[j][k];
A[j][k] = temp;
if (in2<=N-2)
in2++;
else
{
in2 = 0;
in1++;
}
}
}
}
}
}
public static void main(String args[]) {
int m, n;
Scanner sc = new Scanner(System.in);
do {
System.out.println("ENTER THE ROWS AND COLUMNS BETWEEN 2 AND 8");
m = sc.nextInt();
n = sc.nextInt();
System.out.println("m=" + m + "n=" + n);
if (m <= 2 || n <= 2 || m >= 8 || n >= 8)
System.out.println("OUT OF RANGE");
}
while (m <= 2 || n <= 2 || m >= 8 || n >= 8);
d ob = new d(m, n);
ob.Input();
System.out.println("THE ORIGINAL MATRIX IS:");
ob.display();
ob.bsum();
ob.store();
ob.sort();
System.out.println("THE SORTED MATRIX IS:");
ob.display();
ob.bsum();
}
}
Last edited: