Multiply 3D array in Java

The problem is: suppose you have a 3D array, Z (so int [] [] z = New Int [n] [n] [n]) All you have to do is print a new 3D array, Z, x times multiplication

Say you have

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

And x = 3 (user input)

The goal is to print out:

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 a cube...) matrix, each printed matrix will have the same size What I have so far is:

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;
}

Solution

Paulsm4's advice is dead - you don't want to do the whole thing with just two functions You should have input arrays, user input, multiplication, and output, each in their own functions You may find other opportunities to separate your program - I strongly recommend breaking your program into functions as much as possible, especially because you can debug small pieces more effectively than large pieces (what's wrong with your current program? Input? Output? Calculation? Data storage size, as Adam noticed? It's hard to say, because you don't know which functions work perfectly and which still have problems.)

I would like to suggest a further change: if you can help, don't put other terms of your test so far away Consider changing this:

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.");
}

In this regard:

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 make maintenance easier, and perhaps it will be easier to find their own conventions that can be moved into their units (consider a scanner in a function parameter with a string prompt and return a number greater than zero - it can prompt and re prompt as needed, and the result will be your main code requirement - keep it as clean as possible. This function is more useful when the code is rewritten in this way.)

As for the mistake of bringing you here, I think it may be the culprit:

System.out.printf("%f ",q));

You calculate a lot here, but you don't store the results at all Don't you need these results to be calculated further?

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>