Multidimensional arrays & Functions

Lecture 5 - Multidimensional arrays & Functions

Functions

we can pass to a functions either by:

  • reference

  • value

Here we pass by value

public class Main {
    public static void printMe(int value_of_num) {
        System.out.println(value_of_num);
        value_of_num = 99;
    }


    public static void main(String[] args) {
        int num = 5;
        printMe(num);
        printMe(num);
    }
}
5
5

The function is void since it doesn't return a value

also here we pass by value

public class Main {
    public static int printMe(int value_of_num) {
        System.out.println(value_of_num);
        value_of_num = 99;
        return value_of_num;
    }


    public static void main(String[] args) {
        int num = 5;
        int x = printMe(num);
        int y = printMe(x);
    }
}
5
99

The function is int since we return a value

Exercise: Palindrome

public class Main {
    public static boolean palindrom(int [] arr){
        for (int i = 0; i < arr.length/2; i++) {
            if (arr[i]!=arr[arr.length-1-i])
                return false;
        }
        return true;
    }


    public static void main(String[] args) {
        int[] arr = {1,2,3,2,1};
        boolean ans = palindrom(arr);
        System.out.println(ans);

        int[] arr2 = {1,3,3,2,1};
        boolean ans2 = palindrom(arr2);
        System.out.println(ans2);
    }
}
true
false

Multidimensional arrays

public class Main {
    public static void main(String[] args) {
        int[][] a = {
                {1, 2, 3},
                {4, 5, 6, 9},
                {7},
        };

        for (int i = 0; i < a.length; i++) {
            System.out.print(a[i].length + " ");
        }
        //error out of bounds
        //a[0][3] = 10;
    }
}

Each array can be a different size

Exercise: print the matrix and its sum

package com.company;

import java.util.Scanner;

public class Main {
    public static int sumArr2(int [][]arr2) {
        int sum = 0;

        for (int i = 0; i < arr2.length; i++) {
            for (int j = 0; j < arr2[i].length; j++) {
                System.out.print(arr2[i][j] + " ");
                sum += arr2[i][j];
            }
            System.out.println();
        }
        return sum;
    }


    public static void main(String[] args) {
        int n = 2;
        int m = 4;

        int[][] arr = new int[n][m];

        for (int i = 0; i < n; i++) {
            for (int j = 0; j < m; j++) {
                arr[i][j] = (i*m)+j;
            }
        }

        int sum_of_matrix = sumArr2(arr);
        System.out.println(sum_of_matrix);

    }
}

Explanation:

here

public class Main {
    public static int sumArr2(int [][]arr2) {
        int sum = 0;
         ...
        return sum;
    }


    public static void main(String[] args) {
        int n = 2, m = 4;
        int[][] arr = new int[n][m];
         ...
        int sum_of_matrix = sumArr2(arr);
    }
}

arr is passed by

  • reference

  • and not by value

Exercise: Trace of matrix

import java.util.Scanner;

public class Main {
    public static int trace(int [][]a2) {
        int sum = 0;

        //check if matrix isn't nxn
        //if matrix is n x m, and n!=m then return -1
        if (a2.length!=a2[0].length)
            return -1;

        for (int i = 0; i < a2.length; i++) {
            for (int j = 0; j < a2[i].length; j++) {
                System.out.print(a2[i][j] + " ");
                if( i == j)
                    sum += a2[i][j];
            }
            System.out.println();
        }
        return sum;
    }


    public static void main(String[] args) {
        int n = 3;
        int m = 3;

        int[][] arr = new int[n][m];

        //create matrix:
        // 0 1 2 
        // 3 4 5 
        // 6 7 8 
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < m; j++) {
                arr[i][j] = (i*m)+j;
            }
        }
        System.out.println(trace(arr));

    } 
}

Exercise: Transpose

public class Main {
    public static int [][] transpose(int [][]a2) {
        int[][] n_arr = new int[a2[0].length][a2.length];


        for (int i = 0; i < a2.length; i++) {
            for (int j = 0; j < a2[i].length; j++) {
                System.out.print(a2[i][j] + " ");
                n_arr[j][i] = a2[i][j];
            }
            System.out.println();
        }
        //completed all
        return n_arr;
    }


    public static void main(String[] args) {
        int n = 3;
        int m = 3;

        int[][] arr = {{1, 0, 5}, {0, 2, 1}, {4, 1, 3}};


        int[][] n_arr = transpose(arr);
    }
}

Exercise: Check Symmetry of matrix

import java.util.Scanner;

public class Main {
    public static boolean checkSymmetry(int [][]a2) {
        boolean flag = true;

        if (a2.length!=a2[0].length)
            return false;

        for (int i = 0; i < a2.length; i++) {
            for (int j = 0; j < a2[i].length; j++) {
                System.out.print(a2[i][j] + " ");
                if( a2[i][j] != a2[j][i])
                    flag = false;
            }
            System.out.println();
        }
        //completed all
        return flag;
    }


    public static void main(String[] args) {
        int n = 3;
        int m = 3;

        int[][] arr = {{1, 0, 4}, {0, 2, 1}, {4, 1, 3}};

        System.out.println(checkSymmetry(arr));
    }
}

Matrix Multiplication

public class Main {
    public static void printMat(int[][] mat){
        for (int i = 0; i < mat.length; i++){
            for (int j = 0; j < mat[i].length; j++)
                System.out.print(mat[i][j] + " ");
            System.out.println();
        }
    }

    public static int[][] matMul(int[][] a, int[][] b) {
        //if error, return this
        if(a[0].length!=b.length){
            System.out.println("error, dims are not compatible");
            return new int[][]{{-1}};
        }

        //start here
        int[][] result = new int[a.length][b[0].length];

        for (int row = 0; row < result.length; row++) {
            for (int col = 0; col < result[row].length; col++) {
                result[row][col] = matMulCell(a, b, row, col);
            }
        }
        return result;
    }

    public static int matMulCell(int[][] a, int[][] b, int row, int col) {
        int cell = 0;
        for (int i = 0; i < b.length; i++)
            cell += a[row][i] * b[i][col];

        return cell;
    }

    public static void main(String[] args) {
        int[][] mat1 = {
                {1, 2, 3},
                {4, 5, 6},
        };

        int[][] mat2 = {
                {10, 11},
                {20, 21},
                {30, 31},
        };

        int[][] res = matMul(mat1, mat2);

        printMat(mat1);
        System.out.println("** times **");
        printMat(mat2);
        System.out.println("== equals ==");
        printMat(res);

    }
}
1 2 3 
4 5 6 
** times **
10 11 
20 21 
30 31 
== equals ==
140 146 
320 335

Last updated