9 Replies - 923 Views - Last Post: 14 November 2012 - 08:42 PM Rate Topic: -----

#1 Brovahkiin501  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 48
  • Joined: 01-October 12

Java Program to Add Matrices

Posted 13 November 2012 - 09:35 PM

Hi everyone!

I'm trying to write a code to add two matrices together. I think I have most of the program correct but can somebody help me figure out how to print out the result? Thank you very much!

import java.util.Arrays;
import java.util.Scanner;
public class MatrixAddition {
	public static void main(String[] args) {
		Scanner input = new Scanner(System.in);
		final int N = 3;
		System.out.print("Enter matrix 1: ");
		double[][] matrix1 = new double[N][N];
		for (int i = 0; i < matrix1.length; i++)
			for (int j = 0; j < matrix1[i].length; j++) {
				matrix1[i][j] = input.nextDouble();
			}
		System.out.print("Enter matrix 2: ");
		double[][] matrix2 = new double[N][N];
		for (int i = 0; i < matrix1.length; i++)
			for (int j = 0; j < matrix1[i].length; j++) {
				matrix2[i][j] = input.nextDouble();
				
		
			}
	}
	

	
	public static double[][] addMatrix(double[][] a, double[][] B)/>/> {
		double[][] sumOfMatrices = new double[a.length][b[0].length];
			for (int i = 0; i < a.length; i++) {
				for (int j = 0; j < sumOfMatrices.length; j++)
					for (int k = 0; k < sumOfMatrices[0].length; k++)
					sumOfMatrices[i][j] = a[i][j] + b[i][j];
				
			}
			return sumOfMatrices;
		
		
		
	}

}




Is This A Good Question/Topic? 0
  • +

Replies To: Java Program to Add Matrices

#2 WolverineX  Icon User is offline

  • New D.I.C Head

Reputation: 5
  • View blog
  • Posts: 49
  • Joined: 11-November 12

Re: Java Program to Add Matrices

Posted 13 November 2012 - 10:00 PM

Just do System.out.println(sumOfMatrices); at the end of the code...
Was This Post Helpful? 0
  • +
  • -

#3 Brovahkiin501  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 48
  • Joined: 01-October 12

Re: Java Program to Add Matrices

Posted 13 November 2012 - 10:11 PM

I have changed my code a bit but it is still not working correctly /: Here is the new output:
Enter matrix 1: 1 2 3
1 2 3
1 2 3
Enter matrix 2: 1 2 3
[[D@19e0ff2f
[[D@19e0ff2f
[[D@19e0ff2f
[[D@29173ef, [D@1b52513a, [D@3952c4d9][[D@68f99ff5
[[D@68f99ff5
[[D@68f99ff5
[[D@5c74c3aa, [D@75d9fd51, [D@3a860d49][[D@36d251a3
[[D@36d251a3
[[D@36d251a3
[[D@29edc073, [D@37f5d386, [D@6c121f1d]



Here is my modified code as well:
import java.util.Arrays;
import java.util.Scanner;
public class MatrixAddition {
	public static void main(String[] args) {
		Scanner input = new Scanner(System.in);
		final int N = 3;
		System.out.print("Enter matrix 1: ");
		double[][] matrix1 = new double[N][N];
		for (int i = 0; i < matrix1.length; i++)
			for (int j = 0; j < matrix1[i].length; j++) {
				matrix1[i][j] = input.nextDouble();
			}
		System.out.print("Enter matrix 2: ");
		double[][] matrix2 = new double[N][N];
		for (int i = 0; i < matrix1.length; i++)
			for (int j = 0; j < matrix1[i].length; j++) {
				matrix2[i][j] = input.nextDouble();
				
		System.out.print(Arrays.toString(addMatrix(matrix2, matrix2)));
			}
	}
	

	
	public static double[][] addMatrix(double[][] a, double[][] B)/> {
		double[][] sumOfMatrices = new double[a.length][b[0].length];
			for (int i = 0; i < a.length; i++) {
				for (int j = 0; j < sumOfMatrices.length; j++)
					for (int k = 0; k < sumOfMatrices[0].length; k++)
					sumOfMatrices[i][j] = a[i][j] + b[i][j];
				System.out.println(sumOfMatrices);
			}
			return sumOfMatrices;
		
		
		
	}

}


Was This Post Helpful? 0
  • +
  • -

#4 WolverineX  Icon User is offline

  • New D.I.C Head

Reputation: 5
  • View blog
  • Posts: 49
  • Joined: 11-November 12

Re: Java Program to Add Matrices

Posted 13 November 2012 - 10:40 PM

Well if you wanna use "toString" then you have to define that method. Since Java has already a defined one, but when yo use it then it gives you this weird codes..

So make a toString like this below:

public String toString() {

return sumOfMatrices;

}
Was This Post Helpful? 0
  • +
  • -

#5 Brovahkiin501  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 48
  • Joined: 01-October 12

Re: Java Program to Add Matrices

Posted 13 November 2012 - 10:50 PM

Okay, so from reading my textbook I was able to figure out that to print a two-dimensional array, you have to print each element in the array using a loop so now I have changed my code again but there is still something wrong /:

import java.util.Arrays;
import java.util.Scanner;
public class MatrixAddition {
	public static void main(String[] args) {
		Scanner input = new Scanner(System.in);
		final int N = 3;
		System.out.print("Enter matrix 1: ");
		double[][] matrix1 = new double[N][N];
		for (int i = 0; i < matrix1.length; i++)
			for (int j = 0; j < matrix1[i].length; j++) {
				matrix1[i][j] = input.nextDouble();
			}
		System.out.print("Enter matrix 2: ");
		double[][] matrix2 = new double[N][N];
		for (int i = 0; i < matrix1.length; i++)
			for (int j = 0; j < matrix1[i].length; j++) {
				matrix2[i][j] = input.nextDouble();
				
		System.out.print(Arrays.toString(addMatrix(matrix2, matrix2)));
			}
	}
	

	
	public static double[][] addMatrix(double[][] a, double[][] B)/> {
		double[][] sumOfMatrices = new double[a.length][b[0].length];
			for (int i = 0; i < a.length; i++) {
				for (int j = 0; j < sumOfMatrices.length; j++)
					for (int k = 0; k < sumOfMatrices[0].length; k++)
					sumOfMatrices[i][j] = a[i][j] + b[i][j];
				for (int row = 0; row < a.length; row++) {
					for (int column = 0; column < a[row].length; column++) {
						System.out.print(a[row][column] + " ");
					}
					System.out.println();
				}
			}
			return sumOfMatrices;
		
		
		
	}

}


Was This Post Helpful? 0
  • +
  • -

#6 blackcompe  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1155
  • View blog
  • Posts: 2,533
  • Joined: 05-May 05

Re: Java Program to Add Matrices

Posted 13 November 2012 - 10:53 PM

You need to iterate over each entry in the array and print it.

    private void print(double[][] matrix)
    {
        for (double[] row : matrix)
        {
            for (double elem : row)
                System.out.print(elem + " ");
            System.out.println();
        }
    }



Calling System.out.println on primitive arrays prints garbage. However, the Arrays class has a method, Arrays.toString(T[] array), (see API) that calls toString on each array element and prettily prints the result.

This post has been edited by blackcompe: 13 November 2012 - 10:54 PM

Was This Post Helpful? 1
  • +
  • -

#7 Brovahkiin501  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 48
  • Joined: 01-October 12

Re: Java Program to Add Matrices

Posted 13 November 2012 - 11:32 PM

I am sorry I am having so much trouble with this...I made some changes to my code according to your suggestion but now there is an error in the line that says
 System.out.print(print(matrix)); 
. If somebody could point out what's wrong it would be much appreciated! Here is my full code:

import java.util.Arrays;
import java.util.Scanner;
public class MatrixAddition {
	public static void main(String[] args) {
		Scanner input = new Scanner(System.in);
		final int N = 3;
		System.out.print("Enter matrix 1: ");
		double[][] matrix1 = new double[N][N];
		for (int i = 0; i < matrix1.length; i++)
			for (int j = 0; j < matrix1[i].length; j++) {
				matrix1[i][j] = input.nextDouble();
			}
		System.out.print("Enter matrix 2: ");
		double[][] matrix2 = new double[N][N];
		for (int i = 0; i < matrix1.length; i++)
			for (int j = 0; j < matrix1[i].length; j++) {
				matrix2[i][j] = input.nextDouble();
				
		System.out.print(print(matrix));
			}
	}
	
	public static double[][] addMatrix(double[][] a, double[][] B)/> {
		double[][] sumOfMatrices = new double[a.length][b[0].length];
			for (int i = 0; i < a.length; i++) {
				for (int j = 0; j < sumOfMatrices.length; j++)
					for (int k = 0; k < sumOfMatrices[0].length; k++)
					sumOfMatrices[i][j] = a[i][j] + b[i][j];
				
			}
			return sumOfMatrices;
	}
	
	public static void print(double[][] matrix) {
		for (double[] row : matrix) {
			for (double elem : row)
				System.out.print(elem + " ");
			System.out.println();
		}
	}
		
		
	}





Was This Post Helpful? 0
  • +
  • -

#8 blackcompe  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1155
  • View blog
  • Posts: 2,533
  • Joined: 05-May 05

Re: Java Program to Add Matrices

Posted 13 November 2012 - 11:43 PM

You should consider using an IDE. It will point out obvious errors.

Scanner input = new Scanner(System.in);
		final int N = 3;
		System.out.print("Enter matrix 1: ");
		double[][] matrix1 = new double[N][N];
		for (int i = 0; i < matrix1.length; i++)
			for (int j = 0; j < matrix1[i].length; j++) {
				matrix1[i][j] = input.nextDouble();
			}
		System.out.print("Enter matrix 2: ");
		double[][] matrix2 = new double[N][N];
		for (int i = 0; i < matrix1.length; i++)
			for (int j = 0; j < matrix1[i].length; j++) {
				matrix2[i][j] = input.nextDouble();
				
		System.out.print(print(matrix));



In the above code, you haven't defined a variable named matrix, so it doesn't make sense to the compiler. Did you forget to multiple the matrices?
Was This Post Helpful? 1
  • +
  • -

#9 Brovahkiin501  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 48
  • Joined: 01-October 12

Re: Java Program to Add Matrices

Posted 14 November 2012 - 02:54 PM

Well right there, I'm trying to print this method
 public static void print(double[][] matrix) {
		for (double[] row : matrix) {
			for (double elem : row)
				System.out.print(elem + " ");
			System.out.println();
		}
	}
		


But I'm not sure why it's not working /:
Was This Post Helpful? 0
  • +
  • -

#10 blackcompe  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1155
  • View blog
  • Posts: 2,533
  • Joined: 05-May 05

Re: Java Program to Add Matrices

Posted 14 November 2012 - 08:42 PM

Well, if you're going to say it's not working, at least post the code, so we can see what you have? You haven't even posted an error message. Is there a compile-time error (i.e. the code won't compile), a runtime error (i.e. there's an exception thrown), or a bug (i.e. you get unexpected output)?

This post has been edited by blackcompe: 14 November 2012 - 08:42 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1