Searching for an element x in a matrix.


import java.util.*;

 

public class TwoDArrays {

   public static void main(String args[]) {

       Scanner sc = new Scanner(System.in);

       int rows = sc.nextInt();

       int cols = sc.nextInt();

 

       int[][] numbers = new int[rows][cols];

 

       //input

       //rows

       for(int i=0; i

           //columns

           for(int j=0; j

               numbers[i][j] = sc.nextInt();

           }

       }

 

       int x = sc.nextInt();

 

       for(int i=0; i

           for(int j=0; j

               //compare with x

               if(numbers[i][j] == x) {

                   System.out.println("x found at location (" + i + ", " + j + ")");

               }

           }

       }

  }

}



Share to whatsapp

More Questions from Java Basic Codes Module 0

Write an infinite loop using do while condition.


View

Two numbers are entered by the user, x and n. Write a function to find the value of one number raised to the power of another i.e. x^n.


View

Print the spiral order matrix as output for a given matrix of numbers.


View

Write a function that takes in the radius as input and returns the circumference of a circle.


View

Take an array of names as input from the user and print them on the screen.


View

Write a function which takes in 2 numbers and returns the greater of those two.


View

Write a function to calculate the product of 2 numbers.


View