Print Diagonals of 2 Dimensional Array

package org.wesome.dsalgo;

public class PrintDiagonals {
    public static void printDiagonals(int[][] arr, int indx) {
        for (int row = 0; row < arr.length; row++) {
            for (int col = 0; col < arr[row].length; col++) {
                if ((row + col == indx) || (row == col)) {
                    System.out.print(arr[row][col] + " ");
                } else {
                    System.out.print(" _ ");
                }
            }
            System.out.println("\n");
        }
    }
}
package org.wesome.dsalgo;

import org.junit.jupiter.api.Test;

import static org.wesome.dsalgo.PrintDiagonals.printDiagonals;

public class PrintDiagonalsTest {
    @Test
    void printDiagonalsTest1() {
        int[][] arr = new int[][]{{1, 2, 3}, {4, 5, 6}, {7, 8, 9}};
        int indx = 2;
        printDiagonals(arr, indx);
    }

    @Test
    void printDiagonalsTest2() {
        int[][] arr = new int[][]{{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}};
        int indx = 4;
        printDiagonals(arr, indx);
    }
}
plugins {
    id 'java'
    id "io.freefair.lombok" version "6.2.0"
}

group = 'org.wesome'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = JavaVersion.VERSION_1_8

configurations {
    compileOnly {
        extendsFrom annotationProcessor
    }
}

repositories {
    mavenCentral()
}

dependencies {
    testImplementation 'org.junit.jupiter:junit-jupiter:5.6.2'
}

test {
    useJUnitPlatform()
}

 

follow us on