Matrix problem in java using bubble sort [SOLVED]

  • Comp Sci
  • Thread starter ishika17
  • Start date
  • #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

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();
  }
}


1617899301542.png
 
Last edited:

Answers and Replies

  • #2
36,911
8,967
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.
This confused me at first. The matrix is two-dimensional. What you're calling dimensions are actually the row size and column size.
ishika17 said:
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
bsum() was also confusing at first until I saw the code. Your comment should state that bsum() calculates the sum of the first elements of each row and the last elements of each row.
ishika17 said:
This is my attempted code

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 - 1)
              in2++;
            else
              in1++;
            if (in2 == N)
              in2 = 0;
            if (in1 == M)
              in1 = 0;
          }
        }
      }
    }
  }
  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();
  }
}
Your store() method is more complicated than it needs to be. All it needs to do is to find the largest value in the matrix and the smallest value. If you call sort() first (once you get it working correctly), the largest value will be at A[0][0] and the smallest value will be at A[M-1][N-1]. You don't need any loops at all.
ishika17 said:
https://www.physicsforums.com/attachments/281126

Although it's giving the output but he elements are not sorted completely

It should be:
11 9 8
6 5 4
3 2 -3

So guys, I'm hoping you could help me point out where the problem is.
Thank You.
Pretty obviously, there is something wrong in your bubble sort. I'm reasonably sure you have access to a debugger, so I suggest you use it to single-step through your code in your sort() method, and see where things are going wrong. I suspect, but don't know for sure, that your problem is here:
Java:
      for (int j = 0; j < M; j++) {
        for (int k = 0; k < N; k++) {
Namely, that you have M and N switched. Try your code out on a small matrix, say 2 rows of 3 columns, and see if the code does something wrong.

Other comment
Your choice of names for variables is not good. You have too many one-letter variable names, which makes it difficult for a reader (me) to understand what they are used for. I've taught many programming classes, and I always encourage my students to use meaningful names (by deducting points if they don't do so).
Instead of M and N, use something like RowSize and ColSize. Single letter names for loop variables such as i, j, and k, are fine, but all other variables should have a meaningful name.

Your program uses d (terrible name for a class), m, n, mm, nn, M, N, A -- all should be more descriptive. Not quite as bad are in1, in2, and ob.
 
  • #3
ishika17
10
3
Your comment should state that bsum() calculates the sum of the first elements of each row and the last elements of each row.
but it actually calculates the border elements which also includes the elements in first and and last columns and not just rows.
 
  • #4
ishika17
10
3
If you call sort() first (once you get it working correctly), the largest value will be at A[0][0] and the smallest value will be at A[M-1][N-1]
I'm calculating maximum and minimum values so i can sort them in descending order because i couldn't think of how to sort the the elements which are arranged in both rows and columns.
 
  • #5
ishika17
10
3
Your choice of names for variables is not good. You have too many one-letter variable names, which makes it difficult for a reader (me) to understand what they are used for
even i don't like it.
But sadly, when this is what you've been taught since your first programming class,this is what happens.
they would ask us not to take names of variable and class which are long (like index,Maxvalues,etc.) even though they are sensible.
I'll take note of it.
 
  • #6
ishika17
10
3
by deducting points if they don't do so
i agree.
Your program uses d (terrible name for a class), m, n, mm, nn, M, N, A -- all should be more descriptive. Not quite as bad are in1, in2, and ob.
thank you.I'll take note of it.

This is why I'm actually here and i hope to improve further.
 
  • #7
36,911
8,967
but it actually calculates the border elements which also includes the elements in first and and last columns and not just rows.
The set of first elements in each row is the set of elements in the first column. Similarly, the set of last elements in each row is the set of elements in the last column.
 
  • #8
36,911
8,967
Your program uses d (terrible name for a class), m, n, mm, nn, M, N, A -- all should be more descriptive. Not quite as bad are in1, in2, and ob.
they would ask us not to take names of variable and class which are long (like index,Maxvalues,etc.) even though they are sensible.
I'm very sorry to hear that your instructor is giving you such bad advice. As I mentioned, I taught programming classes for a long time ( > 25 years), plus I worked in the software industry. Every textbook and every development team I worked with emphasized using informative variable names. They don't have to be long, just long enough to make it easy for someone else reading your code to understand what the variable is being used for. Your instructor doesn't know what he or she is talking about. It might not be a good strategy to convey my opinion to your instructor, but that is my considered opinion.
This is why I'm actually here and i hope to improve further.
Good for you! There are quite a few people here who are very knowledgeable in all sorts of programming languages.
 

Suggested for: Matrix problem in java using bubble sort [SOLVED]

Replies
2
Views
413
Replies
3
Views
83
Replies
2
Views
355
Replies
1
Views
320
Replies
6
Views
129
Replies
19
Views
968
Replies
17
Views
478
Replies
9
Views
952
Top