Problem with matrix multiplication programme

Associate
Joined
19 Nov 2008
Posts
412
Location
carnmoney outside Belfast
can anyone help me with this programme?

Code:
 import java.io.*;
 import java.util.Scanner;

public class Practical4
{


   public static void main(String[] args)
   	{
	//NOTE java stored as [row][column]
   	int optionSelection, arraySelection1, arraySelection2;
   	int setuparray1 [][] = new int [][] {{7, 8},{3, 1} };
   	int setuparray2 [][] = new int [][] {{4, 5},{6, 9} };
   	int setuparray3 [][] = new int [][] {{4, 6, 3},{-1,9,-5}};

	optionSelection = getScannerInput.anInt("Please Select the number of what would you like to do?\n1 = Addition,\n2 = Scalar Multiplication,\n3 = Matrix Product\n4 = Print Array\n\n");

		//Option for Matrix addition selected
		if(optionSelection == 1){
		arraySelection1 = getScannerInput.anInt("\nWhat is the first matrix would you like to select?(1,2 or 3)\n");
		arraySelection2 = getScannerInput.anInt("What is the Second matrix would you like to select?(1,2 or 3)\n");

			//Can be down with a switch statement but due to time I went with a simple if-else for each case
			//Combinations needed are 1-2, 1-3, 2-3 and the reverse order being passed so 6 statements for each
			//combination and one for error checking
			if (arraySelection1 == 1 && arraySelection2 == 2){
			matrixAddition(setuparray1, setuparray2);
			}

			else if (arraySelection1 == 2 && arraySelection2 == 1){
			matrixAddition(setuparray2, setuparray1);
			}

			else if (arraySelection1 == 1 && arraySelection2 == 3){
			matrixAddition(setuparray1, setuparray3);
			}

			else if (arraySelection1 == 3 && arraySelection2 == 1){
			matrixAddition(setuparray3, setuparray1);
			}

			else if (arraySelection1 == 2 && arraySelection2 == 3){
			matrixAddition(setuparray2, setuparray3);
			}

			else if (arraySelection1 == 3 && arraySelection2 == 2){
			matrixAddition(setuparray3, setuparray2);
			}

			else{
			System.out.println("not a valid option");
			}
		}//end of option 1, Addition

		//Option 2 Scalar Multiplication
		if(optionSelection == 2){
		arraySelection1 = getScannerInput.anInt("What is the matrix would you like to select? (1,2 or 3)\n");
		arraySelection2 = getScannerInput.anInt("What factor would you like to scale it by?\n");

			//all 3 arrays can be passed. Order is already controlled by programme so only an option to pass each array
			//needs to be taken into account.
			if (arraySelection1 == 1){
			scalarMultiplication(arraySelection2,setuparray1);
			}

			else if (arraySelection1 == 2){
			scalarMultiplication(arraySelection2,setuparray2);
			}

			else if (arraySelection1 == 3){
			scalarMultiplication(arraySelection2,setuparray3);
			}

		}//end of option 2 Scalar Multiplication

		// Option 3, Matrix product
		if(optionSelection == 3){
		arraySelection1 = getScannerInput.anInt("What is the first matrix would you like to select? (1,2 or 3)\n");
		arraySelection2 = getScannerInput.anInt("What is the Second matrix would you like to select? (1,2 or 3)\n");


			//This is the same as the addition for the combinations that can be passed. Again a switch statement could
			// of been used.
			//Combinations needed are 1-2, 1-3, 2-3 and the reverse order being passed so 6 statements for each
			//combination and one for error checking
			if (arraySelection1 == 1 && arraySelection2 == 2){
			matrixProduct(setuparray1, setuparray2);
			}

			else if (arraySelection1 == 2 && arraySelection2 == 1){
			matrixProduct(setuparray2, setuparray1);
			}

			else if (arraySelection1 == 1 && arraySelection2 == 3){
			matrixProduct(setuparray1, setuparray3);
			}

			else if (arraySelection1 == 3 && arraySelection2 == 1){
			 matrixProduct(setuparray3, setuparray1);
			}

			else if (arraySelection1 == 2 && arraySelection2 == 3){
			 matrixProduct(setuparray2, setuparray3);
			}

			else if (arraySelection1 == 3 && arraySelection2 == 2){
			 matrixProduct(setuparray3, setuparray2);
			}

			else{
			System.out.println("not a valid option");
			}
		}//end of option 3, Matrix Product

		if(optionSelection == 4){
		arraySelection1 = getScannerInput.anInt("What is the matrix would you like to select? (1,2 or 3)\n");

			if (arraySelection1 == 1){
			printArray(setuparray1);
			}

			else if (arraySelection1 == 2){
			printArray(setuparray2);
			}

			else if (arraySelection1 == 3){
			printArray(setuparray3);
			}
		}
	}



public static void matrixAddition( int[][] a, int[][] b) //a method that implements matrix addition
  	{


	int RowSizeA = a.length;
	int RowSizeB = b.length;
	int ColumnSizeA = a[0].length;
	int ColumnSizeB = b[0].length;

	int [][]c;
	c = new int [RowSizeA][ColumnSizeA];


		//Compare array length
		if ((RowSizeA == RowSizeB)&&(ColumnSizeA == ColumnSizeB)){

			//Carry out addition
			for (int column = 0; column < 2; column++){
				for (int row = 0; row < 2; row++){
					c[row][column] = a[row][column] + b[row][column];
				}
			}

		}
		else{

			System.out.println("These cannot be added");
			return;
		}

	printArray(c);
	return;
	}
	
public static void scalarMultiplication( int factor, int[][] a) //a method that implements scalar multiplicaton
  	{
  		int RowSizeA = a.length;
		int ColumnSizeA = a[0].length;

		int [][]c;
		c = new int[RowSizeA][ColumnSizeA] ;

		for (int column = 0; column < RowSizeA; column++){
				for (int row = 0; row < ColumnSizeA; row++){
					c[row][column] = a[row][column] * factor;
				}
		}

		printArray(c);
	return;
  	}
  	
public static void matrixProduct( int[][] a, int[][] b)  //a method that implements matrix multiplicaton
  	{

	int total = 0,ProductOfTerms;
	int RowSizeA = a.length;
	int RowSizeB = b.length;
	int ColumnSizeA = a[0].length;
	int ColumnSizeB = b[0].length;

	int [][]c;
	c = new int[RowSizeA][ColumnSizeB];

		if (ColumnSizeA == RowSizeB){

			//Go along the row in matrix one and down the column of matrix two adding the results.
			//Then Go along the row again adding the results for the second column of Matrix B
			//Repeat for Row two of matrix A for all rows
			//This is done by:-
			//ColumnMatrixOne is used to select RowMatrixTwo (inner loop)
			//ColumnMatrixTwo selects the next column of Matrix 2
			//RowMatrixOne is used to select the next row of matrix 1 after all column terms in second matrix are totaled

			for (int RowMatrixOne = 0; RowMatrixOne < a.length; RowMatrixOne++){
				for(int ColumnMatrixTwo = 0; ColumnMatrixTwo < b[0].length; ColumnMatrixTwo++){
					for (int ColumnMatrixOne = 0; ColumnMatrixOne < a[0].length; ColumnMatrixOne++){
						ProductOfTerms = a[RowMatrixOne][ColumnMatrixOne] * b[ColumnMatrixOne][ColumnMatrixTwo];
						total = total + ProductOfTerms;
					}
				c[RowMatrixOne][ColumnMatrixTwo] = total;
				total = 0;
				}

			}

			printArray(c);

		}
		else {
			System.out.println("These matrix can't be multiplied together");
			return;//need to return to main

		}
	return;
  	}

public static void printArray(int [][] c) //prints the contents of a 2D array
  	{
		System.out.println("\n");

		for(int row = 0; row < c.length; row++){


			for(int column = 0; column < c[0].length; column++){
			System.out.println(c[row][column]);

			}

		}

	return;
  	}

}

The Programme pretty much works unless I choose to do scalar Multiplication on the 2x3 matrix/array. I then get the error QUOTE]Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 2
at Practical4.printArray(Practical4.java:254)
at Practical4.main(Practical4.java:127)[/CODE]

the second problem was when using this code to get the output as
a b c
d e f

Code:
public static void printArray(int [][] c) //prints the contents of a 2D array
  	{
		System.out.println("\n");

		int arrayColumnSize = c[0].length;

		if (arrayColumnSize == 1){
			for(int row = 0; row < c.length; row++){

				System.out.println(c[row][1]);

			}
		}

		if (arrayColumnSize == 2){
			for(int row = 0; row < c.length; row++){

				System.out.println(c[row][1] + "," + c[row][2]);

			}
		}

		if (arrayColumnSize == 3){
			for(int row = 0; row < c.length; row++){

				System.out.println(c[row][1] + "," + c[row][2] + "," + c[row][3]);

			}
		}

		

	return;
  	}

I get a similar error only when trying to print, add, multiply anything

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 2
at Practical4.printArray(Practical4.java:256)
at Practical4.main(Practical4.java:131)

can anyone help me get this to work?
 
Last edited:
You're trying to read from indices that don't exist in the array. In the scalarMultiplication function you are mixing up your rows and columns. In the second printArray function, you're starting your index at 1 instead of 0. Should be:

Code:
System.out.println(c[row][0] + "," + c[row][1] + "," + c[row][2]);
 
You're trying to read from indices that don't exist in the array. In the scalarMultiplication function you are mixing up your rows and columns. In the second printArray function, you're starting your index at 1 instead of 0. Should be:

Code:
System.out.println(c[row][0] + "," + c[row][1] + "," + c[row][2]);

ffs >.> I keep telling my friends not to do that when working with arrays. *facepalm*

thank you so much!! Ill ammend and see what happenes now.

EDIT: The programme works now but as before it doesnt like getting the 2 x 3 matrix passed to the scalar multiplication.

EDIT2: Found the second error, I mixed the row and column size up like you said.

thank you so much!!
Very stupid noobish errors I should have caught tbh =/
 
Last edited:
Back
Top Bottom