Quantcast
Channel: Programming Forums
Viewing all articles
Browse latest Browse all 51036

Print Matrices Side By Side

$
0
0
Current Code:
import java.util.Scanner;
public class Prog7i5 {
public static void main(String[] args) {
	
	//Declare matrices
	double a[][] = new double[3][3];
	double b[][] = new double[3][3];
	
	//Ask for user input for matrix a
	Scanner input = new Scanner(System.in);
	System.out.println("Enter matrix a: ");
	//Create matrix a
	for(int row = 0; row < a.length; row++) {
		for(int column = 0; column < a[0].length; column++) {
			a[row][column] = input.nextDouble();
		}
	}
	
	//Ask for user input for matrix b
	System.out.println("Enter matrix b: ");
	//Create matrix b
	for(int row = 0; row < b.length; row++) {
		for(int column = 0; column < a[0].length; column++) {
			b[row][column] = input.nextDouble();
		}
	}
	
	//Print title
	System.out.println("The matrices are added as follows.");
	
	//Print matrix a
	for(int row = 0; row < a.length; row++) {
		for(int column = 0; column < a[0].length; column++) {
			System.out.print(a[row][column] + " ");
		}
		
	System.out.println();
	}
	
	//Print +
	System.out.println(" ");
	System.out.println("+");
	System.out.println(" ");
	
	//Print matrix b
	for(int row = 0; row < b.length; row++) {
		for(int column = 0; column < b[0].length; column++) {
			System.out.print(b[row][column] + " ");
		}
		
	System.out.println();
	}
	
	//Print =
	System.out.println(" ");
	System.out.println("=");
	System.out.println(" ");
	
	//Print matrix c
	for(int row = 0; row < addMatrix(a, B)/>/>.length; row++) {
		for(int column = 0; column < addMatrix(a, B)/>/>[0].length; column++) {
			System.out.print(addMatrix(a, B)/>/>[row][column] + " ");
		}
		
	System.out.println();
	}
}

public static double[][] addMatrix(double[][] a, double[][] B)/>/> {
	
	//Declare matrix c
	double c[][] = new double[3][3];
	
	//Add matrices
	for(int row = 0; row < c.length; row++) {
		for(int column = 0; column < c[0].length; column++) {
			c[row][column] = a[row][column] + b[row][column];
		}
	}
	
	return c;
}
}


Result:
Enter matrix a: 
1 2 3 4 5 6 7 8 9
Enter matrix b: 
0 2 4 1 4.5 2.2 1.1 4.3 5.2
The matrices are added as follows.
1.0 2.0 3.0 
4.0 5.0 6.0 
7.0 8.0 9.0 
 
+
 
0.0 2.0 4.0 
1.0 4.5 2.2 
1.1 4.3 5.2 
 
=
 
1.0 4.0 7.0 
5.0 9.5 8.2 
8.1 12.3 14.2


How I want it to look:
1.0 2.0 3.0   0.0 2.0 4.0   1.0 4.0 7.0
4.0 5.0 6.0 + 1.0 4.5 2.2 = 5.0 9.5 8.2
7.0 8.0 9.0   1.1 4.3 5.2   1.1 4.3 5.2


Time and patience is appreciated! :)/>

UDATE: Some of the formatting acted weird when I copy/pasted from Eclipse. But the program itself is working and printing the matrices vertically. :)

Viewing all articles
Browse latest Browse all 51036

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>