arrays - How Can I Display a N x N Matrix of Random Numbers in Java? -
i'm trying create matrix of random double numbers. matrix must of size n x n , numbers must between 1 , 100. i've been trying sort out ages , know must simple (as is).
here code:
public static void main(string[] args) { printrandomgraph(randomarray(4)); } private static double[] randomarray(int n) { double[] randomarray = new double[n]; double[][] randommatrix = new double [n][n]; random rand = new random(); rand.setseed(system.currenttimemillis()); (int = 0; < n; i++) { integer r = rand.nextint()% 100; randomarray[i] = math.abs(r); (int j = 0; j < n; j++) { arrays.fill(randommatrix, i, i+1, randomarray); } } return randomarray; } private static void printrandomgraph(double[] inputarray) { int n = inputarray.length; double[] showarray = new double[n]; double[][] showmatrix = new double [n][n]; (int j = 0; j < n; j++) { (int = 0; < n; i++) { double r = inputarray[i]; showarray[i] = r; arrays.fill(showmatrix, i, i+1, showarray); } } system.out.println(arrays.deeptostring(showmatrix)); }
when run code random array repeated n times :
[[63.0, 97.0, 64.0, 75.0], [63.0, 97.0, 64.0, 75.0], [63.0, 97.0, 64.0, 75.0], [63.0, 97.0, 64.0, 75.0]]
i think need go top of loop , add new array...? please =(
any appreciated. thank you.
did try this:
private static double[][] randomarray(int n) { double[][] randommatrix = new double [n][n]; random rand = new random(); rand.setseed(system.currenttimemillis()); (int = 0; < n; i++) { (int j = 0; j < n; j++) { integer r = rand.nextint()% 100; randommatrix[i][j] = math.abs(r); } } return randommatrix; }
to print graph:
public void printgraph(double[][] array, int n) { (int = 0; < n; i++) { (int j = 0; j < n; j++) { system.out.println(array[i][j]); } } }
to print graph square brackets:
public void printgraph(double[][] array, int n) { (int = 0; < n; i++) { system.out.print(" [ "); (int j = 0; j < n; j++) { system.out.print(array[i][j]); } //put println instead of print here have each row in new line system.out.print(" ]"); } }
also, seems lot homework, if is, please tag :)
Comments
Post a Comment