3D array multiplication in Java

The problem is this: let's say you have a 3d array, Z (so int [] [] [] Z = new int [n] [n] [n]). What you are trying to do is print a new 3D array, which is a multiplication by Z, X times.

Say what you have

z[3][3][3] = { {{3,3,3}{3,3,3}} {{3,3,3}{3,3,3}} {{3,3,3}{3,3,3}} } 

and x = 3 (user input)

The goal is to print this:

 Z[0] Z[1] XXXXXXX Z[0]*Z[0] Z[1]*Z[1] XXXXXXX Z[0]*Z[0]*Z[0] Z[1]*Z[1]*Z[1] 

Since this is a square (actually cubic) matrix, each printed matrix will be the same. What I'm still like this:

  public static void main(String[] args) { Scanner getIt = new Scanner(System.in); System.out.println("Input 1 integer n: "); int n = getIt.nextInt(); if (n > 0){ final double M[][][] = new double[n][n][n]; for (int i = 0; i < n; i++) for (int j = 0; j < n; j++) for (int k = 0; k < n; k++) M[i][j][k] = 3.0; System.out.println("Input 1 integer p: "); int p = getIt.nextInt(); if(p > 0){ for(int q = 1; q <= p; q++){ for (int i = 0; i < n; i++){ for (int j = 0; j < n; j++){ for (int k = 0; k < n; k++){ System.out.printf("%f ", Math.pow(matrixMult(M[i], M[i])[j][k], q)); } System.out.println("\n"); } } System.out.println("xxxxxxxxxxxxxxxxxxxxx"); } } else{ System.out.println("Woops, you entered a negative p."); } } else{ System.out.println("Woops, you entered a negative n."); } } public static double[][] matrixMult(double a[][], double b[][]) { int aRows = a.length; int aCols = a[0].length; int bCols = b[0].length; double[][] result = new double[aRows][bCols]; for(int i = 0; i < aRows; i++){ for(int j = 0; j < bCols; j++){ for(int k = 0; k < aCols; k++){ result[i][j] += a[i][k] * b[k][j]; } } } return result; } 
+4
source share
1 answer

The Paulsm4 tip is dead - you don't want to do everything in two functions. You must have an input array, user input, multiplication and output, each in its own functions. You may find other ways to split your program as you work on it. I highly recommend breaking your program down into functions as much as possible, especially because you can debug small chunks much more efficiently than large pieces. (What's wrong with your current program? Input? Calculation? Data warehouse sizes, as Adam noted? It's hard to say because you don’t know which functions work fine and which still don't work.)

I would like to suggest the following change: do not put the else your tests so far if you can help. Think about it:

 int n = getIt.nextInt(); if (n > 0){ /* loop */ System.out.println("Input 1 integer p: "); int p = getIt.nextInt(); if(p > 0){ /* loop loop loop */ } else{ System.out.println("Woops, you entered a negative p."); } } else{ System.out.println("Woops, you entered a negative n."); } 

:

 int n = getIt.nextInt(); if (n <= 0) { System.out.println("Woops, you entered a negative n."); /* exit or recover */ } System.out.println("Input 1 integer p: "); int p = getIt.nextInt(); if (p <= 0) { System.out.println("Woops, you entered a negative p."); /* exit or recover */ } /* loop */ /* loop loop loop */ 

Writing code in this way will simplify maintenance and will probably also make it easier to find units that can be deleted in their own routines. (Consider a function that takes a String prompt and Scanner in and returns a number greater than zero - it can query and repeat the request as necessary, and the result will be exactly what the basic code requirements are as clean as possible. This function is more obvious, useful when code is rewritten this way.)

As for the error that brought you here, I think this is probably the culprit:

 System.out.printf("%f ", Math.pow(matrixMult(M[i], M[i])[j][k], q)); 

Here you calculate a lot, but don’t save the results at all. You do not need these results for further calculation?

+1
source

Source: https://habr.com/ru/post/1403376/


All Articles