In the matrix, you need to display symmetrically about the vertical axis of the sector, which lie to the right and to the left of the main and secondary diagonal. All this needs to be done using Java parallelism.
- And the problem / question is what? How did you try to solve this problem, and with what exactly did you have difficulties? - Regent
- Difficulties with everything. It's hard to imagine how it can be done in several streams - Kristina Kuli
- Dear kff, you may well be right, but hope dies last. Sometimes people just take and help, that's a miracle. I have the courage to hope - Kristina Kuli
- @KristinaKuli I don’t think that it’s so hard to express the gratitude adopted on this site by clicking on the green check mark. People really often just help, but if they stop getting even an elementary “thank you” for their help, the desire to help can pass. Therefore, I propose not to reduce the number of people willing to help. ;) - user194374
- @kff, I agree with you, thanked you for your previous help in solving the problem with JavaScript. But all this is completely irrelevant to the current issue - Kristina Kuli
|
2 answers
Square matrix
A (1,1) A (1,2) A (1,3)
A (2.1) A (2.2) A (2.3)
A (3.1) A (3.2) A (3.3)
A (i, j) - the value of the matrix element
And - the name of the matrix
i - line number
j is the column number
The main diagonal is from the upper left corner to the lower right corner. The secondary diagonal is from the upper right corner to the lower left corner.
The ratio of indices in a square matrix
i<j элементы матрицы находятся над главной диагональю i>j элементы матрицы находятся под главной диагональю i+j<n+1 элементы матрицы находятся над побочной диагональю i+j>n+1 элементы матрицы находятся под побочной диагональю // то что нужно исключить i+j=n+1 элементы матрицы находятся на побочной диагонали i=j элементы матрицы находятся на главной диагонали Based on these data, you will now know which sectors (array elements) are above and below the main and secondary diagonals.
public class MyTest { @Test public void runTest() { int length = 9; int n = length - 2; int[][] underMain = new int[length][length]; int[][] aboveMain = new int[length][length]; int[][] underSecond = new int[length][length]; int[][] aboveSecond = new int[length][length]; int[][] arr = new int[length][length]; fill(arr, 8, length); for (int i = 0; i < length; i++) { for (int j = 0; j < length; j++) { if (i < j) { aboveMain[i][j] = arr[i][j]; } if (i > j) { underMain[i][j] = arr[i][j]; } if (i + j < n + 1) { aboveSecond[i][j] = arr[i][j]; } if (i + j > n + 1) { underSecond[i][j] = arr[i][j]; } } } niceView(underMain); niceView(aboveMain); niceView(underSecond); niceView(aboveSecond); } // метод для вывода в консоль public void niceView(int[][] array) { for (int i = 0; i < array.length; i++) { for (int j = 0; j < array.length; j++) { System.out.printf("[%d]", array[i][j]); } System.out.println(); } System.out.println(); } //Метод для заполнения массива public void fill(int[][] a, int val, int length) { for (int i = 0; i < length; i++) { for (int j = 0; j < length; j++) { a[i][j] = val; } } } } Continuation with concurrency only after clarifying the issue .....
- And where is the required parallelism here? - user194374
- oneThe question will be concretized - there will be an answer with concurrency. At the moment, I can only assume that you will need to use it in order to "display symmetrically about the vertical axis." And here simply there is no place, and to create 4 streams and to drive there a for loop with if for each case is certainly possible, but stupid, since it will not bring any performance gain. - alexandr gaiduchok
|
In the end, I thought and wrote this. In two streams, flips the necessary sectors.
import java.util.Arrays; import java.util.Random; public class Lab1 { public static final int ARRAY_SIZE = 10000; public static final int THREADS_COUNT = 2; public static void generateArr(int arr[][]) { Random randomGenerator = new Random(); for(int i=0; i< arr.length; i++){ for(int j=0;j < arr[i].length; j++){ arr[i][j] = randomGenerator.nextInt(2); } } } public static void printArr(int arr[][]) { for(int i=0; i< arr.length; i++){ for(int j=0;j < arr[i].length; j++){ System.out.print(arr[i][j]+" "); } System.out.println(""); } } public static void transformArr(int arr[][], int from, int to) { for(int j=0; j< arr.length/2; j++){ for(int i=from;i <to ; i++){ if ((i!=j)&&((j)!=(arr.length-1-i)) ){ int t = arr[i][arr.length-1-j]; arr[i][arr.length-1-j] =arr[i][j]; arr[i][j] = t; } } } } static class MatrixTransformationThread extends Thread{ private int arr[][]; private int from; private int to; MatrixTransformationThread(int[][] arr, int from, int to) { super(); this.arr = arr; this.from = from; this.to = to; } @Override public void run(){ for(int j=0; j< arr.length/2; j++){ for(int i=from;i <to ; i++){ if ((i!=j)&&((j)!=(arr.length-1-i)) ){ int t = arr[i][arr.length-1-j]; arr[i][arr.length-1-j] =arr[i][j]; arr[i][j] = t; } } } } } public static void main(String[] args) throws InterruptedException { int array[][] = new int[ARRAY_SIZE][ARRAY_SIZE]; generateArr(array); long startTimeSimple = System.nanoTime(); transformArr(array,1,(array.length-1)/2); transformArr(array,(array.length-1)/2,(array.length-1)); long elapsedTimeSimple = System.nanoTime() - startTimeSimple; System.out.println(elapsedTimeSimple); MatrixTransformationThread [] threads = new MatrixTransformationThread[THREADS_COUNT]; long startTimeThread = System.nanoTime(); threads[0] = new MatrixTransformationThread(array , 1 , (array.length-1)/2 ); threads[1] = new MatrixTransformationThread(array,(array.length-1)/2,(array.length-1)); threads[0].start(); threads[1].start(); for( Thread th : threads ){ th.join(); } long elapsedTimeThread = System.nanoTime() - startTimeThread; System.out.println(elapsedTimeThread); } } |
