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

Enter 3 numbers from the user & make a function to print their average.


View

Take an array of Strings input from the user & find the cumulative (combined) length of all those strings.


View

Input a string from the user. Create a new string called ‘result’ in which you will replace the letter ‘e’ in the original string with letter ‘i’. 

Example : 

original = “eabcdef’ ; result = “iabcdif”

Original = “xyz” ; result = “xyz”


View

For a given matrix of N x M, print its transpose in java.


View

Write a function that calculates the Greatest Common Divisor of 2 numbers.


View

Write a function to multiply 2 numbers.


View

Reverse a String (using StringBuilder class) in java.


View