简体   繁体   中英

Filling in the main diagonals in a 2D array

I am trying to write a code that fills in the 2 main diagonals in an NxN matrix, for example: if N=5 (which is entered through command line), we would have a 5x5 matrix filled with zeros and the diagonals would have 2s filled in, like:

2 0 0 0 2
0 2 0 2 0
0 0 2 0 0
0 2 0 2 0
2 0 0 0 2

I wrote a code for an all-zero table, but i can't figure our how to fill in the diagonals. Looking at the case of 5x5 i would have to fill in the matrix at the following indices:

#1 (0,0)  (0,n-1) 
#2 (1,1) (1,n-2)
#3 (2,2) (2,n-3) 
#4 (3,1) (3, n-2)
#5 (4,0) (4,n-1)

However, since N can be any number, i assume that first i have to find the middle row, after which i have to decrement the indices in the reverse order.

I am learning Java for 2 weeks only and this one is pretty hard. My code for a zero-filled table is this:

public static void main (String[] args){
        int n = Integer.parseInt(args[0]);
        System.out.println(n);
        int[][] table = new int[n][]; 
         for (int i = 0; i < n; i++) { 
         table[i] = new int[i + 1]; 
         for (int j = 0; j <= i; j++) { 
         table[i][j] = (0); 


            }
        } System.out.print(Arrays.deepToString(table));
    }

Obviously, this is very far from what i need to achieve, and i am not sure if it's entirely right. I would really appreciate some help.

Try this,

/* package whatever; // don't place package name! */

import java.util.*;
import java.lang.*;
import java.io.*;

/* Name of the class has to be "Main" only if the class is public. */
class Ideone
{
    public static void main (String[] args){
        int n = 5;
        System.out.println(n);
        int[][] table = new int[n][]; 
         for (int i = 0; i < n; i++) { 
         table[i] = new int[n]; 
         for (int j = 0; j < n; j++) {
            if(i==j || n-i == j+1){
                table[i][j] = table[i][n-i-1] = 2;  
            }
            else{
            table[i][j] = 0; 
            }
            System.out.print(table[i][j]);
            }
            System.out.println();
        } 
    }
}

To fill one diagonal, we just have to count from i==0 to i==size-1 and fill in (i,i) each time.

 for(int i=0; i<size; i++) {
     table[i][i] = 2;
 }

The other diagonal is only slightly harder:

 for(int i=0; i<size; i++) {
     table[i][some calculation involving i and size] = 2;
 }

You should be able to work out what the calculation is.

You can do it in two loops - but you can also combine filling both diagonals into one loop.

You could also amend your nested loops that create the array, to handle the cells on the diagonals as you encounter them there.

  #inside the loop
  if( a condition indicating that the cell is on a diagonal) {
      table[i][j] = 2;
  } else {
      table[i][j] = 0;
  }

Write down the coordinates of the cells on the diagonals, and you should quickly see what the condition is.

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