MATRIX
(2D ARRAYS)
//creating a 2D array syntax:
1. For Integer dataType:
int[][] matrix = new int[rows][cols];
2. For Decimal(float) dataType:
float[][] matrix = new float[rows][cols];
3. For Decimal(double) dataType:
double[][] matrix = new double[rows][cols];
/* take a matrix as input from the user.
Search for a given number x and print the indices at which it occurs.*/
import java.util.*;
public class Main{
public static void main(String args[]){
Scanner in = new Scanner(System.in);
System.out.println("Enter no. of rows: ");
int rows = in.nextInt();
System.out.println("Enter no. of columns: ");
int cols = in.nextInt();
int[][] numbers = new int[rows][cols];
//input
//rows
for(int i = 0; i < rows; i++){
//rows
for(int j = 0; j < cols; j++){
numbers[i][j] = in.nextInt();
}
}
System.out.println("Enter the number you want to
search: ");
int x = in.nextInt();
//output
for(int i = 0; i < rows; i++){
for(int j = 0; j < cols; j++){
if(numbers[i][j] == x){
System.out.println(x + " found at index ("
+ i + "," + j + ")" );
}
}
}
}
}