I'm trying to flip an array row by row

To clarify this, this is homework. I'm just looking for advice, I'm not looking for someone to do my homework for me.

I already did the first half. It uses two arrays to print the Asterisk project (in this case, the letter “S.”. This works fine. Then I skip two lines and print the design, but it flips (so that each line is reversed)., But when I run the program, it prints two S and the second one is not canceled. Any ideas on what I am doing wrong?

public class Design { public static void main (String [] args) { char [] array = new char [150]; for (int index = 0; index < array.length; index ++) { array [index] = '#'; } int [] indexNumbers = { 0,1,2,3,4,5,6,7,8,9,10,20,30,40,50, 60,70,71,72,73,74,75,76,77,78,79,89,99,109,119,129,139,140, 141,142,143,144,145,146,147,148,149 }; for (int i = 0; i < indexNumbers.length; i++) { array [indexNumbers[i]] = ' '; } for (int index = 0; index < array.length; index ++) { if (index % 10 == 0 && index > 0) System.out.println(); System.out.print (array[index]); } //Now, to reverse the letter System.out.println(); System.out.println(); int lines = 5; for (int i = 0; i< array.length; i++){ if (i >= lines) lines += 10; char temp = array [i]; array [i] = array [lines - i - 1]; array [lines - i - 1] = temp; } for (int index = 0; index < array.length; index ++) { if (index % 10 == 0 && index > 0) System.out.println(); System.out.print (array[index]); } } } 

EDIT: Yes ... design in spaces, everything else is asterisks.

+4
source share
3 answers

your reversal is a bit confusing .... simplifies the job if you do it in two cycles.

 for (int row = 0; row < (array.Length / 10); row++) { for (int col = 0; col < 5; col++) { int rowStart = row * 10; int rowEnd = rowStart + 9; char temp = array[rowStart + col]; array[rowStart + col] = array[rowEnd - col]; array[rowEnd - col] = temp; } } 
+1
source

First, why don't you use String[] or char[][] ? Instead, you use a simple array to place multiple lines inside. This makes your code confusing and fragile.

To swap an array, the rule is usually simple: get the first and last row and replace them. Get the second and second last, and replace them, get the third and third last and replace them ... Until you get to the middle. It will be much easier if you have an array where each element is a string (for example, in String[] or in char[][] ).

If you need to keep the idea of ​​a simple char[] , where each 10-char block is a string, just replace each 10-char block as above.

If you do not want to change the general behavior of your program, this is a problematic block:

  int lines = 5; for (int i = 0; i< array.length; i++){ if (i >= lines) lines += 10; char temp = array [i]; array [i] = array [lines - i - 1]; array [lines - i - 1] = temp; } 

You are not exchanging strings here, you are changing characters instead. Thus, your if does not check and skips lines, but instead checks and skips characters.

This is better:

  int lines = array.length / 10; for (int i = 0; i<= lines / 2; i++){ for (int j = 0; j < 10; j++) { char t = array[i * 10 + j]; array[i * 10 + j] = array[(lines - i - 1) * 10 + j]; array[(lines - i - 1) * 10 + j] = t; } } 
+1
source

So..

First of all, start by typing '#' instead of ' ' and '.' instead of '#' . You will see what happens next.

Secondly, you have a problem in the opposite, you actually do not change anything, the way you calculate the index lines - i - 1 is wrong. A good way is (i / 10) * 10 + (10 - (i % 10)) -1 . Yes, this is terribly terrible, but if you want a single line using a one-dimensional array, here it is. Now you need to understand this and integrate it into your code;)

0
source

All Articles