arrays - Filling a vertical matrix Java -
i'm trying fill matrix vertically, 1 row missing. can me ? there code. maybe there easier way fill matrix verically, cant find it.
public static void main(string[]args){ scanner input = new scanner(system.in); system.out.print("enter value of matrix: "); int n = input.nextint(); int [][] matrix = new int [n][n]; (int = 1; < matrix.length; i++) { matrix[0][i] = matrix[0][i -1] + n; } for(int = 1; < matrix.length; i++){ (int j = 0; j < matrix.length; j++){ matrix[i][j] = matrix[i -1][j] + 1; system.out.print(matrix[i][j] + " "); } system.out.println(); } input.close(); }
output: enter value of matrix: 4 1 5 9 13 2 6 10 14 3 7 11 15
try
public static void main(string[]args){ scanner input = new scanner(system.in); system.out.print("enter value of matrix: "); int n = input.nextint(); int [][] matrix = new int [n][n]; matrix[0][0]=0; //you have forgotten first value (int = 1; < matrix.length; i++) { matrix[0][i] = matrix[0][i -1] + n; //initializing first line } for(int = 1; < matrix.length; i++){ (int j = 0; j < matrix.length; j++){ matrix[i][j] = matrix[i -1][j] + 1; } // re-loop display time start i=0 for(int = 0; < matrix.length; i++){ (int j = 0; j < matrix.length; j++){ system.out.print(matrix[i][j] + " "); } system.out.println(); } input.close(); }
Comments
Post a Comment