Advertisement
BetinaUKTC

matrix search

May 17th, 2020
1,091
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.89 KB | None | 0 0
  1. import java.util.Scanner;
  2.  
  3. public class main {
  4.  
  5.     public static void main(String[] args) {
  6.         Scanner scan = new Scanner(System.in);
  7.         int target = scan.nextInt();
  8.         int[][] arr = {
  9.                 {2, 3, 4},
  10.                 {5, 3, 7},
  11.                 {8, 3, 3},
  12.         };
  13.         boolean found= false;
  14.         for (int row = 0; row < arr.length; row++) {
  15.             for (int col = 0; col < arr[0].length; col++) {
  16.                 if (target == arr[row][col] && found == true){
  17.                     System.out.printf(", [%d][%d]",row,col);
  18.                 }
  19.                 if (target == arr[row][col] && found == false) {
  20.                     System.out.printf("Index is: [%d][%d]",row, col);
  21.                     found = true;
  22.                 }
  23.             }
  24.         }
  25.         if (found == false){
  26.             System.out.println("Not found");
  27.         }
  28.     }
  29. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement