# Thread: I don't get this code. I would appreciate an explanation for this

1. Member
Join Date
Jul 2013
Posts
10
Rep Power
0

## I don't get this code. I would appreciate an explanation for this

Java Code:
```public class TwoDimMethods
{
static final int rows = 6; //this can be set to any number
static final int columns = 6; //this can be set to any number

public static void main(String[] args)
{
int[][] board ={{23,5,6,15,18,3},
{4,16,24,67,10,6},
{12,54,23,76,11,22},
{1,12,34,22,8,65},
{81,54,32,67,33,12}
,{56,22,90,22,-6,63}};
int[ ] onedim = new int[36];
printMatrix(board);
largestinRows(board);
largestinColumns(board);

swapper(board, onedim,1<2);

sortit(onedim);

swapper(board, onedim, 1>2);
printMatrix(board);

sumRows(board);
sumColumns(board);

reverseDiagonal(board);
System.out.println("After Reversing Diagonal the board");
printMatrix(board);
}
public static void sortit(int [] single){
int temp;
for(int i=0; i< single.length-1 ; i++)
for(int j=0; j< single.length-1-i ; j++){

if( single[j]> single[j+1]) {
temp = single[j];
single[j]= single[j+1];
single[j+1] =temp;}
}}
public static void swapper(int[][] matrix, int [] single, boolean t)
{
int row, col, pos=0;

for(row = 0; row < matrix.length; row++)
{
for(col = 0; col < matrix[row].length; col++){
if(t) single[pos++]= matrix[row][col] ;
else matrix[row][col]= single[pos++]; }
}
}

public static void printMatrix(int[][] matrix)
{
int row, col;

for(row = 0; row < matrix.length; row++)
{
for(col = 0; col < matrix[row].length; col++)
System.out.print(matrix[row][col] + "\t");
System.out.println();
}
}

public static void sumRows(int[][] matrix)
{
int row, col;
int sum;

//sum of each individual row
for(row = 0; row < matrix.length; row++)
{
sum = 0;
for(col = 0; col < matrix[row].length; col++)
sum = sum + matrix[row][col];
System.out.println("Sum of row " + (row+1) + " = " + sum);
}
}

public static void sumColumns(int[][] matrix)
{
int row, col;
int sum;

//sum of each individual column
for(col = 0; col < matrix[0].length; col++)
{
sum = 0;
for(row = 0; row < matrix.length; row++)
sum = sum + matrix[row][col];
System.out.println("Sum of column " + (col+1) + " = " + sum);
}
}

public static void largestinRows(int[][] matrix)
{
int row, col;
int largest;

//Largest element in each row
for(row = 0; row < matrix.length; row++)
{
largest = matrix[row][0]; //assume that the first element of
//the row is largest
for(col = 1; col < matrix[row].length; col++)
if(largest < matrix[row][col])
largest = matrix[row][col];
System.out.println("Largest element of row " + (row+1) + " = "
+ largest);
}
}

public static void largestinColumns(int[][] matrix)
{
int row, col;
int largest;

//Largest element in each column
for(col = 0; col < matrix[0].length; col++)
{
largest = matrix[0][col]; //assume the the first element of
//the column is largest
for(row = 1; row < matrix.length; row++)
if(largest < matrix[row][col])
largest = matrix[row][col];
System.out.println("Largest element of col " + (col+1) + " = "
+ largest);
}
}

public static void reverseDiagonal(int[][] matrix)
{
int row;
int temp;

//Reverse main diagonal
for(row = 0; row < matrix.length / 2; row++)
{
temp = matrix[row][row];
matrix[row][row] = matrix[rows-1-row][rows-1-row];
matrix[rows-1-row][rows-1-row] = temp;
}

//Reverse the opposite diagonal
for(row = 0; row < matrix.length / 2; row++)
{
temp = matrix[row][rows-1-row];
matrix[row][rows-1-row] = matrix[rows-1-row][row];
matrix[rows-1-row][row] = temp;
}
}
}```

2. ## Re: I don't get this code. I would appreciate an explanation for this

Do you have any specific questions about some lines of code? Please post the question and say which lines you have questions about.

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•