Home > Software design >  Pattern-printing program - Unable to print the mirrored part in reverse order
Pattern-printing program - Unable to print the mirrored part in reverse order

Time:04-03

I am trying to create a pattern printing program.

I'm not able to print my pattern in reverse order. I know there's some logical error in the second for loop, but I can't identify it.

    public class test {
      public static void main(String[] args) {
        int num = 7;
        int temp1 = 1;
        int temp2 = 1;
        for (int i = 1; i <= num / 2; i  ) {
          for (int j = 1; j <= num - i; j  ) {
            System.out.print(" ");
          }
          for (int k = 1; k <= temp1; k  ) {
            System.out.print(Math.abs(k - temp2));
          }
          temp1  = 2;
          temp2  ;
          System.out.println();
        }
        temp1 = 1;
        temp2 = 1;
        for (int i = num - (num / 2); i >= 1; i--) {
          for (int j = 1; j <= num - i; j  ) {
            System.out.print(" ");
          }
          for (int k = temp1; k >= 1; k--) {
            System.out.print(Math.abs(k - temp2));
          }
          temp1  = 2;
          temp2  ;
          System.out.println();
        }
      }
    }

Program output:

      0   
     101  
    21012 
   0      
    101   
     21012
      3210123

Expected output:

      0   
     101  
    21012 
   3210123
    21012
     101
      0

CodePudding user response:

Because the figure you need to print consist of three parts, you have to spit your solution into three separate methods. So it'll be easier to test them.

The top and bottom a very similar. The simplest approach you can take is to reuse the same code for bottom part with only one change: the index of the outer for is being initialed with a max value and will get decremented at each step of iteration (you get it right, but you've also changed the inner for loop because your first block of loops is also responsible for printing the middle part).

public static void print(int size) {
    printTopPart(size);
    if (size % 2 == 1) { // middle part will be printed if size is odd
        printMiddlePart(size);
    }
    printBottomPart(size);
}

public static void printTopPart(int size) {
    int height = size / 2;
    for (int row = 0; row < height; row  ) {
        // print the white space
        for (int i = 0; i < height - row; i  ) {
            System.out.print(" ");
        }
        // print the row of numbers
        for (int i = 0; i < row * 2   1; i  ) {
            System.out.print(Math.abs(row - i));
        }
        System.out.println(); // advance the output to the next row
    }
}

public static void printMiddlePart(int size) {
    // print the row of numbers
    for (int i = 0; i < size; i  ) {
        System.out.print(Math.abs(size / 2 - i));
    }
    System.out.println(); // advance the output to the next row
}

public static void printBottomPart(int size) {
    int height = size / 2;
    for (int row = height - 1; row >= 0; row--) {
        // print the white space
        for (int i = 0; i < height - row; i  ) {
            System.out.print(" ");
        }
        // print the row of numbers
        for (int i = 0; i < row * 2   1; i  ) {
            System.out.print(Math.abs(row - i));
        }
        System.out.println(); // advance the output to the next row
    }
}

main() - demo

public static void main(String[] args) {
    print(7);
}

Output

   0
  101
 21012
3210123
 21012
  101
   0
  • Related