+ 1

# Pls help. Working with matrices in Java.

each of the three tasks must be written as a method. Task 1. Swap the columns, with the maximum and minimum elements, places Task 2. Find the maximum value in the matrix, delete the row and column in which it is located. Task 3. Delete rows and columns for multiple maxima in the matrix. here is my code that outputs the matrix: public class Main { public static void main(String[] args) { int ar[][] = {{2, 1, -10, 4, 9}, {4, 0, 1, 11, 6}, {1, 3, 9, 4, 19}}; showMatrix(ar); } static void showMatrix(int[][] x) { for (int i = 0; i < x.length; i++) { for (int j = 0; j <x[i].length; j++) { System.out.printf("%10s",x[i][j]); } System.out.println(); } } } The code ia correct but I need 3 more methods of each task.

8 Answers

+ 5

Continue in same way...
For 1) : you need to find -10, 19 locations? Same way use 2 loops and find.
2) Initially set min = ar[0][0]; and max = 0;
Change current element is less then overide to min , and greater than max then override max. And Locations as minR, maxR, minC, maxC.
3) You can't delete from arrays in java as it is static data structure. You can copy to new array or skip when printing in the output .
Pls Show your attempt if not successful..
hope it helps...
edit:
Each will same just some changes ..

+ 5

You will need to create a new matrix if you need to delete rows and colums.
Maybe this helps:
https://stackoverflow.com/questions/1805147/how-to-remove-a-row-from-a-2d-array

+ 4

John
When you found min and max -> store columnMax and columnMin.
if(arr[i][j] > maxValue){
maxValue = arr[i][j];
columnMax = i;
}
i is the index of the outer loop, j the index of the inner loop:
-> i is for the columns
-> j is for the rows
(Maybe it is better to use column and row as name instead of i and j).
Do the same for minValue.
Now you just swap the arrays of your array, like you would do with other variables:
int[] temp = arr[columnMax];
arr[columnMax] = arr[columnMin];
arr[columnMin] = temp;

+ 3

John
It's actually I already mentioned. I edited, see again my first post. You can't delete from array.
Copy to new array, by skipping the row and column values which includes max, min values.
For example : -10 is at i=0, j = 2 so skip when I=0 or j = 2 while in looping... (Entire 0th row and 2 column )..
You can copy or just print on the output, if no need to store in array..

+ 2

thanks to all of you, I will try to do 1 task, maybe someone knows how to do 2 tasks? I don't really understand how to delete the row and column of the matrix.

+ 2

Jayakrishna thank you very much for your help,i will try to do it.

+ 1

Task 1) I found the max and min element, but I don't know how to swap the 1st and 3rd lines (min with max)
static int maxElement(int[][] ar) {
int maxValue = ar[0][0];
for (int j = 0; j < ar.length; j++) {
for (int i = 0; i < ar[j].length; i++) {
if (ar[j][i] > maxValue) {
maxValue = ar[j][i];
}
}
}
return maxValue;
}
static int minElement(int[][] ar) {
int maxValue = ar[0][0];
for (int j = 0; j < ar.length; j++) {
for (int i = 0; i < ar[j].length; i++) {
if (ar[j][i] < maxValue) {
maxValue = ar[j][i];
}
}
}
return maxValue;
}

+ 1

Thousand thanks man

Hot today

What's wrong? :(

1 Votes

Why won't my image display

0 Votes

I have made a calculator in which my % (Percentage) not work correctly for 100%50 or 100%20.

2 Votes

How the answer is 50?

0 Votes

Number of Ones ( C++ ) question!

1 Votes