Matrix problem in java using bubble sort

Click For Summary

Discussion Overview

The discussion revolves around a Java programming task involving the implementation of a matrix class that performs various operations, including inputting matrix dimensions, calculating the sum of boundary elements, finding maximum and minimum values, and sorting the matrix using bubble sort. The conversation includes code snippets and attempts to clarify the requirements and functionality of the program.

Discussion Character

  • Homework-related
  • Technical explanation
  • Debate/contested

Main Points Raised

  • One participant describes the variables used in their matrix implementation, including methods for input, display, boundary sum calculation, storing max and min values, and sorting.
  • Another participant expresses confusion regarding the terminology used for matrix dimensions, clarifying that they refer to row size and column size.
  • A participant notes that the boundary sum calculation method, bsum(), was initially confusing but is clarified through the provided code.
  • Concerns are raised about the complexity of the store() method, suggesting it could be simplified by leveraging the sorted order of the matrix.
  • One participant points out that the sorting is not functioning correctly and requests assistance in identifying the issue.
  • Another participant suggests using a debugger to step through the sort() method to identify potential errors, specifically mentioning a possible mix-up of matrix dimensions in the loop structure.
  • Feedback is provided regarding variable naming conventions, recommending more descriptive names for better code readability.

Areas of Agreement / Disagreement

Participants express differing views on the implementation details, particularly regarding the sorting method and variable naming conventions. There is no consensus on the correctness of the current sorting implementation, and the discussion remains unresolved regarding the specific issues in the code.

Contextual Notes

Participants highlight potential limitations in the current implementation, including the complexity of certain methods and the need for clearer variable names. There is also mention of unresolved issues in the sorting logic that may affect the output.

ishika17
Messages
10
Reaction score
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:
Physics news on Phys.org
ishika17 said:
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.
 
  • Like
Likes   Reactions: ishika17
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.
 
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.
 
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.
 
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.
 
ishika17 said:
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.
 
  • Like
Likes   Reactions: ishika17
Mark44 said:
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.
ishika17 said:
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.
ishika17 said:
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.
 
  • Like
Likes   Reactions: ishika17

Similar threads

  • · Replies 18 ·
Replies
18
Views
3K
  • · Replies 12 ·
Replies
12
Views
2K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 5 ·
Replies
5
Views
3K
  • · Replies 14 ·
Replies
14
Views
4K
  • · Replies 6 ·
Replies
6
Views
3K
  • · Replies 3 ·
Replies
3
Views
1K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 4 ·
Replies
4
Views
3K