diff --git a/labs/2/Matrix.java b/labs/2/Matrix.java new file mode 100644 index 0000000..76d9482 --- /dev/null +++ b/labs/2/Matrix.java @@ -0,0 +1,63 @@ +public class Matrix { + private int[][] m; + + public void init(int[][] base_m) { + this.m = base_m; + } + + public int[][] getMatrix() { + return this.m; + } + + public void print() { + for (int[] ml : this.m) { + for (int e : ml) { + System.out.printf("%4d ", e); + } + System.out.println(); + } + } + + public void transpose() { + print(); + if (this.m[0].length != this.m.length) { + int new_h = this.m[0].length; + int new_w = this.m.length; + + int[][] new_m = new int[new_h][new_w]; + + for (int i = 0; i < this.m.length; i++) { + for (int j = 0; j < this.m[i].length; j++) { + new_m[j][i] = this.m[i][j]; + } + } + + this.m = new_m; + } else { + // if a matrix is square, there is no need to + // make a new one + + int temp_value_holder; + + for (int i = 0; i < this.m.length; i++) { + for (int j = 0; j < i; j++) { + temp_value_holder = this.m[i][j]; + this.m[i][j] = this.m[j][i]; + this.m[j][i] = temp_value_holder; + } + } + } + } + + public double getAvg() { + double total = 0; + + for (int[] ml : this.m) { + for (int e : ml) { + total += e; + } + } + + return total / (this.m.length * this.m[0].length); + } +}