简体   繁体   中英

Getting element from diagonal 2d array

Hi can someone help me figure out the following question. I got the 2d array to print out the diagonal elements but its in the wrong direction and i have no clue how to generate the other parts!

Fill in an array of N rows and N columns as follows:

• the elements of the last column (except the element of line 0 and column N-1) are created by a random natural number between 0 and 10 limits included.

• the elements of the first column (except the element of line N-1 and of column 0) are created by a random natural number between 0 and 10 limits included.

• The elements of the diagonal going from the upper right corner to the lower left corner are obtained by calculating the square of the number of the line of the element.

• any other element is obtained:

✓ if it is above the diagonal, by adding a random number 0 or 1 or 2 to the element of the same column and of the adjacent lower line.

✓ if it is below the diagonal, by adding a random number 1 or 2 or 3 to the element of the same column and of the upper adjacent line.

HERE YOU CAN SEE WHAT I HAVE SO FAR

public class TableauxJuinA {

public static void main(String[] args) {
    // TODO Auto-generated method stub
    //////////////bloc1:decleration des variables////////////
    int n;
    int a;
    /////////////bloc2///////////////////////////////////////
    n = (int)(1+Math.random()*(10)*2+1);
     a = 0;
    int[][]tab = new int [n][n];
    ///////////////////bloc3/?/////////////////
    for (int j = 0; j < n-1; j++) {
        tab[j][n-1] = (int) (Math.random()*10);
    }
    for (int  j = 0; j <tab.length; j++) {
    tab[j][0]=(int)(Math.random()*10);
    for (int diagonale = 0; diagonale <tab.length; diagonale++) {   
        tab[diagonale][diagonale]= (int)(diagonale*diagonale);
    }
    }
    aff(tab,n);
}
private static void aff(int[][] tab, int n ) {
    for (int i = 0; i <n; i++) {
        for (int j = 0; j < n; j++) {
            System.out.print("  [" + tab[i][j] + "]\t");


        }
        System.out.println();

You should think about your array indices very well. If I have an 3x3 array like below, the first array (ie [1, 2, 3]) has index 0. So if I want to get the 3, the first number on the diagonal you want, I would have to call array[0][2], the third number (zero-indexed) of the first array. Therefore the indices of the diagonal upper-left to bottom-right are indeed array[x][x], but the indices for the upper-right to lower-left are in this case array[0][2], array[1][1] and array[2][0]. These indices could be called with the for-loop I have defined below.

array = [[1, 2, 3],
         [4, 5, 6], 
         [7, 8, 9]]

for (int row = 0; row < array.length; row++) {
    for (int column = array.length - 1; column >= 0; column--) {
         int diagonal = array[row][column]
    }

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM