How do you mirror the lines on the horizontal axis of symmetry in the body?



  • There's nothing on the Internet, I want to know how to do it. Here's the program with the body itself.

    class з4 {
      public static void main (String[] args){
        int[][] a=new int[4][5];
        for (int i=0;i < a.length;i++){
          for (int j=0;j < a[i].length;j++){
            a[i][j]=(int)(Math.random()*10);
          }
        }
        for (int i=0;i < a.length;i++,System.out.println()){
          for (int j=0;j < a[i].length;j++){
            System.out.print(a[i][j]+" ");
          }
        }
      }
    }
    


  • Best:

    for (int i = 0; i < a.length / 2; ++i)
    {
        int tmp[] = a[i];
        a[i] = a[a.length - i - 1];
        a[a.length - i - 1] = tmp;
    }
    

    Well, if you want to be harder or you have to save your memory, you can say,

    for (int i = 0; i < a.length; ++i)
        for (int j = 0; j < a[i].length; ++j)
        {
             int tmp = a[i][j];
             a[i][j] = a[a.length - i - 1][j];
             a[a.length - i - 1][j] = tmp;
        }
    

    P.S. Actually, I'm not sure the second option would be to save memory. The first version of the compiler could simply re-examine the references.

    Update

    TC, I'm good tonight. I'll forgive the community... Hold the code:

    class Test4 
    {
        public static void main (String[] args)
        {
            // Инициализация матрицы.
            int[][] a = new int[4][5];
            for (int i = 0; i < a.length; i++)
               for (int j=0; j < a[i].length; j++)
                a[i][j] = (int)(Math.random()*10);
    
        // Вывод исходной матрицы
        for (int i=0;i &lt; a.length;i++,System.out.println())
          for (int j=0;j &lt; a[i].length;j++)
            System.out.print(a[i][j]+" ");
    
        // Трансформация матрицы.
        for (int i = 0; i &lt; a.length / 2; ++i) {
            int tmp[] = a[i];
            a[i] = a[a.length - i - 1];
            a[a.length - i - 1] = tmp;
        }
    
        // Вывод изменённой матрицы.
        System.out.println();
        for (int i=0;i &lt; a.length;i++,System.out.println())
          for (int j=0;j &lt; a[i].length;j++)
            System.out.print(a[i][j]+" ");
    }
    

    }




Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2