Trans Array (JAVA PROGRAM pdf)

4
Class Transarray 1/4 /* [email protected] * javaprogramsbyashking13th.blogspot.in * ashkingjava.blogspot.in * * * QUESTION * * A transpose of an array obtained by interchanging the * elements of the rows and columns. * A class Transarray contains a two dimensional integer * array of order [m*n]. The maximum value possible for * both 'm' and 'n' is 20. * * Design a class Transarray to find the transpose of a * given matrix . the details of the members are given * below : * * CLASS NAME : Transarray * * DATA MEMBERS * arr[][] : stores th matrix elements * m : integer to store the number of rows * n : integer to store the number of columns * * MEMBER FUNCTIONS * Transarray() : default constructor * Transarray(int mm,int nn) : to initialise the size of matrix, * m=mm , n=nn * void fillarray() : to enter the elements of the matrix * void transpose(transarray A) : to find the transpose of a given matrix * void disparray() : displays the array in a matrix form * * Define main() method to execute the class. */ import java.io.*; public class Transarray { int arr[][]=new int[20][20]; /* * data member to store the matrix */ int m; /* * data memebr to store number of rows in the matrix */ int n; /* * data memebr to store number of columns in the matrix */ Mar 25, 2014 3:22:33 PM

description

JAVA PROGRAM TO FIND THE TRANSPOSE OF A MATRIX (A transpose of an array obtained by interchanging the elements of the rows and columns) . WITH COMMENTS AND DETAILS TO HELP YOU UNDERSTAND THEM.THIS PROGRAM HAS BEEN RUN ON Blue J AND IS ERROR FREE.IT WILL SURELY HELP YOU IN STUDYING JAVA PROGRAMS FOR EXAMS OR OTHERWISE.

Transcript of Trans Array (JAVA PROGRAM pdf)

Page 1: Trans Array (JAVA PROGRAM pdf)

Class Transarray 1/4

/* [email protected] * javaprogramsbyashking13th.blogspot.in * ashkingjava.blogspot.in * * * QUESTION * * A transpose of an array obtained by interchangin g the * elements of the rows and columns. * A class Transarray contains a two dimensional in teger * array of order [m*n]. The maximum value possible for * both 'm' and 'n' is 20. * * Design a class Transarray to find the transpose of a * given matrix . the details of the members are gi ven * below : * * CLASS NAME : Transarray * * DATA MEMBERS * arr[][] : stores th mat rix elements * m : integer to st ore the number of rows * n : integer to st ore the number of columns * * MEMBER FUNCTIONS * Transarray() : default const ructor * Transarray(int mm,int nn) : to initialise the size of matrix, * m=mm , n=nn * void fillarray() : to enter the elements of the matrix * void transpose(transarray A) : to find the t ranspose of a given matrix * void disparray() : displays the array in a matrix form * * Define main() method to execute the class. */

import java.io.*;public class Transarray{ int arr[][]= new int [20][20]; /* * data member to store the matrix */ int m; /* * data memebr to store number of rows in the m atrix */ int n; /* * data memebr to store number of columns in th e matrix */

Mar 25, 2014 3:22:33 PM

Page 2: Trans Array (JAVA PROGRAM pdf)

Class Transarray (continued) 2/4

DataInputStream d= new DataInputStream(System.in); /* * input stram object. * YOUCAN ALSO USE BUFFERED READER */

/* * default constructor to initialise the array */ public Transarray() { for ( int i=0;i<20;i++) { for ( int j=0;j<20;j++) { arr[i][j]=0; } } } //end of default constructor Transarray()

/* * parameterised consructor to initialise size of matrix */ public Transarray( int mm, int nn) { m=mm; n=nn; } //end of parameterised constructor

void fillarray()throws IOException { for ( int i=0;i<m;i++) { for ( int j=0;j<n;j++) { System.out.println( "enter " +i+ "*" +j+ " th element" ); arr[i][j]=Integer.parseInt(d.readLi ne()); /* * inputing elements of the matrix */ } } } //end of method fillarray()

/* * method to find the transpose of the inputted array */ void transpose(Transarray A) { int arr1[][]= new int [20][20];

Mar 25, 2014 3:22:33 PM

Page 3: Trans Array (JAVA PROGRAM pdf)

Class Transarray (continued) 3/4

/* * creating a local array to store the * transpose of the matrix */ for ( int i=0;i<20;i++) { for ( int j=0;j<20;j++) { arr1[i][j]=A.arr[j][i]; /* * storing the element of original array arr with * poaition i,j at a position j,i i n the local array * i,e., creating the transpose of the matrix. */ } }

System.out.println( "transpose" ); /* * printing the transpose array i,e.,arr1 i n matrix form */ for ( int i=0;i<n;i++) { for ( int j=0;j<m;j++) { System.out.print(arr1[i][j]+ "\t" ); } System.out.println(); }

} //end of transpose(transarray A)

/* * method to display the array (original) */ void disparray() { for ( int i=0;i<m;i++) { for ( int j=0;j<n;j++) { System.out.print(arr[i][j]+ "\t" ); } System.out.println(); } }

/* * main() method to create object and call meth ods using the object * to execute the program. */

Mar 25, 2014 3:22:33 PM

Page 4: Trans Array (JAVA PROGRAM pdf)

Class Transarray (continued) 4/4

public void main() throws IOException { int mm=0; int nn=0; System.out.println( "enter no of rows" ); mm=Integer.parseInt(d.readLine()); System.out.println( "enter no of columns" ); nn=Integer.parseInt(d.readLine()); /* * inputting the number of rows and number of columns from the user */

/* * if and only when no of rows or and colum ns both are less than 20, * then only we have to the transpose. * if any one or both of them exceed 20 we will display the * appropriate message and not find the tra nspose, as we have * defined the matrix with maximum 20 rows and columns only. */ if (mm<20&&nn<20)

{ Transarray o1= new Transarray(mm,nn); /* * creating object using parameterised constructor * and then calling functins using it t o execute the program. */ o1.fillarray(); System.out.println( "original" ); o1.disparray(); o1.transpose(o1); } else { System.out.println( "no of rows or columns cannot exceed 20" ); } } //end of main

} //end of class

Mar 25, 2014 3:22:34 PM