Skip to content

@Eabryt /Matrix
Last active

Embed URL

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
/**
* @(#)matrixoperations.java
*
*Description: Fills 2 5x5 arrays with ranom values 1-100. Prints out the arrays, adds the arrays together and places the answer in a third array. Multiplies the first array by 2 and prints out the answer.
* Multiplies the 2nd array by 3 and displays the answer. Finally multiplys the first array by the second array and displays the answer in the third array.
*
* @author
* @version 1.00 2013/2/28
*/
import java.util.Random;
import java.util.Scanner;
public class matrix {
public static void populate(int MatrixA[][], int MatrixB[][], int MatrixC[][])
{
Random ranGen = new Random();
for(int i=0;i<5; i++)
{
for(int x=0;x<5;x++)
{
MatrixA[i][x] = ranGen.nextInt(10)+1; //populates the first 2d array with numbers 1-100
MatrixB[i][x] = ranGen.nextInt(10)+1; //populates the second 2d array with numbers 1-100
MatrixC[i][x]=0;
}
}
}
public static void arrayOutput(int MatrixA[][], int MatrixB[][], int MatrixC[][]) //outputs the arrays as matrices
{
Scanner textReader = new Scanner(System.in);
System.out.println("Which array do you want to output? 1 for Matrix A, 2 for Matrix B, 3 for Matrix C");
int choose = textReader.nextInt();
switch(choose)
{
case 1:
System.out.println("Matrix A");
for(int z=0;z<5; z++) //outputs Matrix A
{
System.out.println("");
for(int a=0;a<5;a++)
{
System.out.print(MatrixA[z][a]);
System.out.print(" ");
}
}
break;
case 2:
System.out.println("Matrix B"); //outputs Matrix B
for(int b=0;b<5; b++)
{
System.out.println("");
for(int r=0;r<5;r++)
{
System.out.print(MatrixB[b][r]);
System.out.print(" ");
}
}
break;
case 3:
System.out.println("Matrix C"); //outputs Matrix C
for(int b=0;b<5; b++)
{
System.out.println("");
for(int r=0;r<5;r++)
{
System.out.print(MatrixC[b][r]);
System.out.print(" ");
}
}
break;
}
}
public static void addition(int MatrixA[][], int MatrixB[][], int MatrixC[][]) //adds the 2 matrices together and places them in Matrix C
{
for(int i=0; i<5;i++)
{
for(int x = 0; x<5; x++)
{
MatrixC[i][x]=MatrixA[i][x] + MatrixB[i][x];
}
}
}
public static void multiplyA(int MatrixA[][]) //Multiplies Matrix A by 2
{
for(int i=0; i<5; i++)
{
for(int x=0; x<5;x++)
{
MatrixA[i][x]=MatrixA[i][x]*2;
}
}
System.out.println("Done");
}
public static void multiplyB(int MatrixB[][]) //Multiplies Matrix B by 3
{
for(int i=0; i<5; i++)
{
for(int x=0; x<5;x++)
{
MatrixB[i][x]=MatrixB[i][x]*3;
}
}
}
public static void multiplyC(int MatrixA[][], int MatrixB[][], int MatrixC[][]) //Multiplies Matrix A by Matrix B and saves in Matrix C
{
int i, x;
//int add = 0;
int sum=0;
for(i=0;i<5;i++)
{
for(x=0;x<5;x++)
{
for(int z=0; z<5;z++)
{
sum+=(MatrixA[i][z]*MatrixB[z][i]);
MatrixC[i][x] = sum;
sum=0;
}
}
sum=0;
}
}
public static void swap(int MatrixA[][],int MatrixC[][])
{
for(int i=0; i<5;i++)
{
for(int x=0; x<5;x++)
{
}
}
MatrixA[0][0]=MatrixA[0][4];
MatrixC[1][1]=MatrixA[1][3];
MatrixC[2][2]=MatrixA[2][2];
MatrixC[3][3]=MatrixA[1][1];
MatrixC[4][4]=MatrixA[0][0];
}
public static void main(String [] args)
{
Scanner textReader = new Scanner(System.in);
boolean run=true;
int[][] MatrixA = new int [5][5]; //creates a 2d array with 5 rows and 5 columns
int[][] MatrixB = new int [5][5]; //creates another 2d array with 5 rows and 5 columns
int[][] MatrixC = new int [5][5];
int option;
do{
System.out.println("\nPress 1 to populate the arrays.\nPress 2 to output the arrays.\nPress 3 to add MatrixA and MatrixB.\nPress 4 to Multiply Matrix A by 2.\nPress 5 to multiply MatrixB by 3.\nPress 6 to multiply Matrix A by Matrix B.\nPress 7 to swap the diagonals in MatrixA.\nPress 8 to exit the program.");
option =textReader.nextInt();
switch(option)
{
case 1:
populate(MatrixA, MatrixB, MatrixC);
break;
case 2:
arrayOutput(MatrixA, MatrixB, MatrixC);
break;
case 3:
addition(MatrixA, MatrixB, MatrixC);
break;
case 4:
multiplyA(MatrixA);
case 5:
multiplyB(MatrixB);
case 6:
multiplyC(MatrixA, MatrixB, MatrixC);
break;
case 7:
swap(MatrixA, MatrixC);
break;
case 8:
run=false;
break;
}
}while(run==true);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.