Pages

Monday, 16 December 2019

Program on address calculation in Two Dimensional array

Program on address calculation in Two Dimensional array
import java.util.*;

class tda
{
 public static void main(String args[])
 {
  int item,m,n;
  Scanner s = new Scanner (System.in);
  System.out.print( " Enter the size of the row : ");
  m=s.nextInt();
  System.out.print( " Enter the size of the col : ");
  n=s.nextInt();
  //int[] a; // declaration
  //a=new int[n];// creation

  int[][] a= new int[m][n]; // declaration and creation
  System.out.printf("\n Enter %d Elements ",m*n);
  for(int i=0;i<m;i++)
   {
    for(int j=0;j<n;j++)
     {
      System.out.printf("\n Elements a[%d][%d] =",i,j);
      a[i][j]=s.nextInt();
     }
   }

  System.out.print("\n Array elements : ");
  for(int i=0;i<m;i++)
   {
    for(int j=0;j<n;j++)
     {
      System.out.print( a[i][j] + " ");
     }
   }

  for(int i=0;i<m;i++)
   {
    for(int j=0;j<n;j++)
     {
      System.out.printf("\nArray = a[%d][%d] ---- Data = %d",i,j,a[i][j]);
     }
   }
  System.out.print("\n Enter the Array element which u want : ");
  item=s.nextInt();
  for(int i=0;i<m;i++)
   {
   for(int j=0;j<n;j++)
    {
    if(item == a[i][j])
      {
       System.out.printf("\nArray = a[%d][%d] ---- Data =   %d",i,j,a[i][j]);
      }
    }
  }
 }
}
output:

No comments:

Post a Comment

Constructors & Destructors in c++

  Constructors :  A Constructor is a special member function, which is used to initialize the objects of its class. The Constructor is invok...