Question: Write a program that reverses the order of the elements in each row of the matrix. Print out the resulting matrix. import java.io . *

Write a program that reverses the order of the elements in each row of the matrix.
Print out the resulting matrix. import java.io.* ;
public class Ch67ex06_ReverseRow_4a_stu
{
static int[][] data ={{3,2,5},// ragged array, "data"
{1,4,4,8,13},
{9,1,0,2},
{0,2,6,3,-1,8},
{-1,-2,-3,4,5,45},
{56},
{0,1,2,3,4,5,6,7}};
private static void printArray()
{
PLN("");
for ( int row=0; row < data.length; row++)// process by rows
{
for ( int col=0; col < data[row].length-1; col++)// process each column of a row
P( data[row][col]+","); // scope is one line, not a block
PLN( data[row][data[row].length-1]);
}
}
public static void main ( String[] args )
{
printArray(); // print out the initial array
for ( int row=0; row < data.length; row++)// process all rows
{
int endIdx = data[row].length; // determine ending column number
for ( int col=0; col < endIdx/2 ; col++)//# use half the row; even/odd! #//
{
//---- insert code here ----//
//# standard swap #//
//# put end col into start col #//
//# be careful with 'off by one' #//
}//# half cols because of swapping #//
}
printArray(); // print out the row-reversed array
}
public static void P(Object x){System.out.print(x);}// Shorthand System.out.print()
public static void PLN(Object x){System.out.println(x);}// Shorthand System.out.println()
}

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!