java - Mirroring an array -
i trying mirror array. in other words, if create array looks (the user gives size):
0 2 4 6 7 8 9 10 11 12
after mirroring, should this:
0 2 4 2 0 8 9 10 9 8
however, doesn't work when make 3 x 5 array or bigger. repeats same number this:
0 2 4 2 2 8 9 10 8 8
this code far:
import java.util.scanner; public class mirror { public static void main (string [] args) { scanner kb = new scanner (system.in); system.out.println("please enter number of rows: "); int rows = kb.nextint(); system.out.println("please enter number of columns:"); int columns = kb.nextint(); int [][] mirror = new int [rows][columns]; int sum =0; (int r = 0; r<mirror.length; r++) { (int c = 0; c<mirror[r].length ; c++) { mirror[r][c]= sum; sum= sum+2; } } (int r = 0; r<mirror.length; r++) { (int c = 0; c<mirror[r].length ; c++) { system.out.print (mirror[r][c]+ " "); } system.out.println(); } int add = 0; (int r = 0; r<mirror.length; r++) { (int c = mirror[r].length/2 +1; c<mirror[r].length ; c++) { (int x= 0; x<mirror[r].length/2; x=x++) { mirror[r][c] = mirror [r][x]; } } } system.out.println(); (int r = 0; r<mirror.length; r++) { (int c = 0; c<mirror[r].length ; c++) { system.out.print (mirror[r][c]+ " "); } system.out.println(); } } }
do guys have ideas on problem is? tried debug can't find problem. sorry , thankyou, beginner.
you doing more loops necessary.
try changing this:
for (int r = 0; r<mirror.length; r++) { /*for (int c = mirror[r].length/2 +1; c<mirror[r].length ; c++) { (int x= 0; x<mirror[r].length/2; x=x++) { mirror[r][c] = mirror [r][mirror[r].length/2 +1 -c]; } }*/ for(int c=0; c<mirror[r].length; c++){ if(c<mirror[r].length/2.0) mirror[r][mirror[r].length-1-c] = mirror[r][c]; } }
you need 1 loop row, in loop using if (to detect middle), can want.
Comments
Post a Comment