3 Write a small code snippet that will give the two indices

3. Write a small code snippet that will give the two indices of a 2D array given the numbered element in the array. For instance, imagine we have this 2D array and I wanted the 5th element. The indices (ij) to get that element would be i 1 and j 1 thus A[1][1]. Similarly the 8th element would be i 1 and j 2 thus AL1][2]. HINT: Division and modulus are your best friends. in all

Solution

import java.util.Scanner;
public class test {
public static void main(String[] args) {
System.out.println(\"Hello, World!\");
      
       int[][] arr=new int[100][100];
       int row, col, i, j, element;
      
       Scanner in = new Scanner(System.in);
      
       System.out.println(\"Enter the size of matrix (maximum 100 x 100)\ \");
       row = in.nextInt();
       col = in.nextInt();
      
       System.out.println(\"\ Enter values of the matrix\ \");
       for(i = 0; i< row; i++)
           for(j = 0; j < col; j++)
           {
               arr[i][j] = in.nextInt();
               System.out.println(\"\ \");
           }
      
       System.out.println(\"\ The matrix is as follows\ \");
       for(i = 0; i< row; i++)
       {
           for(j = 0; j < col; j++)
           {
               System.out.print(arr[i][j] + \"\\t\");
           }
           System.out.println(\"\ \");
       }
          
       System.out.println(\"\ Enter the element whose indices need to be known\ \");
       element = in.nextInt();
       for(i = 0; i< row; i++)
           for(j = 0; j < col; j++)
           {
               if(arr[i][j] == element)
               System.out.println(\"\ Indices of the \" + element + \" are i = \" + i + \" and j = = \" + j + \"\ A[\" + i + \"][\" + j + \"]\");
           }
}
}

 3. Write a small code snippet that will give the two indices of a 2D array given the numbered element in the array. For instance, imagine we have this 2D array

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site