@AlephZero/

2DArrayHomework

Java

No description

fork
loading
Files
  • Main.java
Main.java
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
class Main {
  public static void main(String[] args) {
    System.out.println("Hello world!");
  }

  // precondition: arr is a square matrix
  // for all i, sum of ith row + sum of ith col == arr[i][i] ** 2
  public static void isSpecial(int[][] arr) {
    boolean correct = true;
    for (int i = 0; i < arr.length; i++) {
      int sum = 0;
      for (int j = 0; j < arr.length; j++) {
        sum += arr[i][j];
        sum += arr[j][i];
      }
      if (sum != arr[i][i] * arr[i][i]) {
        correct = false;
        System.out.println("Incorrect row: " + i);
      }
    }
    if (correct) {
      System.out.println("All rows are correct");
    }
  }

  public static int maxRow(int[][] arr, int row) {
    int max = arr[row][0];
    for (int i = 0; i < arr[row].length; i++) {
      if (arr[row][i] > max) {
        max = arr[row][i];
      }
    }
    return max;
  }

  public static int maxCol(int[][] arr, int col) {
    int max = arr[0][col];
    for (int i = 0; i < arr.length; i++) {
      if (arr[i][col] > max) {
        max = arr[i][col];
      }
    }
    return max;
  }

  public static void printMaxRowAndCol(int[][] arr) {
    for (int i = 0; i < arr.length; i++) {
      for (int j = 0; j < arr[i].length; j++) {
        if (maxRow(arr, i) == arr[i][j] && maxCol(arr, j) == arr[i][j]) {
          System.out.println("Row: " + i + " Column: " + j + " Value: " + arr[i][j]);
        }
      }
    }
  }
}