1. your-netid_hw5_1.java : Liang page 253, programming exercise 7.6.
(Multiplying two matrices) Write a method to multiply two matrices. The header of the method is as follows:
public static double[][] multiplyMatrix(double[][] a, double[][] b)
To multiply matrix a by matrix b, the number of columns in a must be the same as the number of rows in b, and the two matrices must have elements of the same or compatible types. Let c be the result of the multiplication. Each element cij is ai1 x b1j + ai2 x b2j + … + ain x bnj.
Write a test program that prompts the user to enter tow 3 x 3 matrices and displays their product.

So this was the homework problem, and this is the code I have. And it works. The only problem is the output needs to be rounded, I don't know how to do that. Help???

import java.util.Scanner;
public class MultiplyMatrix {
	
	public static void main(String[]args){
		
		System.out.println("Enter numbers continuously for a 3 x 3 matrix: ");
		 Scanner input = new Scanner(System.in); 
		 double [][] matrix1 = new double[3][3];
		 for (int r = 0; r < 3; r++)
		 {
		 	for (int c = 0; c < 3; c++)
		 	{
				System.out.print("Enter a number: ");
				matrix1[r][c] = input.nextDouble();
			}
		 }
		 
			System.out.println("Now enter numbers continuously for second 3 x 3 matrix: ");			 
			 double [][] matrix2 = new double [3][3];
			 for (int g = 0; g < 3; g++)
			 {
			 	for (int d = 0; d < 3; d++)
			 	{
					System.out.print("Enter a number: ");
					matrix2[g][d] = input.nextDouble();
				}
			 }
		 
		multiply(matrix1, matrix2);
	}

	public static double[][] multiply(double a[][], double b[][]) {
			int m1rows = a.length;
		    int m1cols = a[0].length;
		    int m2cols = b[0].length;
		  
		    double[][] result = new double[m1rows][m2cols];

		    for (int i=0; i<m1rows; i++)
		      for (int j=0; j<m2cols; j++)
		        for (int k=0; k<m1cols; k++){
		        result[i][j] += a[i][k] * b[k][j];
		        }
		    
		    printMatrix(result);
		 return result;
	}
	
	  static void printMatrix(double[][] mat) {
	       int M = mat.length;
	       int N = mat[0].length;

	       for (int i = 0; i < M; i++) {
	           for (int j = 0; j < N; j++) {
	               System.out.print( mat[i][j] + " ");
	           }
	           System.out.println();
	       }
	   }

}

The test problem asks you to input
1 2 3
4 5 6
7 8 9

and

0 2 4
1 4.5 2.2
1.1 4.3 5.3
This is what it outputs:

5.300000000000001 23.9 24.0
11.600000000000001 56.3 58.2
17.9 88.69999999999999 92.4

How can i round those values????

Humm i'm not sure I understand. I know to how round numbers. But how would i round

result[i][j] += a[i][k] * b[k][j];

Edited 3 Years Ago by mike_2000_17: Fixed formatting

Do you want to round the value in the result?
Vs rounding the value when it is printed?
For example if result = 1.555543
Do you want to change the contents of result to 1.560000?
or leave the value in result unchanged and only round it when printing?

How many decimal places do you want to round to?

Sorry I should have specified. I need all the values to be rounded to two decimal places. So for example 88.69999999999999 should be 88.69 or 88.70

Are you referring to the values in the variables or the Strings that are printed?
The values in the variables will always have the same significance unless you change them.
To round a double to the second decimal place, add .005, multiply by 100, convert to int and divide by 100 back into the double.
For example:

double dbl = 96.567899999;
      int rnded = (int)((dbl + .005) * 100);    // round to 2 decimal places
      double dblRnded = rnded/100.0;
      System.out.println("dbl=" + dbl + ", dblRnded=" + dblRnded); // dbl=96.567899999, dblRnded=96.57

for example 88.69999999999999 should be 88.69 or 88.70

The first: 88.69 is truncated, the second is rounded

Edited 5 Years Ago by NormR1: n/a

Okay. I know how to round a single value. My issue is that the final method that i call is PrintMatrix, and it prints the "result" matrix I used in the Multiply method. I need to figure out how to round ALL the numbers in that matrix so they only have two decimal places. Its a 3x3 matrix and it outputs this.
5.300000000000001 23.9 24.0
11.600000000000001 56.3 58.2
17.9 88.69999999999999 92.4

I need the program to return the matrix to look a little more cleaned up. So how would i make the entire matrix rounded and not just one number.

For instance instead of this:
5.300000000000001 23.9 24.0
11.600000000000001 56.3 58.2
17.9 88.69999999999999 92.4
I need this:
5.3 23.9 24.0
11.6 56.3 58.2
17.9 88.7 92.4

how to round ALL the numbers

how would i make the entire matrix rounded and not just one number.

Write code that takes each number in the array and formats it the way you want.

Another solution would be to use the DecimalFormat class.

public static void main(String[] args) throws Exception {
	double[] doubles = {5.300000000000001, 123231.600000000000001, 88.695555555 };
	final DecimalFormat fmt = new DecimalFormat(".00");
	for (double d : doubles) {
		System.out.println(fmt.format(d));
	}
}

Edited 5 Years Ago by ~s.o.s~: n/a

Use the DecimalFormat class.

public static void main(String[] args) throws Exception {
	double[] doubles = {5.300000000000001, 123231.600000000000001, 88.695555555 };
	final DecimalFormat fmt = new DecimalFormat(".00");
	for (double d : doubles) {
		System.out.println(fmt.format(d));
	}
}
This article has been dead for over six months. Start a new discussion instead.