1

I am currently working on trying to add and multiply two matrices together and print the results. I had done the multiplication right or at least I am certain however I cannot test the results as I do not know how to print it. Everything seemed fine and I just needed to figure out how to print results but as soon as I placed in the addition of the matrices I ran into multiple issues. Here is the blank code I was given beforehand to finish in with what was necessary to add, multiply and print the matrices:

public class Matrix {

public static void matrix_add(double[][] a, double[][] b) {
// add matrices

}

public static void matrix_multi(double[][] a, double[][] b) {
// multiply matrices

}

public static void main(String[] args) {

 double ar1[][] =
        {{.7,.2,.1},
        {.3, .6, .1},
        {.5, .1, .4}};

 double ar2[][] =
        {{.2, .3, .5},
         {.1, .2, .1},
         {.1, .3, .4}};

matrix_add(ar1, ar2);
System.out.println();
matrix_multi(ar1, ar2);

 }

}

Here are my results after of which I am certain my calculations are correct for addition and multiplying the matrices:

public class operationson2Darrays {

public static void matrix_add(double[][] ar1, double[][] ar2) {
    // add matrices
    double[][] ar4 = new  double[ar1.length][ar1[0].length];
    for(int i=0;i<ar1.length;i++){
        for(int j=0;j<ar1[0].length;j++){
            ar4[i][j] = ar1[i][j] + ar2[i][j];
        }
    }


public static void matrix_multi(double[][] ar1, double[][] ar2) {
    // multiply matrices
    int i, j, k;
    int row1 = ar1.length;
    int col1 = ar1[0].length;
    int row2 = ar2.length;
    int col2 = ar2[0].length;
    int[][] ar3 = new int[row1][col2];
    for (i = 0; i < row1; i++) {
        for (j = 0; j < col2; j++) {
            for (k = 0; k < col1; k++) {
                ar3[i][j] += ar1[i][k] * ar2[k][j];
            }
        }
    }
}

public static void main(String[] args) {

    double ar1[][] =
            {{.7, .2, .1},
                    {.3, .6, .1},
                    {.5, .1, .4}};

    double ar2[][] =
            {{.2, .3, .5},
                    {.1, .2, .1},
                    {.1, .3, .4}};

    matrix_add(ar1, ar2);
    System.out.println();
    matrix_multi(ar1, ar2);


 }

}
}

I am currently running into lots of issues the first being that ar1 and ar2 are already being defined in scope. I understand what that means but I do not have the slightest clue how to fix it. It is also expecting tokens in this line : public static void main(String[] args) {... ?I am confused o what it is supposed to be excepting and lastly it is saying that this line is expecting a method call?: matrix_multi(ar1, ar2);

I am starting to get very confused and assuming my calculations are correct for each section if I remove the adding matrices all of a sudden all of the issues disappear. I would appreciate any help on these errors that I am receiving and how I can go about fixing this and also how I would be able to print the results of the matrices.

  • copy and pasted the code and everything works, the only thing is you are missing a closing bracket for the matrix_add method... other than that i get no errors running your code... – RAZ_Muh_Taz Mar 28 '18 at 18:50
  • also you are using an int[][] for your multiply method, you probably want to use a doubl[][]. finally i'd suggest returning the result of your add or multiply, so have your methods return a double[][] instead of void – RAZ_Muh_Taz Mar 28 '18 at 18:53
  • I don't see any attempt to print anything anywhere other than a final newline. – FredK Mar 28 '18 at 18:56
  • I do not understand how to print matrices results I have tried but nothing has been working I would appreciate any tips to push me in the right direction @FredK –  Mar 28 '18 at 19:04
  • Ah I had not realized! I had removed a closing bracket from the matrix add method and added an additional one at the very end of my code, thank you @RAZ_Muh_Taz –  Mar 28 '18 at 19:05

1 Answers1

0

You should have your methods return the new 2D array so you can do something with the array later, like print it for example. Also you want to have your multiply 2D array be a double[][] instead of an int[][] since you are multiplying two double[][]'s. Finally you can take the newly created arrays and pass them into a method that prints the contents of your arrays.

public static double[][] matrix_add(double[][] ar1, double[][] ar2) {
    // add matrices
    double[][] ar4 = new  double[ar1.length][ar1[0].length];
    for(int i=0;i<ar1.length;i++){
        for(int j=0;j<ar1[0].length;j++){
            ar4[i][j] = ar1[i][j] + ar2[i][j];
        }
    }

    return ar4;
}


public static double[][] matrix_multi(double[][] ar1, double[][] ar2) {
    // multiply matrices
    int i, j, k;
    int row1 = ar1.length;
    int col1 = ar1[0].length;
    int row2 = ar2.length;
    int col2 = ar2[0].length;
    double[][] ar3 = new double[row1][col2];
    for (i = 0; i < row1; i++) {
        for (j = 0; j < col2; j++) {
            for (k = 0; k < col1; k++) {
                ar3[i][j] += ar1[i][k] * ar2[k][j];
            }
        }
    }

    return ar3;
}

public static void print2DArray(double[][] ar)
{
    for(int i=0;i<ar.length;i++){
        for(int j=0;j<ar[0].length;j++){
            System.out.print(ar[i][j] + " ");
        }
        System.out.println();
    }
}

public static void main(String[] args) {

    double ar1[][] =
            {{.7, .2, .1},
                    {.3, .6, .1},
                    {.5, .1, .4}};

    double ar2[][] =
            {{.2, .3, .5},
                    {.1, .2, .1},
                    {.1, .3, .4}};

    double[][] arAdd = matrix_add(ar1, ar2);
    print2DArray(arAdd);
    System.out.println();
    double[][] arMulti = matrix_multi(ar1, ar2);
    print2DArray(arMulti);

 }

Output

0.8999999999999999 0.5 0.6 
0.4 0.8 0.2 
0.6 0.4 0.8 

0.16999999999999998 0.28 0.41000000000000003 
0.13 0.24 0.25 
0.15000000000000002 0.29 0.42000000000000004 
RAZ_Muh_Taz
  • 4,059
  • 1
  • 13
  • 26
  • Wow thank you! Absolutely prefect and I had tried changing it to double as my initial matrices are double anyways but I kept accidentally changing things that I had not wanted to. As of for printing I was doing some weird things were clearly completely wrong, thank you for the help. –  Mar 28 '18 at 19:10