0% found this document useful (0 votes)
7 views9 pages

TwoD Array - Que Solutions

The document contains multiple Java classes that demonstrate operations on 2D arrays, including calculating sums of rows and columns, displaying transposes, extracting diagonals, and sorting rows and columns. Each class prompts the user to input values for the arrays and performs specific operations, such as displaying the original array, calculating sums, and printing transposed or sorted arrays. The document serves as a comprehensive guide for handling 2D arrays in Java.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views9 pages

TwoD Array - Que Solutions

The document contains multiple Java classes that demonstrate operations on 2D arrays, including calculating sums of rows and columns, displaying transposes, extracting diagonals, and sorting rows and columns. Each class prompts the user to input values for the arrays and performs specific operations, such as displaying the original array, calculating sums, and printing transposed or sorted arrays. The document serves as a comprehensive guide for handling 2D arrays in Java.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

/*

Sum of each Row and Column are :


12 4 5 9 => 30
4 2 10 15 => 31
20 5 6 8 => 39
==== ==== ==== ====
36 11 21 32 => 100
*/

import java.util.*;
class SumOfEachRowColumn
{
public static void main( )
{
Scanner SC = new Scanner( System.in );

int NUM [ ][ ] = new int [ 3 ][ 4 ];


int k,n ;

System.out.println( " Enter the 2D Array values : ( Row Major Order ) " );
for( n = 0 ; n < NUM.length ; n++ )
{
for( k = 0 ; k < NUM[0].length ; k++ )
{
System.out.print( "\t Enter any number for Array element" );
System.out.print( " [ " + n + " ][ " + k + " ] : " );
NUM[n][k] = SC.nextInt() ;
}
System.out.println( );
}

System.out.println( "\n Sum of each Row and Column are : " );

int sum;
for( n = 0 ; n < NUM.length ; n++ )
{
sum = 0;
for( k=0 ; k < NUM[0].length ; k++ )
{
System.out.print( "\t" + NUM[n][k] ); // Each Element
sum += NUM[n][k] ;
}
System.out.println( "\t =>\t" + sum ); // Row Total
} // end of outer for

for( k=1 ; k <= NUM[0].length ; k++) // Display Line below each Column
System.out.print( "\t====" );
System.out.println();

Page 1 of 9
int total = 0;
for( n = 0 ; n < NUM[0].length ; n++ )
{
sum = 0;
for( k = 0 ; k < NUM.length ; k++ )
{
sum += NUM[k][n] ;
} // end of inner for
System.out.print( "\t" + sum ); // Each Column Total
total += sum ;
} // end of outer for

System.out.print( "\t =>\t" + total ); // Display Total of Each Elements

} // end of main
} // end of class

= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = == = = =

import java.util.*;
public class Transpose_Display
{
public static void main( )
{
Scanner SC = new Scanner( System.in );

System.out.print( " Enter the No. of Rows required : " );


int ROW = SC.nextInt() ;

System.out.print( " Enter the No. of Columns required : " );


int COL = SC.nextInt() ;

int NUM [ ][ ] = new int[ ROW ][ COL ];


int m,k ;

System.out.println( " Enter the 2D Array values : ( Row Major Order ) " );
for( m =0 ; m < NUM.length ; m++ ) // for No of Rows
{
for( k=0 ; k < NUM[0].length ; k++ ) // For No of Columns
{
System.out.print( "\t Enter any number for Array element" );
System.out.print( " [ " + m + " ][ "+ k + " ] : " );
NUM[m][k] = SC.nextInt() ;
}
System.out.println( );
}

System.out.println( "\n Given 2D Array values are (Row Major Order) : " );
for( m =0 ; m < NUM.length ; m++ ) // for No of Rows
{

Page 2 of 9
for( k=0 ; k < NUM[0].length ; k++ ) // For No of Columns
{
System.out.print( "\t" + NUM[m][k] );
}
System.out.println( );
}

System.out.println( "\n Transpose of given 2D Array is : " );


for( m =0 ; m < NUM[0].length ; m++ ) // For No of Columns
{
for( k=0 ; k < NUM.length ; k++ ) // for No of Rows
{
System.out.print( "\t" + NUM[k][m] );
} // end of inner
System.out.println( );
} // end of outer

} // end of main
} // end of class

= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = == = = =
import java.util.*;
class Transpose_STORE
{
public static void main( int ROW, int COL )
{
Scanner SC = new Scanner( System.in );
int [ ] [ ] NUM = new int[ ROW ][ COL ];
int ans [ ] [ ] = new int [ COL ][ ROW ];
int m,k ;
System.out.println( " Enter the 2D Array values : ( Row Major Order ) " );
for( m =0 ; m < NUM.length ; m++ ) // for No of Rows
{
for( k=0 ; k < NUM[0].length ; k++ ) // For No of Columns
{
System.out.print( "\t Enter any number for Array element" );
System.out.print( " [ " + m + " ][ "+ k + " ] : " );
NUM[m][k] = SC.nextInt() ;
}
System.out.println( );
}
System.out.println( "\n Given 2D Array values are (Row Major Order) : " );
for( m =0 ; m < NUM.length ; m++ ) // for No of Rows
{
for( k=0 ; k < NUM[0].length ; k++ ) // For No of Columns
{
System.out.print( "\t" + NUM[m][k] );
}
System.out.println( );
}
Page 3 of 9
//////////// Storing Transpose in another array ///////
for( m =0 ; m < NUM.length ; m++ ) // for No of Rows
{
for( k=0 ; k < NUM[0].length ; k++ ) // For No of Columns
ans[k][m] = NUM[m][k] ;
} // end of outer
////////////////////////////////////////////

System.out.println( "\n TRANSPOSE of Given 2D Array values are (WRONG):" );


for( m =0 ; m < ans[0].length ; m++ ) // for No of Columns
{
for( k=0 ; k < ans.length ; k++ ) // For No of Rows
{
System.out.print( "\t" + ans[k][m] );
}
System.out.println( );
}

System.out.println( "\n Transpose of 2D Array is : " );


for( m = 0 ; m < ans.length ; m++ ) // For No of Rows
{
for( k = 0 ; k < ans[0].length ; k++ ) // for No of Columns
{
System.out.print( "\t" + ans[m][k] );
} // end of inner
System.out.println( );
} // end of outer

} // end of main
} // end of class

= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = == = = =

import java.util.*;
class Diagonals
{
public static void main( )
{
Scanner SC = new Scanner( System.in );

// int NUM[][] = { {1,2,3,4,5}, {6,7,8,9,10}, {11,12,13,14,15},


// {16,17,18,19,20} , {21,22,23,24,25} } ;

int NUM [ ][ ] = new int[ 5 ][ 5 ];


int k,n ;

System.out.println( " Enter the 2D Array values : ( Row Major Order ) " );
for( n =0 ; n < NUM.length ; n++ )
{
for( k=0 ; k < NUM[0].length ; k++ )
Page 4 of 9
{
System.out.print( "\t Enter any number for Array element" );
System.out.print( " [ " + n + " ][ " + k + " ] : " );
NUM[n][k] = SC.nextInt() ;
}
System.out.println( );
}

System.out.println( "\n Given 2D Array : " );


for( n =0 ; n < NUM.length ; n++ ) // row major Order
{
for( k=0 ; k < NUM[0].length ; k++ )
{
System.out.print( "\t" + NUM[n][k] );
}
System.out.println( );
} // end of outer for

System.out.println( "\n Leading (Principle) Diagonal : " );


for( n =0 ; n < NUM.length ; n++ )
{
for( k=0 ; k < NUM[0].length ; k++ )
{
if( n==k )
System.out.print( "\t" + NUM[n][k] );
else
System.out.print( "\t" );
}
System.out.println( );
} // end of outer for

System.out.println( "\n Leading (Principle) Diagonal (Approach No 2 ): " );


for( n = 0 ; n < NUM.length ; n++ )
{
System.out.print( "\t" + NUM[n][n] );
} // end of for

System.out.println( "\n Right Diagonal : " );


for( n = 0 ; n < NUM.length ; n++ )
{
for( k = 0 ; k < NUM[0].length ; k++ )
{
if( n+k == NUM.length-1 ) // represents Right Diagonal element
System.out.print( "\t" + NUM[n][k] );
else
System.out.print( "\t" );
}
System.out.println( );
} // end of outer for
Page 5 of 9
System.out.println( "\n Leading & Right Diagonal : " );
for( n = 0 ; n < NUM.length ; n++ )
{
for( k = 0 ; k < NUM[0].length ; k++ )
{
if( n==k || (n+k == NUM.length-1) )
System.out.print( "\t" + NUM[n][k] );
else
System.out.print( "\t" );
}
System.out.println( );
} // end of outer for

System.out.println( "\n Right Diagonal (Approach No 2) : " );


for( n=0, k=NUM[0].length-1 ; n < NUM.length ; n++, k-- )
{
System.out.print( "\t" + NUM[n][k] );
} // end of for

System.out.println( "\n Right Diagonal (Approach No 3) is WRONG :" );


for( n=0, k=0 ; n < NUM.length ; n++, k++ )
{
if( (n+k == NUM.length-1) )
System.out.print( "\t" + NUM[n][k] );
} // end of for

System.out.println( "\n Right Diagonal (Approach No 4) :" );


for( n=0 ; n < NUM.length ; n++ )
{
System.out.print( "\t" + NUM[ n ][ NUM.length-n-1 ] );
} // end of for

} // end of main
} // end of class

= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = == = = =

Page 6 of 9
import java.util.*;
class RowWiseSorting
{
private static void BubbleSort ( int ARR [ ] ) // Sort a 1D Array
{
int pass, c, temp;
for( pass = 1 ; pass < ARR.length ; pass++ )
{
for( c = 0 ; c < ARR.length-pass ; c++ )
{
if( ARR[c] > ARR[c+1] )
{
temp = ARR[c];
ARR[c] = ARR[c+1];
ARR[c+1] = temp;
}
} // end of inner for
} // end of outer for
} // end of BubbleSort

public static void main ( int ROW, int COL )


{
Scanner SC = new Scanner( System.in );
int NUM [ ][ ] = new int[ ROW ][ COL ];
int m,k ;
System.out.println( " Enter the 2D Array values : ( Row Major Order ) " );
for( m =0 ; m < NUM.length ; m++ ) // for No of Rows
{
for( k=0 ; k < NUM[0].length ; k++ ) // For No of Columns
{
System.out.print( "\t Enter any number for Array element" );
System.out.print( " [ " + m + " ][ "+ k + " ] : " );
NUM[m][k] = SC.nextInt() ;
}
System.out.println( );
}

System.out.println( "\n Given 2D Array values are (Row Major Order) : " );
for( m =0 ; m < NUM.length ; m++ ) // for No of Rows
{
for( k=0 ; k < NUM[0].length ; k++ ) // For No of Columns
{
System.out.print( "\t" + NUM[m][k] );
}
System.out.println( );
}

Page 7 of 9
//////////// Sort Each Row Individually ///////
for( m =0 ; m < NUM.length ; m++ ) // for No of Rows
{
BubbleSort ( NUM[m] ); // Passing Each Row Individually
}
////////////////////////////////////////////

System.out.println( "\n After Sorting each Row, 2D Array are : " );


for( m =0 ; m < NUM.length ; m++ ) // for No of Rows
{
for( k=0 ; k < NUM[0].length ; k++ ) // For No of Columns
{
System.out.print( "\t" + NUM[m][k] );
}
System.out.println( );
}
} // end of main
} // end of class
= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = == = = =

import java.util.*;

class ColumnWiseSorting
{
private static void BubbleSort ( int ARR[ ] )
{
int pass, c, temp;
for( pass = 1 ; pass < ARR.length ; pass++ )
{
for( c = 0 ; c < ARR.length-pass ; c++ )
{
if( ARR[c] > ARR[c+1] )
{
temp = ARR[c];
ARR[c] = ARR[c+1];
ARR[c+1] = temp;
}
} // end of inner for
} // end of outer for
}

public static void main( int ROW, int COL )


{
Scanner SC = new Scanner( System.in );

int NUM[ ][ ] = new int[ ROW ][ COL ]; // 2D


int temp[ ] = new int[ ROW ]; // 1D
int m,k ;

Page 8 of 9
System.out.println( " Enter the 2D Array values : ( Row Major Order ) " );
for( m =0 ; m < NUM.length ; m++ ) // for No of Rows
{
for( k=0 ; k < NUM[0].length ; k++ ) // For No of Columns
{
System.out.print( "\t Enter any number for Array element" );
System.out.print( " [ " + m + " ][ "+ k + " ] : " );
NUM[m][k] = SC.nextInt() ;
}
System.out.println( );
}

System.out.println( "\n Given 2D Array values are (Row Major Order) : " );
for( m =0 ; m < NUM.length ; m++ ) // for No of Rows
{
for( k=0 ; k < NUM[0].length ; k++ ) // For No of Columns
{
System.out.print( "\t" + NUM[m][k] );
}
System.out.println( );
}

//////////// Sort Each Column Individually ///////


for( m =0 ; m < NUM[0].length ; m++ ) // for No of Columns
{
for( k = 0 ; k < NUM.length ; k++ )
temp[k] = NUM[k][m]; // Transfer from 2D to 1D

BubbleSort ( temp ); // Passing Each Column Individually which is


// stored in Temporary 1D Array

for( k = 0 ; k < NUM.length ; k++ )


NUM[k][m] = temp[k] ; // Transfer from Sorted 1D to 2D
}
////////////////////////////////////////////

System.out.println( "\n After Sorting each Column, 2D Array are : " );


for( m =0 ; m < NUM.length ; m++ ) // for No of Rows
{
for( k=0 ; k < NUM[0].length ; k++ ) // For No of Columns
{
System.out.print( "\t" + NUM[m][k] );
}
System.out.println( );
}

} // end of main
} // end of class

Page 9 of 9

You might also like