public
Last active

  • Download Gist
Matrix
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191
/**
* @(#)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);
}
}

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.